How to Install Laravel 5.6 PHP Framework with Nginx on Ubuntu 18.04

Laravel is a free and open source PHP framework that implements the MVC (Model-View-Controller) design pattern. Created by Taylor Otwell in 2011, it as an attempt to provide an advanced alternative to the CodeIgniter (CI) framework. In 2011, Laravel released version 1 and version 2, and the latest version 5.6 comes with more and improved features like Command-Line (CLI) support named 'artisan', support for different database systems, Route improvements etc.

In this tutorial, I will guide you step-by-step on how to install Laravel version 5.6 with Nginx as the web server, PHP-FPM 7.2, and MariaDB. Laravel 5.6 will run under LEMP stack with Ubuntu 18.04 as OS version.

There is a version of this tutorial for Laravel on CentOS 7 as well.

What we will do

  1. Update Ubuntu Repository
  2. Install Nginx
  3. Install PHP-FPM 7.2
  4. Install MariaDB
  5. Install PHP Composer
  6. Configure Nginx Virtual Host for Laravel
  7. Install Laravel
  8. Testing

Prerequisites

  • Ubuntu 18.04 server
  • Root privileges

Step 1 - Update Ubuntu

Before we start with the installation, we need to update the repository and then update all packages to the latest version. So to do that, first login to the Ubuntu server using ssh.

ssh root@ip

Now, update your Ubuntu repository and upgrade all packages on your system to the latest version.

sudo apt update
sudo apt upgrade

Reboot your server and then connect again using ssh.

Step 2 - Install Nginx

In this step, we will install Nginx 1.14 to the system. It's available on default ubuntu repository, you just need to install it with the following apt command.

sudo apt install nginx -y

After the installation is complete, start the nginx service and add it to start automatically at system boot using the systemctl command.

systemctl start nginx
systemctl enable nginx

Nginx is running on port 80, check related stats using the netstat command.

netstat -plntu

Enable and start Nginx web server

Or you can use the curl command as well.

curl -I localhost

Step 3 - Install PHP 7.2 and PHP-FPM

Nginx is installed, and now we need to install PHP-FPM version 7 on the system. We will install PHP 7.2 and some PHP extensions needed by Laravel.

Install PHP and PHP-FPM 7.2 with all extensions needed using the following command.

sudo apt install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip -y

Next, go to the PHP configuration directory and edit the php.ini file present in the fpm directory.

cd /etc/php/7.2/
vim fpm/php.ini

Uncomment the following CGI line, and change the value to 0.

cgi.fix_pathinfo=0

That's it. Save and exit.

Now we can start PHP-FPM and enable it to start automatically at system boot.

systemctl start php7.2-fpm
systemctl enable php7.2-fpm

By default on Ubuntu, PHP-FPM is running under the sock file. Check the PHP-FPM sock file with the netstat command in the following way:

netstat -pl | grep php7.2-fpm

Install and start PHP-FPM

PHP and PHP-FPM 7.2 has been installed on the Ubuntu 18.04 systems.

Step 4 - Install MariaDB

This is optional, but when your Laravel project is based on MySQL, you need to do this for your project. We will install MariaDB latest version 10.1 on to the system.

Install MariaDB from the repository using the following command.

sudo apt install mariadb-server mariadb-client -y

After the installation is complete, run MariaDB and enable it to launch at system boot.

systemcl start mysql
systemctl enable mysql

MariaDB started on port 3306, check it using the netstat command.

netstat -plntu

Start and check MariaDB

Next, configure the MariaDB root password with the 'mysql_secure_installation' command below.

mysql_secure_installation

Input your root password, remove anonymous users, disallow root login remotely etc.

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

MariaDB installation and configuration has been completed.

Step 5 - Install PHP Composer

The composer is a package manager for the PHP programming language. Created in 2011, it's inspired by Node.js 'npm' and Ruby's named 'bundler'. On ubuntu 18.04, composer is available in the repository, so we can install it using the apt command.

Install PHP Composer with the following command.

sudo apt install composer -y

After the installation has been completed, run 'composer' command and you will get the results as shown in the screenshot.

composer

Install PHP Composer

PHP Composer installed on Ubuntu 18.04.

Step 6 - Configure Nginx virtual host for Laravel

In this step, we will configure the nginx virtual host for Laravel. But before we do that, we must decide the directory for our Laravel project. In our case, we will use '/var/www/laravel' directory for the project. Just create it with mkdir.

mkdir -p /var/www/laravel

Next, go to the nginx configuration directory and create a new virtual host file 'laravel' under the 'sites-available' directory.

cd /etc/nginx/
vim sites-available/laravel

Paste the following configuration there.

server {
         listen 80;
         listen [::]:80 ipv6only=on;
 
         # Log files for Debugging
         access_log /var/log/nginx/laravel-access.log;
         error_log /var/log/nginx/laravel-error.log;
 
         # Webroot Directory for Laravel project
         root /var/www/laravel/public;
         index index.php index.html index.htm;
 
         # Your Domain Name
         server_name laravel.hakase-labs.co;
 
         location / {
                 try_files $uri $uri/ /index.php?$query_string;
         }
 
         # PHP-FPM Configuration Nginx
         location ~ \.php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
         }
 }

That's it. Save and exit.

Now activate the virtual host by creating a symlink of the 'laravel' file to the 'sites-enabled' directory.

Then test nginx configuration and make sure there is no error.

ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
nginx -t

Nginx virtual host for laravel has been created, now restart the nginx service.

systemctl restart nginx

Configure Nginx for Laravel

Step 7 - Install Laravel

Before we start with Laravel installation, make sure the unzip utility is installed on your system. If you do not have the tool, install it using the following command.

sudo apt install unzip -y

We've already defined a directory for Laravel : '/var/www/laravel' directory. Go to that directory.

cd /var/www/laravel

Install Laravel with the composer command. There are two ways to install Laravel: Installing via the Laravel Installer and Installing via Composer create project.

We will install latest Laravel version 5.6 via the composer create project.

Run the following composer command.

composer create-project laravel/laravel .

You need to wait for the Laravel installation.

Install Laravel

Laravel installation is complete.

Laravel installation complete

We need to change the ownership of the Laravel project directory to 'www-data' user, and change the permission of the storage directory to 755.

chown -R www-data:root /var/www/laravel
chmod 755 /var/www/laravel/storage

Laravel Installation has been completed without any error.

Step 8 - Testing

In the virtual host configuration file, we've already defined the domain name for Laravel 'laravel.hakase-labs.co'.

Open your web browser and visit the domain you installed Laravel on, mine here on the test server is http://laravel.hakase-labs.co/.

And you should see the Laravel home page.

Laravel successfully installed

Laravel Installation with Nginx, PHP-FPM 7.2 and MariaDB on Ubuntu 18.04 is successful.

References

Share this page:

6 Comment(s)