Laravel 5: Difference between revisions

10,465 bytes removed ,  28 February 2015
move installation procedures to a separate page
→‎Generate an application key: add more explanation for encryption key
move installation procedures to a separate page
Line 1: Line 1:
== Installation ==
[[Laravel 5 installation on Ubuntu 14.04|Installation on Ubuntu 14.04]]


* ''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 an hour or more
== Alternative installation using Homestead ==


=== Prerequisite environment setup ===
* ''Tested on Windows 8.1 (x64) / medium / about an hour or more
 
Make sure you have PHP 5.4 or greater version installed. On a Debian-derived distribution such as Ubuntu, it's as simple as <code># apt-get install php5</code>.
 
Get the git. <code># apt-get install git</code>
 
Other basic requirements are mcrypt, mbstring. OpenSSL support is built-in on this version<ref name="openssl">''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>.</ref>. I also install JSON, xdebug, sqlite, readline, mysql, memcached, intl, and curl.
 
<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;">
$ sudo apt-get install php5-mcrypt php5-json php5-xdebug php5-sqlite php5-readline php5-mysql php5-memcached php5-intl php5-curl
</span>
</div>
 
mbstring is a part of libapache2-mod-php5 package<ref name="mbstring">''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>.</ref>.
 
<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;">
$ sudo apt-get install libapache2-mod-php5
</span>
</div>
 
=== Files and applications ===
 
Download Laravel installer using Composer.
 
<source lang="bash" highlight="1">
$ 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)
    Down    Downloading: 100%
 
  - Installing symfony/console (v2.6.4)
    Down    Downloading: 100%
 
  - Installing guzzlehttp/streams (2.1.0)
    Down    Downloading: 100%
 
  - Installing guzzlehttp/guzzle (4.2.3)
    Down    Downloading: 100%
 
  - Installing laravel/installer (v1.2.0)
    Down    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
</source>
 
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.
 
<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;">
$ ln -s ~/.composer/vendor/bin/laravel ~/bin/laravel
</span>
</div>
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.
 
<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;">
$ laravel new sanban
</span><br>
<span style="color:#00aa00;">Crafting application...</span><br>
<span style="color:#cccccc;">
Generating optimized class loader<br>
Compiling common classes<br>
Compiling views<br>
Application key [CJwBsllwDbWLlxy7zH7zRATmSu2laUyA] set successfully.<br>
<span style="color: yellow">Application ready! Build something amazing.</span>
</span>
</div>
 
Then I enable access to this new application via web. This is an apache config.
 
<source lang="apache">
<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>
</source>
 
Make sure to restart the httpd (i.e. <code>$ service apache2 restart</code> on Ubuntu).
 
=== Post-installation updates ===
 
Laravel installation through its own installer may not get the latest version, so execute <code>$ composer update</code> once from the project home directory. In this example, I'd run it from <span class="shell">/srv/www/sanban</span>.
 
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">
<?php
// config/app.php
..
'url' => 'http://hostname.com',
..
'timezone' => 'America/Denver',
..
</source>
 
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.
 
<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;">
$ <b class="color:white;">sudo chown -R www-data:mhan storage</b>
</span>
</div>
 
I also like to add a link to <span class="package">artisan</span> PHP executable into my <span class="shell">~/bin</span> folder.
 
<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;">
$ <b class="color:white;">chmod u+x artisan.sanban && ln -s /srv/www/sanban/artisan ~/bin/artisan.sanban</b>
</span>
</div>
 
I add the application short name as an extension to <span class="package">artisan</span> because I have multiple instances of Laravel applications and create links to corresponding <span class="package">artisan</span> executables.
 
==== 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.
 
<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;">
$ <b class="color:white;">chmod u+x vendor/phpunit/phpunit/phpunit</b>
$ <b class="color:white;">ln -s /srv/www/sanban/vendor/phpunit/phpunit/phpunit ~/bin/phpunit.sanban</b>
</span>
</div>
 
I also want the unit testing to stop on a failure.  You can change <span class="package">stopOnFailure</span> attribute to <span class="package">true</span> in <span class="shell">phpunit.xml</span> file.
 
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">
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>
 
In order for this to work properly your project directory has to be initialized as a git repository.
 
<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;">
$ <b class="color:white;">git init</b><br>
<span style="color:#cccccc">Initialized empty Git repository in /srv/www/sanban/.git/</span>
</span>
</div>
 
==== 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 <code>i</code> for <span class="shell">/vendor/laravel/framework/src/Illuminate</span>.
 
<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;">
$ <b class="color:white;">ln -s ./vendor/laravel/framework/src/Illuminate i</b>
</span>
</div>
 
== Configuration ==
 
==== Name the application ====
 
I'm naming the application <span class="package">sanban</span>.
 
<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;">
$ <b style="color:white">artisan.sanban app:name Sanban</b>
</span>
</div>
 
==== Generate an application key ====
 
Check your <span class="package">.env</span> 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.
 
<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;">
$ <b style="color:white">artisan.sanban key:generate</b>
</span>
</div>
 
== References ==
<div class="references-small"><references /></div>