Install Laravel 4 on Ubuntu 12.04 LTS (a how-to tutorial)
Laravel 4 is the big thing. Every blog talks about it, nearly every developer-twitter-account mentions it. Hmm, looks like everybody should try it out ! But – to my surprise – the official installation tutorial is quite confusing and also incomplete. If you install Laravel exactly like described on http://laravel.com/docs/quick#installation, well, then you’ll be stuck right after the first seconds. There’s another, more detailed installation tutorial, right in http://laravel.com/docs/installation, but this will also not clearly help you with the installation process as it simply leaves out essential steps in the setup, and you’ll have to do a lot of additional stuff to get this thing running. That’s probably why you are reading this, right ? ;) So here’s “my” full step-by-step guideline on how to install laravel 4.0 and 4.1 on a naked Ubuntu 12.04 LTS.
Preparing the server / environment
Log in into your naked Ubuntu 12.04 LTS and do an update and upgrade:
sudo apt-get update sudo apt-get upgrade
Choosing the right PHP version: Laravel 4 uses Composer to install. And currently there’s a bug in composer which makes installation of dependencies extremely slow, like 60 minutes for something that usually takes 10 seconds. This bug only happens on PHP 5.3 systems, so installing PHP 5.4 might be a good choice when Composer runs very very slow. Upgrading to PHP 5.5 is (currently, November 2013) not a good choice as this will also install Apache 2.4 which has different config files, and I also don’t know how to rewrite the Apache 2.2 stuff to 2.4.
OPTIONAL: Preparation of installing PHP 5.4: This can be skipped if you are fine with 5.3.
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php5-oldstable sudo apt-get update sudo apt-cache policy php5
The last command shows the to-be-installed version of PHP (on the top) . Should be 5.4.x! This process replaces the default PHP version by the chosen one, in this case the “old-stable”, 5.4.
Installing Apache, PHP and MySQL
sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install mysql-server sudo apt-get install php5-mysql
The installation process is self-explaining. You can prove the installed versions of PHP and Apache with:
php -v
and
apache2 -v
Installing necessary PHP extensions
sudo apt-get install unzip sudo apt-get install curl sudo apt-get install openssl sudo apt-get install php5-mcrypt
Usually these 4 are not installed by default, but they are necessary to 1.) unzip the .zip’ped Laravel 4 download, 2.) use Composer (which needs CURL), 3.) use Composer (which downloads via HTTPS, useable only with installed OpenSSL) 4.) use Laravel 4 (which needs mcrypt).
Install Composer (systemwide)
In case you wonder: The download of Composer into each project “by hand” is not recommended anymore. We use the “real” installation of Composer here:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
Activate mod_rewrite
Install the mod_rewrite module (or extension or whatever it is) and restart the Apache:
sudo a2enmod rewrite sudo service apache2 restart
Open the default vhost config file:
sudo nano /etc/apache2/sites-available/default
Now search for “AllowOverride None” (which should be there TWO times) and change both to “AllowOverride All“. Search for these two lines
DocumentRoot /var/www <Directory /var/www>
and change them to
DocumentRoot /var/www/public <Directory /var/www/public>
This will route a user who enters your server directly to /var/www/public (the base folder of Laravel 4) instead of /var/www. Exit and save with CTRL+X, Y, ENTER.
Install Laravel 4
These commands will move you into the web root (/var/www), download Laravel from GitHub, extract the files into a folder called “laravel-master”, move it’s content into /var/www and delete the download and the empty folder:
cd /var/www wget https://github.com/laravel/laravel/archive/master.zip unzip master.zip && cd laravel-master/ && mv * ../ && cd .. rm -r laravel-master && rm master.zip
Run the installation with Composer by
composer install
Note from comments: Eventually you’ll have to do a sudo composer install.
Make the storage folder writeable (777 is too much for production servers, but it’s okay for this tutorial. Would be cool if more experienced linux guys could comment here: What’s the best overall-working way to give PHP write-rights to this folder ?)
sudo chmod -R 777 app/storage
and restart the server:
sudo service apache2 restart
First run of Laravel 4
When you navigate to your server’s IP (or domain), you should see the Laravel startscreen:
Now edit app/routes.php and add a new route:
Route::get('/mytest', function() { return "Oh yeah, this really works !"; });
When you navigate to your server’s IP/adress and add “/mytest” to the URL, you should see “Oh yeah, this really works !” in the browser.
http://your-domain-or-ip/mytest
This is basically how laravel works. To get an idea what’s possible with routes, have a look on http://laravel.com/docs/quick ! Feel free to comment, share and rate this tutorial.