Installing Laravel on Ubuntu for Nginx

We have already talked about installing Laravel for Apache based Ubuntu servers in our last tutorial. This tutorial will show you how to install Laravel PHP framework on a Nginx server installation.

Pre-Requisities

As always, you should ensure that the server is well updated before proceeding.

sudo apt-get update
sudo apt-get upgrade

This tutorial is based on the assumption that you have a Ubuntu based server setup already. Also make sure you have Git installed. If not, then you can follow our guide to do so.

Installing Nginx

Installing Nginx server is simple and straight forward. So let's do that.

sudo apt-get install nginx

Installing PHP-FPM

PHP doesn't work with Nginx the same way as it does with Apache. For Nginx, you need the FastCGI implementation of PHP called PHP-FPM (PHP-FastCGI Process Manager). Let's Install PHP then. We also need the Mcrypt extension of PHP for Laravel to work.

sudo apt-get install php5-fpm php5-cli php5-mcrypt

Configuring Nginx for Laravel

Before proceeding, let's do some basic configuration of the Nginx server that we have installed. First we need to create the directory where Laravel will be installed.

sudo mkdir -p /var/www/html/laravel

That being done, now we have to tell Nginx that this is where our site will work from. For that we need to create a virtualhost entry in Nginx. Nginx stores default configuration in a file named default at /etc/nginx/sites-available/default. Let's copy it and create our own version of the file.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/laravel

Let's edit the configuration file

sudo nano /etc/nginx/sites-available/laravel

You will see something like

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }
}

I have removed the comments for readability purposes. You will need to change the root location which would be /var/www/html/laravel/public in our case. You also need to change the server_name variable either to the domain name you would be using or the IP address of your server.

Edit the file so that it looks like the following

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html/laravel/public;
    index index.php index.html index.htm;

    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file after editing. Now we need to enable this configuration file. So far only the default profile is active. To do so, we need to create a symbolic link to laravel file in the directory /etc/nginx/sites-enabled. We would also need to disable the default profile so that it doesn't interfere with our laravel install.

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Our configuration of the Nginx server is complete. Next step is to configure the PHP-FPM.

Configure PHP-FPM

This step is simple and requires us to change one variable in PHP's configuration file. Time to open php.ini

sudo nano /etc/php5/fpm/php.ini

Find the following text in it

;cgi.fix_pathinfo=1

Uncomment the variable by removing the ; from the front and change its value to 1 so that it looks like

cgi.fix_pathinfo=0

This tells PHP to not to execute a similar named script if the original one is not found which is important from security point of view.

Lastly we also need to enable the mcrypt extension which we installed earlier.

sudo php5enmod mcrypt

That's it. Now just restart the PHP-FPM service to load the changes.

sudo service restart php5-fpm

Installing Composer

Composer is a dependency management tool which allows us to install various PHP frameworks and libraries through a single command. It takes care of all the other packages and dependencies which you would have had to install separately to install a framework if you go down the manual route. It can save lots of times and hassles. We would be installing Laravel using Composer.

Download Composer. Make sure you are in your home directory first.

curl -sS https://getcomposer.org/installer | php

This downloads composer.phar file in your home directory. Now if you wish, you can run it from here by using something like php composer.phar [command] but since we would prefer to run composer globally, we need to shift it to a more appropriate location so that we can run it easily.

sudo mv composer.phar /usr/local/bin/composer

Now you can run it directly by just calling composer from anywhere.

Installing Laravel

Now that we have installed composer, we need to install Laravel. Install it by the following command.

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

This downloads and installs the latest version of Laravel (5.2 currently) to your server.

Before we finish the tutorial, we need to give proper permissions to some directories.

sudo chgrp -R www-data /var/www/html/laravel
sudo chmod -R 775 /var/www/html/laravel/storage

Conclusion

That's it. Your Laravel installation is complete. Now launch http://servername or http://serverip in your browser. You should see the following screen, if successful.

Share this page:

3 Comment(s)

Add comment

Comments

From: Ahmed

Thanks!!!!

From: David

Very good walkthrough, thank you!  One minor error:

sudo service restart php5-fpm   should be:   sudo service php5-fpm restart

Cheers!

From: StrikS

Thank you very mach. I spend 3 days for install laravel on CentOS. Then try install it on Debian with you tutorial and get working framework. <3