Setting up Laravel & Nginx on Server

Laravel is an open-source PHP framework that provides a set of tools and resources to build modern PHP applications. With a complete ecosystem leveraging its built-in features, Laravel’s popularity has grown rapidly in the past few years, with many developers adopting it as their framework of choice for a streamlined development process.

In this guide, you’ll install and configure a new Laravel application on an Ubuntu 18.04 server, using Composer to download and manage the framework dependencies. When you’re finished, you’ll have a functional Laravel demo application pulling content from a MySQL database.

Before we move forward I assume that you have a server with ssh access and have a sudo user to install the required stuff.

We will go through following steps in this read, so hold on, read, copy and paste ;)

1: Install the LEMP stack.
2: Install Composer
3: Installing Required PHP modules
4: Creating a New Laravel Application
5: Setting Up Nginx
6: Happiness

1 : Install the LEMP stack:


The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx (pronounced like “Engine-X”) web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.

We will start by installing the NGINX server

sudo sudo apt update
sudo apt install nginx

you may be having ufw firewall running so its a good approach to allow connections to Nginx. Nginx registers itself with ufw upon installation, so the procedure is rather straightforward.
We will also go through the process of configuring SSL for your server in my other read.

Enable this by typing:

sudo ufw allow ‘Nginx HTTP’

You can verify the change by running:

sudo ufw status

This command’s output will show that HTTP traffic is allowed:

Type the address that you receive in your web browser and it will take you to Nginx’s default landing page:



Install MySQL by typing

sudo apt install mysql-server

The MySQL database software is now installed, but its configuration is not yet complete.

To secure the installation, MySQL comes with a script that will ask whether we want to modify some insecure defaults. Initiate the script by typing:

sudo mysql_secure_installation

this will ask you you if you want to configure VALIDATE PASSWORD PLUGIN
press ‘Y’ and move on and put a password with your desire security.

Once done you can access MYSQL by typing:

sudo mysql

Now configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:


after this you can type exit to exit from MYSQL shell.

Now you can access you MYSQL shell by typing

mysql -u root -p

INSTALLING PHP and Configuring Nginx to Use the PHP Processor:

You now have Nginx installed to serve your pages and MySQL installed to store and manage your data. However, you still don’t have anything that can generate dynamic content. This is where PHP comes into play.

Install the php-fpm module along with an additional helper package, php-mysql, which will allow PHP to communicate with your database backend. The installation will pull in the necessary PHP core files. Do this by typing:

sudo apt install php-fpm php-mysql

You now have all of the required LEMP stack components installed, but you still need to make a few configuration changes in order to tell Nginx to use the PHP processor for dynamic content, we will do this but after installing Laravel Application.

But first we need to install COMPOSER:

let’s install the dependencies. We’ll need curl in order to download Composer and php-cli for installing and running it. The php-mbstring package is necessary to provide functions for a library we’ll be using. git is used by Composer for downloading project dependencies, and unzip for extracting zipped packages. Everything can be installed with the following command:

sudo apt install curl php-cli php-mbstring git unzip

now we can move on to install composer.

2: Install Composer

cd ~
curl -sS -o composer-setup.php

Next, verify that the installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. Copy the hash from that page and store it as a shell variable:


Now execute the following PHP script to verify that the installation script is safe to run:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You’ll see the following output.

Now you can continue to install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You’ll see the following output:

To test your installation, run:


And you’ll see this output displaying Composer’s version and arguments.

/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 1.6.5 2018-05-04 11:44:59

command [options] [arguments]

-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
. . .

Now let’s look at using Composer to install LARAVEL.

3: Installing Required PHP modules

Before you can install Laravel, you need to install a few PHP modules that are required by the framework. We’ll use apt to install the php-mbstring, php-xml and php-bcmath PHP modules.

sudo apt install php-mbstring php-xml php-bcmath

Now move to the directory where you want to create you project:

4: Creating a New Laravel Application

cd /var/www/

The following command will create a new test_laravel directory containing a barebones Laravel application based on default settings:

composer create-project --prefer-dist laravel/laravel test_laravel

You will see output similar to this:

Installing laravel/laravel (v5.8.17)
- Installing laravel/laravel (v5.8.17): Downloading (100%)
Created project in travel_list
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 80 installs, 0 updates, 0 removals
- Installing symfony/polyfill-ctype (v1.11.0): Downloading (100%)
- Installing phpoption/phpoption (1.5.0): Downloading (100%)
- Installing vlucas/phpdotenv (v3.4.0): Downloading (100%)
- Installing symfony/css-selector (v4.3.2): Downloading (100%)

after installation you can move into the project directory by typing:

cd test_laravel

and continue configuring App.

cp .env.example .env
nano .env

now you can add the DB connection, cache driver details and whatever you want to add to the .env file.

Now we will continue setting up NGINX from where we left:

5: Setting Up Nginx

We need to give the web server user write access to the storage and cache folders, where Laravel stores application-generated files:

sudo chown -R www-data.www-data /var/www/travel_list/storage
sudo chown -R www-data.www-data /var/www/travel_list/bootstrap/cache

The application files are now in order, but we still need to configure Nginx to serve the content. To do this, we’ll create a new virtual host configuration file at /etc/nginx/sites-available:

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

you can paste the following lines to your into you own configuration file

server {
listen 80;
server_name server_domain_or_IP;
root /var/www/larvel_test/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

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

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;

location ~ /\.(?!well-known).* {
deny all;

keep in mind that server_name server_domain_or_IP; should be replace accordingly, if you have some domain pointing towards your server then add you domain name other wise you server IP address.

To activate the new virtual host configuration file, create a symbolic link to laravel_test in sites-enabled:

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

To confirm that the configuration doesn’t contain any syntax errors, you can use:

sudo nginx -t

You should see output like this:

To apply the changes, reload Nginx with:

sudo systemctl reload nginx

Now go to your browser and access the application using the server’s domain name or IP address, as defined by the server_name directive in your configuration file:


YAY, you are all done, congratulations if you faced no errors while going through all this :)

6: Happiness

Your happiness will be seeing the following page :

and my Happiness is knowing that you have seen it, and how I am going to know this ???
yeah you got it, Clap buddy ;)

I will be writing another story on how to enable and configure NGINX to use SSL certificate.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store