Tutorial

How To Install Laravel with Nginx on an Ubuntu 12.04 LTS VPS

Published on October 10, 2013
Default avatar

By Wouter ten Bosch

How To Install Laravel with Nginx on an Ubuntu 12.04 LTS VPS

Status: Deprecated

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.

Introduction


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.

Preparation

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.

Installation


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.

Configuring Nginx

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!

Configuring PHP

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!

(Re)Starting PHP and Nginx

Now make sure that both services are restarted.

service php5-fpm restart
service nginx restart

Installing Composer

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

Installing Laravel

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

Testing


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:

Fixing permissions

This is really quite a easy fix:

chgrp -R www-data /var/www/laravel
chmod -R 775 /var/www/laravel/app/storage

Wrap Up


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.

Learn more about us


About the authors
Default avatar
Wouter ten Bosch

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


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 your fastcgi_params, like you been setting fastcgi_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

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.”, run php5enmod mcrypt and restart nginx

Instead of

composer create-project laravel/laravel /var/www/laravel/ 4.1 

u should use

composer create-project laravel/laravel /var/www/laravel/ --prefer-dist

so this tutorial will not be outdated ;)

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel