This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead: This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
Laravel is a framework for websites in the PHP programming language. It allows developers to rapidly develop a website by abstracting common tasks used in most web projects, such as authentication, sessions, and caching. Laravel 4, the newest version of Laravel is based on an older framework called Symfony, but with a more expressive syntax. It is installed using Composer, a Dependency Manager, allowing developers to integrate even more open source PHP projects in a web project. If you want to want to read a quick introduction to Laravel, read the introduction. If you want to learn more about Composer, visit the website.
Let’s start off by updating the packages installed on your VPS. This makes sure no issues will arise on incompatible versions of the software. Also, make sure you run everything in this tutorial as root, and if you don’t, make sure you add sudo
before every command!
apt-get update && apt-get upgrade
Hit Enter when you’re asked to confirm.
Now we need to install the actual packages required to install Laravel. This will basically be Nginx and PHP. Because Composer is run from the command line, we do need php5-cli
, and because we want to manage the connection between Nginx and PHP using the FastCGI Process Manager, we will need php5-fpm
as well. Besides, Laravel requires php5-mcrypt
and Composer requires git
.
apt-get install nginx php5-fpm php5-cli php5-mcrypt git
This should take a while to install, but you are now ready to configure Nginx and PHP.
We will configure Nginx like Laravel is the only website you will run on it, basically accepting every HTTP request, no matter what the Host header contains. If you want more than one website on your VPS, please refer to this tutorial.
Make a dedicated folder for your Laravel website:
mkdir /var/www
mkdir /var/www/laravel
Open up the default virtual host file.
nano /etc/nginx/sites-available/default
The configuration should look like below:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Now save and exit!
We need to make a small change in the PHP configuration. Open the php.ini file:
nano /etc/php5/fpm/php.ini
Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.
cgi.fix_pathinfo=0
If this number is kept as 1, the PHP interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path — a much safer alternative. Now save it and exit nano.
We need to make another small change in the php5-fpm configuration. Open up www.conf:
nano /etc/php5/fpm/pool.d/www.conf
Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.
listen = /var/run/php5-fpm.sock
Again: save and exit!
Now make sure that both services are restarted.
service php5-fpm restart
service nginx restart
It is now time to install Composer, this process is quite straightforward. Let’s start off by downloading Composer:
curl -sS https://getcomposer.org/installer | php
Now install it globally:
mv composer.phar /usr/local/bin/composer
Heads Up: If you’re installing Laravel on DigitalOcean’s 512MB VPS, make sure you add a swapfile to Ubuntu to prevent it from running out of memory. You can quickly do this by issuing the following commands. This will only work during 1 session, so if you reboot during this tutorial, add the swapfile again.
dd if=/dev/zero of=/swapfile bs=1024 count=512k
mkswap /swapfile
swapon /swapfile
Finally, let’s install Laravel.
composer create-project laravel/laravel /var/www/laravel/ 4.1
Now browse to your cloud server’s IP. You can find using:
/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}'
It will now show you an error! What? The permissions still need to be set on the folders used for caching. Ah! Let’s do that now:
This is really quite a easy fix:
chgrp -R www-data /var/www/laravel
chmod -R 775 /var/www/laravel/app/storage
So that it, you can now enjoy Laravel running on a fast Nginx backend! If you want to use MySQL on your Laravel installation, it is extremely easy: just issue apt-get install mysql-server
and MySQL will be installed right away. For more information on using Laravel visit the website. Happy developing!
<div class=“author”>Submitted by: Wouter ten Bosch</div>
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
@Kamal dit that. But I seem to have solved that issue. I needed to enable mcrypt with
sudo ln -s /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available sudo php5enmod mcrypt
and then restart nginx and php with:
service php5-fpm restart service nginx restart
Also, this tutorial is missing the php json module installation step:
sudo apt-get install php5-json
This worked for me. Hope this helps others as well.
It is improper to
include fastcgi_params;
at the end of block, because it will overwrite all yourfastcgi_param
s, like you been settingfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
.I have these steps, and keep getting a 404. Please help…
Thank for the tutorial. I have followed the steps, but when I browse to my ip I still see the ubuntu default page. Any ideas?
Great tutorials around here, I successfully set up a droplet with a LEMP stack. I would love to install Lumen which is a PHP Micro-Framework by Laravel http://lumen.laravel.com/docs/installation Is there a plan to also add a tutorial for that? If not I just might just try and see, since I guess it would work pretty similar.
Followed everything. But instead of installing laravel through composer I cloned my working laravel app from bitbucket. Now the output is nothing, just blank page !. No errors, just blank page ! please help
I had 502 Bad Gateway Error… This worked for me http://stackoverflow.com/questions/23443398/nginx-error-connect-to-php5-fpm-sock-failed-13-permission-denied
ReWrite configuration in the article is incorrect…
Look at a issue
If you have trouble with the
composer create-project
step and get “Mcrypt PHP extension required.”, runphp5enmod mcrypt
and restart nginxInstead of
u should use
so this tutorial will not be outdated ;)