Laravel 5 installation on Ubuntu 14.04: Difference between revisions

From Han Wiki
Jump to navigation Jump to search
→‎Post-installation updates: add a note for Nginx users
 
(12 intermediate revisions by 2 users not shown)
Line 13: Line 13:
<div class="cli">
<div class="cli">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">~</span><span style="color:#cccccc;">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">~</span><span style="color:#cccccc;">
$ sudo apt-get install php5-mcrypt php5-json php5-xdebug php5-sqlite php5-readline php5-mysql php5-memcached php5-intl php5-curl
$ <b class="color:white;">sudo apt-get install php5-mcrypt php5-json php5-xdebug php5-sqlite php5-readline php5-mysql php5-memcached php5-intl php5-curl</b>
</span>
</span>
</div>
</div>
Line 21: Line 21:
<div class="cli">
<div class="cli">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">~</span><span style="color:#cccccc;">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">~</span><span style="color:#cccccc;">
$ sudo apt-get install libapache2-mod-php5
$ <b class="color:white;">sudo apt-get install libapache2-mod-php5</b>
</span>
</span>
</div>
</div>
Line 29: Line 29:
Download Laravel installer using Composer.
Download Laravel installer using Composer.


<source lang="bash" highlight="1">
<div class="cli">
mhan@brahms:~$ composer global require "laravel/installer=~1.1"
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">~</span><span style="color:#cccccc;">
Changed current directory to /home/mhan/.composer
$ <b class="color:white;">composer global require "laravel/installer=~1.1"</b>
./composer.json has been created
</span><br>
Loading composer repositories with package information
<span style="color:#00aa00">Changed current directory to /home/mhan/.composer<br>
Updating dependencies (including require-dev)
./composer.json has been created<br>
  - Installing symfony/process (v2.6.4)
Loading composer repositories with package information<br>
    Down    Downloading: 100%
Updating dependencies (including require-dev)<br></span>
 
&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>- Installing</nowiki></span> <span style="color:#00aa00">symfony/process </span><span style="color:#cccccc">(</span><span style="color:#aaaa00">v2.6.4</span><span style="color:#cccccc">)</span><br>
  - Installing symfony/console (v2.6.4)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>Downloading: </nowiki></span><span style="color:#aaaa00">100%</span><br>
    Down    Downloading: 100%
<br>
 
&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>- Installing</nowiki></span> <span style="color:#00aa00">symfony/console </span><span style="color:#cccccc">(</span><span style="color:#aaaa00">v2.6.4</span><span style="color:#cccccc">)</span><br>
  - Installing guzzlehttp/streams (2.1.0)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>Downloading: </nowiki></span><span style="color:#aaaa00">100%</span><br>
    Down    Downloading: 100%
<br>
 
&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>- Installing</nowiki></span> <span style="color:#00aa00">guzzlehttp/streams </span><span style="color:#cccccc">(</span><span style="color:#aaaa00">v2.1.0</span><span style="color:#cccccc">)</span><br>
  - Installing guzzlehttp/guzzle (4.2.3)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>Downloading: </nowiki></span><span style="color:#aaaa00">100%</span><br>
    Down    Downloading: 100%
<br>
 
&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>- Installing</nowiki></span> <span style="color:#00aa00">guzzlehttp/guzzle </span><span style="color:#cccccc">(</span><span style="color:#aaaa00">4.2.3</span><span style="color:#cccccc">)</span><br>
  - Installing laravel/installer (v1.2.0)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>Downloading: </nowiki></span><span style="color:#aaaa00">100%</span><br>
    Down    Downloading: 100%
<br>
 
&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>- Installing</nowiki></span> <span style="color:#00aa00">laravel/installer </span><span style="color:#cccccc">(</span><span style="color:#aaaa00">v1.2.0</span><span style="color:#cccccc">)</span><br>
symfony/console suggests installing symfony/event-dispatcher ()
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#cccccc"><nowiki>Downloading: </nowiki></span><span style="color:#aaaa00">100%</span><br>
symfony/console suggests installing psr/log (For using the console logger)
<br><span style="color:#cccccc">
Writing lock file
symfony/console suggests installing symfony/event-dispatcher ()<br>
Generating autoload files
symfony/console suggests installing psr/log (For using the console logger)</span><br>
</source>
<span style="color:#00aa00">Writing lock file<br>
Generating autoload files</span>
</div>


