Menu

How to install Laravel 5.2 in Ubuntu with Nginx

4 years ago 2

Laravel Framework is one of the best frameworks to build any kind of web app with PHP.

It has a great community, and a really good documentation website laravel.com and also some of the best video tutorials on sites like laracasts.com

Some of the key point feature of Laravel :

  • Restful routing.
  • Inherent Database Version control
  • Blade Templating Engine
  • Composer – Makes adding third-party packages easy.
  • Large Community – thousands of programming geeks and application developers
  • Intelligently designed to offer incredible flexibility from small sites to giant enterprise applications.

The best thing about Laravel is that it is a high quality framework, so learning how to use it you become a better programmer in PHP in general.

I recommend you to give this a try if you want to learn PHP the easy way with this super powerful and easy to use framework.

Step 1

Logging in to your server

ssh [email protected]

Make sure your OS is up to date.

sudo apt-get update
sudo apt-get upgrade

Now we will install Nginx

sudo apt-get install nginx
sudo service nginx start

Open the file /etc/nginx/sites-available/default in any editor:

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

Edit this file and add the following settings.

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;
    }
}

Save and exit the file.

Test the nginx configuration for any syntax errors using command:

sudo nginx -t

Sample output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Install PHP with following command:

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

Open php.ini file in any editor:

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

Find the line ‘cgi.fix_pathinfo=1′, uncomment it and change the value 1 to 0.

cgi.fix_pathinfo=0

Now restart php-fpm service.

sudo service php5-fpm restart

The last thing we need to do is enable the MCrypt extension, which Laravel depends on. We can accomplish this by using the php5enmod command, which allows us to easily enable PHP modules:

sudo php5enmod mcrypt

Now, we can restart the php5-fpm service in order to implement the changes that we’ve made:

sudo service php5-fpm restart

Restart the nginx service.

sudo service nginx restart
Step 2

Installing Laravel

Now we will create the folders where our Laravel installation will reside in.

sudo mkdir -p /var/www/laravel

Now we will download and run the installer script from the Composer project.

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

This will create a file called composer.phar in your home directory.

We will now install it in a globally accessible location. Also, we want to rename the name to composer. We can do this with one command.

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

Now theat we have installed Composer , we can use it to install Laravel.

We will be installing Laravel into the /var/www/laravel directory. To install the latest version of Laravel, you can type:

sudo composer create-project laravel/laravel /var/www/laravel 5.2

Now, the files are all installed within our /var/www/laravel directory, but they don’t have the correct permissions. The webserver needs partial ownership and permissions for laravel to work.

We can give ownership of our Laravel directory to the webserver group by typing:

sudo chown -R :www-data /var/www/laravel

Next, we can change the permissions of the /var/www/laravel/app/storage directory to allow the webserver group write permissions. This is necessary for Laravel to function correctly:

sudo chmod -R 775 /var/www/laravel/app/storage

That’s it, now You have Laravel installed in your server.You can access it by going to your server IP or Domain.

http://www.example.org

If everything works you should see something like this.

🙂 Happy Coding