How to Deploy a Laravel Application on LEMP (Ubuntu and PHP 7.2)

Written on Friday, August 31, 2018 02:28:47 PM
Written by Greg Potts

How to Deploy a Laravel Application on LEMP

with PHP 7.2, NGINX, and MySQL

Laravel is one of the most popular open-source web application frameworks written in PHP.


  • An Ubuntu 16+ server is set up.
  • The LEMP stack is installed with Linux, NGINX, MySQL, and PHP.
  • A domain name set up and pointing to the server.

Step 1 - Install PHP, Composer, and other dependencies

PHP 7.2 is the latest stable version of PHP and has many new features, improvements, and bugfixes. You must use a third-party repository to install the latest version of PHP. We'll use the repository by Ondrej Sury. Follow the following steps to do that.

$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.2
$ apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

Install Composer and Unzip:

$ sudo apt-get install composer unzip

To set PHP7.2 as default, run:

$ update-alternatives --set php /usr/bin/php7.2

To check the PHP version, run:

$ php -v

Step 2 - Configure MySQL

MySQL must be configured to have a database that can be used in the Laravel application. It is also a bad security practice to use the root database user within a website because it has unlimited privileges on the database server. We will create a dedicated database user for the Laravel application to use, as well as a new database that the Laravel user will be allowed to access.

Log into MySQL root and run the following to create a new database, laravel,

$ mysql -u root -p
mysql> CREATE DATABASE laravel DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Now we will create a user that can access this database.

mysql> GRANT ALL ON laravel.* TO 'laraveluser'@'localhost' IDENTIFIED BY 'password';
mysql> EXIT;

Step 3 - Setting Up the Demo Application

The demo quickstart application, is distributed by Laravel on GitHub, and will give us an easy way to start up the process. Clone it to the web root.

$ sudo mkdir /var/www/html/quickstart
$ cd /var/www/html/quickstart
$ git clone .

Install the project dependencies via composer.

$ composer install

Step 4 - Configuring the Application Environment

Edit the environment file.

$ sudo vim /var/www/html/quickstart/.env

Make the following changes to the .env file.

DB_PASSWORD=password ... 

Step 5 - NGINX Configuration

In order for the web server to write to the storage and bootstrap/cache folders, we need to change some of the permissions. Run the following commands to do that:

$ sudo chgrp -R www-data storage bootstrap/cache
$ sudo chmod -R ug+rwx storage bootstrap/cache

We now need to create a configuration for NGINX to make it correctly work with the Laravel installation. Copy the default server block config to your custom site's name.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/
$ sudo nano /etc/nginx/sites-enabled/

The modified server block NGINX configuration will look like this:

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/quickstart/public;
        index index.php index.html index.htm index.nginx-debian.html;
        location / {
                try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;

        location ~ /\.ht {
                deny all;

We must now create a symlink between the sites-available server block configuration and a file int he sites-enabled directory.

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Step 6 - Test NGINX Configuration and Reload Configuration

$ sudo nginx -t
$ sudo systemctl restart nginx


You have now successfully deployed a demo application that was shipped with Laravel under the LEMP stack.