Instead of adding <span class="shell">~/.composer/vendor/bin</span> to the PATH environment variable, I simply made a symbolic link to it in <span class="shell">~/bin/</span> folder.
Instead of adding <span class="shell">~/.composer/vendor/bin</span> to the PATH environment variable, I simply made a symbolic link to it in <span class="shell">~/bin/</span> folder.
Line 60: Line 62:
<div class="cli">
<div class="cli">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www</span><span style="color:#cccccc;">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www</span><span style="color:#cccccc;">
$ ln -s ~/.composer/vendor/bin/laravel ~/bin/laravel
$ <b class="color:white;">ln -s ~/.composer/vendor/bin/laravel ~/bin/laravel</b>
</span>
</span>
</div>
</div>
Line 67: Line 69:
<div class="cli">
<div class="cli">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www</span><span style="color:#cccccc;">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www</span><span style="color:#cccccc;">
$ laravel new sanban
$ <b class="color:white;">laravel new sanban</b>
</span><br>
</span><br>
<span style="color:#00aa00;">Crafting application...</span><br>
<span style="color:#00aa00;">Crafting application...</span><br>
Line 75: Line 77:
Compiling views<br>
Compiling views<br>
Application key [CJwBsllwDbWLlxy7zH7zRATmSu2laUyA] set successfully.<br>
Application key [CJwBsllwDbWLlxy7zH7zRATmSu2laUyA] set successfully.<br>
<span style="color: yellow">Application ready! Build something amazing.</span>
<span style="color: #aaaa00">Application ready! Build something amazing.</span>
</span>
</span>
</div>
</div>
Line 96: Line 98:
</source>
</source>


Make sure to restart the httpd (i.e. <code>$ service apache2 restart</code> on Ubuntu).
Make sure to restart the httpd (i.e. <code>$ service apache2 restart</code> on Ubuntu). You should be able to access your website and see the big Laravel 5 text with a quote on the home page.


=== Post-installation updates ===
=== Post-installation updates ===
Line 104: Line 106:
Update <span class="package">app.url</span>, <span class="package">app.timezone</span><ref name="timezone">''PHP Manual''. Program documentation. ''PHP Manual''. N.p., n.d. Web. 26 Feb 2015. <http://php.net/manual/en/timezones.php>.</ref>.
Update <span class="package">app.url</span>, <span class="package">app.timezone</span><ref name="timezone">''PHP Manual''. Program documentation. ''PHP Manual''. N.p., n.d. Web. 26 Feb 2015. <http://php.net/manual/en/timezones.php>.</ref>.


<source lang="php">
<syntaxhighlight lang="php">
<?php
<?php
// config/app.php
// config/app.php
Line 112: Line 114:
'timezone' => 'America/Denver',
'timezone' => 'America/Denver',
..
..
</source>
</syntaxhighlight>


It may be just fine to leave the timezone to UTC if you are going to be accommodate for user's particular timezone through some means. If the application isn't going to be serving users from different time zones and you don't plan to scale it then set it to your local one.
It may be just fine to leave the timezone to UTC if you are going to be accommodate for user's particular timezone through some means. If the application isn't going to be serving users from different time zones and you don't plan to scale it then set it to your local one.
Line 120: Line 122:
<div class="cli">
<div class="cli">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www/sanban</span><span style="color:#cccccc;">
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www/sanban</span><span style="color:#cccccc;">
$ <b class="color:white;">sudo chown -R www-data:mhan storage</b>
$ <b class="color:white;">sudo chown -R www-data:www-data storage</b>
</span><br>
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www/sanban</span><span style="color:#cccccc;">
$ <b class="color:white;">sudo chmod -R 0664 storage</b>
</span><br>
<span style="color:#00ff00;">mhan</span><span style="color:#444444;">@</span><span style="color:#00cccc;">brahms</span><span style="color:#999999">:</span><span style="color:blue;">/srv/www/sanban</span><span style="color:#cccccc;">
$ <b class="color:white;">sudo find storage -type d -exec chmod 2775 '{}' \;</b>
</span>
</span>
</div>
<div class="mw-geshi">
NOTE FOR NGINX USERS: I know this instruction was originally written for Apache web server, but if you're using Nginx and you happen to be using something else than Ubuntu, then you can replace the line that contains 'www-data' with the following:
$ sudo chown -R `grep user /etc/nginx/nginx.conf | sed 's/user //g; s/;//g'`:`grep user /etc/nginx/nginx.conf | sed 's/user //g; s/;//g'` storage
</div>
</div>


Line 136: Line 150:
==== For unit testing ====
==== For unit testing ====


