Contents

How to Install Laravel with an Nginx Web Server on Ubuntu 14.04

01 Jan 2018 0 106  0
How to Install Laravel with an Nginx Web Server on Ubuntu 14.04
How to Install Laravel with an Nginx Web Server on Ubuntu 14.04

About Laravel

Laravel is a modern, open source PHP framework used by web developers which allows a simple and elegant way for developers to quickly get a fully functional web application running.

In this tutorial, we will teach you how to install Laravel on Ubuntu 14.04.

We used Nginx as our web server and the most recent version of Laravel when writing this guide, the most recent version was 4.2.

Install the Backend Components

To begin with Laravel you must install the stack which supports it; you may do this using Ubuntu’s default repositories.

Now, you have to update your local package index to ensure that you’ve got a fresh list of the available packages. Then we may install the required components as shown here.

sudo apt-get update

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

This should install Nginx as your web server using the PHP tools required to run the Laravel code.

Install git as the composer tool, the dependency manager for PHP to use to install Laravel and pull-down packages.

Modify the PHP Configuration

Since you’ve now got these components installed, you may begin configuring them. Start with PHP, which should be quite straight forward.

First, open the main PHP configuration file for the ‘PHP-fpm’ processor which Nginx uses. Open this using sudo privileges in your text editor.

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

You only have to edit one value in this file; Look for the ‘cgi.fix_pathinfo’ parameter. This should be commented out and set to ‘1’. You have to uncomment this and change it to ‘0’.

cgi.fix_pathinfo=0

This will instruct PHP to not attempt to run a similar named script in case that the file requested is not found. This is quite important since this type of behavior can grant an attacker the ability to craft a specially designed request to attempt to trick the PHP into running a code which it should not.

After you have finished, save and exit the file.

The last piece of PHP administration which you have to do is explicitly enable the MCrypt extension which Laravel relies on. We may do this with the ‘php5enmod’ command, which will allow us to enable optional modules.

sudo php5enmod mcrypt

Then, restart the ‘php5-fpm’ service in order for these changes to take effect.

sudo service php5-fpm restart

After that, your PHP will be completely configured, and you will be ready to move on.

Configure Nginx and the Web Root

Next, you are going to address the web server which involves two distinct steps.

To start, configure the document root and directory structure that you will be using in order to hold the Laravel files. You will be placing our files in a directory named ‘/var/www/laravel’.

For now, only the top-level of this path has been made (/var). You need to make the entire path in one step by passing the ‘–ip’ flag to your ‘mkdir’ command; this instructs the utility to create any required parent path elements needed to build the given path.

sudo mkdir -p /var/www/laravel

Since you have the location set aside for the Laravel components, proceed to editing the Nginx server blocks.

Open the default server block configuration file using sudo privileges.

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

Once installed, this file will have a couple of explanatory comments, however, the basic structure should look like the following.

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;

        }

}

This will provide a good basis for the changes you are going to be doing.

Now, you have to change the location of the document root. Laravel should be installed in the ‘/var/www/laravel’ directory that was made.

However, the base files that were used to drive the app are kept in a sub directory inside this called ‘public’. That is where you will be setting your document root.

Another thing you have to do is tell Nginx to serve any ‘index.php’ files before searching for their HTML counterparts after requesting a directory location.

server {

    listen 80 default_server;

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



    root /var/www/laravel/public;

    index index.php index.html index.htm;



    server_name localhost;



    location / {

            try_files $uri $uri/ =404;

    }

}

Then, you must set the ‘server_name’ directive to reference the actual domain name of your server. In case you don’t hold a domain name, you may use your server’s IP address.

You must now edit the way that Nginx will handle requests. We can do this through the ‘try_files’ directive. You would like it to attempt serving the request as a file first; if it’s not able to find a file of the correct name, it will attempt to serve the default index file for a directory which matches the request. If this fails, it will pass the request to the ‘index.php’ file as a query parameter.

The changes shown above can be implemented as shown below.

server {

        listen 80 default_server;

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



        root /var/www/laravel/public;

        index index.php index.html index.htm;



        server_name server_domain_or_IP;



        location / {

                try_files $uri $uri/ /index.php?$query_string;

        }

}

Now, you must create a block which handles the execution of any PHP files. This should apply to any files which end in ‘.php’. It will also attempt the file itself and afterwards pass it as a parameter to the index.php file.

Next, you must set the ‘fastcgi_*’ directives so that the path of requests are properly split for execution, ensuring that Nginx uses the socket which ‘php5-fpm’ is using for communication and ‘index.php’ file is used as the index for those operations.

Finally, set the ‘SCRIPT_FILENAME’ parameter so that PHP is able to locate the requested files correctly. When you are done, the completed file will look as shown below.

server {

    listen 80 default_server;

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



    root /var/www/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;

    }

}

After you are finished, save and exit the file.

Since you have edited the default server block file, which is already enabled, restart Nginx for your configuration changes to take effect.

sudo service nginx restart

Creating Swap File (Optional)

Before you start installing Composer and Laravel, it may be a good idea to enable some swap on your server for your build to complete correctly. This is usually only needed if you happen to be operating on a server without a lot of memory, for example: a 512mb VPS.

Swap space should allow the operating system to temporarily move data from memory onto the disk after the number of information in memory surpasses the physical memory space available. This, in turn, should prevent your applications or system from crashing with an out of memory (OOM) exception after doing memory intensive tasks.

You could also very easily set up some swap space to allow your operating system to shuffle some of this to the disk whenever needed.

0 Comments
Add a comment

Leave a Reply