For compatibility reasons, it's better to use the composer-installed phpunit package.  You can do this by running <code>$ composer update --dev</code> which will install phpunit package automatically.  You may also want to create a symlink to it. This example creates a phpunit.sanban symlink which you can use for this particular instance.
For compatibility reasons, it's better to use the composer-installed phpunit package.  I didn't have enough time to figure out what was wrong, but my pre-installed version of <span class="package">phpunit</span> wasn't working properly. You can do this by running <code>$ composer update --dev</code> which will install phpunit package automatically.  You may also want to create a symlink to it. This example creates a phpunit.sanban symlink which you can use for this particular instance.


<div class="cli">
<div class="cli">
Line 149: Line 163:
If you use VIM like I do, you can insert this into your <span class="shell">~/.vimrc</span> file and just hit <code>,t</code> (comma and then t) key combination to run the unit test wherever you are under the project directory. This is a bit long because I have some repetitive bash commands to automatically detect the current application name and use that, i.d. "sanban" in this example.
If you use VIM like I do, you can insert this into your <span class="shell">~/.vimrc</span> file and just hit <code>,t</code> (comma and then t) key combination to run the unit test wherever you are under the project directory. This is a bit long because I have some repetitive bash commands to automatically detect the current application name and use that, i.d. "sanban" in this example.


<source lang="vim">
<syntaxhighlight lang="vim">
nmap ,t :!if [ -d .git ] \|\| git rev-parse --git-dir > /dev/null 2>&1; then phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}') -c $(git rev-parse --show-toplevel); else phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}'); fi<cr>
nmap ,t :!if [ -d .git ] \|\| git rev-parse --git-dir > /dev/null 2>&1; then phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}') -c $(git rev-parse --show-toplevel); else phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}'); fi<cr>
</source>
</syntaxhighlight>


In order for this to work properly your project directory has to be initialized as a git repository.
In order for this to work properly your project directory has to be initialized as a git repository.
Line 196: Line 210:
== References ==
== References ==
<div class="references-small"><references /></div>
<div class="references-small"><references /></div>
[[Category:Web development]][[Category:Laravel]]

Latest revision as of 10:14, 8 July 2016

Installation

  • Tested on Ubuntu 14.04.2 LTS, Apache/2.4.9, PHP 5.5.15RC1, Laravel (5.0; installer version 1.2.0) / medium / about 30 minutes

Prerequisite environment setup

Make sure you have PHP 5.4 or greater version installed. On a Debian-derived distribution such as Ubuntu, it's as simple as # apt-get install php5.

Get the git. # apt-get install git

Other basic requirements are mcrypt, mbstring. OpenSSL support is built-in on this version[1]. I also install JSON, xdebug, sqlite, readline, mysql, memcached, intl, and curl.

mhan@brahms:~ $ sudo apt-get install php5-mcrypt php5-json php5-xdebug php5-sqlite php5-readline php5-mysql php5-memcached php5-intl php5-curl

mbstring is a part of libapache2-mod-php5 package[2].

mhan@brahms:~ $ sudo apt-get install libapache2-mod-php5

Files and applications

Download Laravel installer using Composer.

mhan@brahms:~ $ composer global require "laravel/installer=~1.1"
Changed current directory to /home/mhan/.composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/process (v2.6.4)
    Downloading: 100%

  - Installing symfony/console (v2.6.4)
    Downloading: 100%

  - Installing guzzlehttp/streams (v2.1.0)
    Downloading: 100%

  - Installing guzzlehttp/guzzle (4.2.3)
    Downloading: 100%

  - Installing laravel/installer (v1.2.0)
    Downloading: 100%

symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing psr/log (For using the console logger)

Writing lock file
Generating autoload files

Instead of adding ~/.composer/vendor/bin to the PATH environment variable, I simply made a symbolic link to it in ~/bin/ folder.

mhan@brahms:/srv/www $ ln -s ~/.composer/vendor/bin/laravel ~/bin/laravel

Create a new Laravel application. For whatever reason the application key doesn't seem to set even though it says so. I had to do this manually later.

mhan@brahms:/srv/www $ laravel new sanban
Crafting application...
Generating optimized class loader
Compiling common classes
Compiling views
Application key [CJwBsllwDbWLlxy7zH7zRATmSu2laUyA] set successfully.
Application ready! Build something amazing.

Then I enable access to this new application via web. This is an apache config.

<VirtualHost *:80>
        ServerAdmin admin@hostname.com
        ServerName sanban.hostname.com
        ServerSignature Off
        DocumentRoot /srv/www/sanban/public

        <Directory /srv/www/sanban/public>
                Options -Indexes +FollowSymLinks -MultiViews
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>

Make sure to restart the httpd (i.e. $ service apache2 restart on Ubuntu). You should be able to access your website and see the big Laravel 5 text with a quote on the home page.

Post-installation updates

Laravel installation through its own installer may not get the latest version, so execute $ composer update once from the project home directory. In this example, I'd run it from /srv/www/sanban.

Update app.url, app.timezone[3].

<?php
// config/app.php
..
'url' => 'http://hostname.com',
..
'timezone' => 'America/Denver',
..

It may be just fine to leave the timezone to UTC if you are going to be accommodate for user's particular timezone through some means. If the application isn't going to be serving users from different time zones and you don't plan to scale it then set it to your local one.

Make sure storage is writable by the web server. There are several ways to do this, and one of the ways is to change the ownership so that the web server and you, a developer, can write freely to it.

mhan@brahms:/srv/www/sanban $ sudo chown -R www-data:www-data storage
mhan@brahms:/srv/www/sanban $ sudo chmod -R 0664 storage
mhan@brahms:/srv/www/sanban $ sudo find storage -type d -exec chmod 2775 '{}' \;

NOTE FOR NGINX USERS: I know this instruction was originally written for Apache web server, but if you're using Nginx and you happen to be using something else than Ubuntu, then you can replace the line that contains 'www-data' with the following:

$ sudo chown -R `grep user /etc/nginx/nginx.conf | sed 's/user //g; s/;//g'`:`grep user /etc/nginx/nginx.conf | sed 's/user //g; s/;//g'` storage

I also like to add a link to artisan PHP executable into my ~/bin folder.

mhan@brahms:/srv/www/sanban $ chmod u+x artisan.sanban && ln -s /srv/www/sanban/artisan ~/bin/artisan.sanban

I add the application short name as an extension to artisan because I have multiple instances of Laravel applications and create links to corresponding artisan executables.

For unit testing

For compatibility reasons, it's better to use the composer-installed phpunit package. I didn't have enough time to figure out what was wrong, but my pre-installed version of phpunit wasn't working properly. You can do this by running $ composer update --dev which will install phpunit package automatically. You may also want to create a symlink to it. This example creates a phpunit.sanban symlink which you can use for this particular instance.

mhan@brahms:/srv/www/sanban $ chmod u+x vendor/phpunit/phpunit/phpunit $ ln -s /srv/www/sanban/vendor/phpunit/phpunit/phpunit ~/bin/phpunit.sanban

I also want the unit testing to stop on a failure. You can change stopOnFailure attribute to true in phpunit.xml file.

If you use VIM like I do, you can insert this into your ~/.vimrc file and just hit ,t (comma and then t) key combination to run the unit test wherever you are under the project directory. This is a bit long because I have some repetitive bash commands to automatically detect the current application name and use that, i.d. "sanban" in this example.

nmap ,t :!if [ -d .git ] \|\| git rev-parse --git-dir > /dev/null 2>&1; then phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}') -c $(git rev-parse --show-toplevel); else phpunit.$(cat $(git rev-parse --show-toplevel)/phpspec.yml \| grep "namespace" \| awk '{print tolower(substr($0,20));exit}'); fi<cr>

In order for this to work properly your project directory has to be initialized as a git repository.

mhan@brahms:/srv/www/sanban $ git init
Initialized empty Git repository in /srv/www/sanban/.git/

Personal preferences

I browse, search, and many other things all in CLI, and sometimes it's cumbersome to type in long path names. Symlinks to the rescue! I use i for /vendor/laravel/framework/src/Illuminate.

mhan@brahms:/srv/www/sanban $ ln -s ./vendor/laravel/framework/src/Illuminate i

Configuration

Name the application

I'm naming the application sanban.

mhan@brahms:/srv/www/sanban $ artisan.sanban app:name Sanban

Generate an application key

Check your .env file to make sure you have an encryption key in there. If don't find it there for whatever reason you can generate one with this command.

mhan@brahms:/srv/www/sanban $ artisan.sanban key:generate

References

  1. Stack Exchange. Program documentation. Stack Exchange. N.p., n.d. Web. 26 Feb. 2015. <http://askubuntu.com/questions/323005/php-openssl-extension-has-a-package>.
  2. Stack Exchange. Program documentation. Stack Exchange. N.p., n.d. Web. 26 Feb. 2015. <http://askubuntu.com/questions/491629/how-to-install-php-mbstring-extension-in-ubuntu>.
  3. PHP Manual. Program documentation. PHP Manual. N.p., n.d. Web. 26 Feb 2015. <http://php.net/manual/en/timezones.php>.