How to install Laravel with LEMP stack on Ubuntu

Harish Kumar · · 2917 Views

Laravel is one of the most popular open-source PHP framework, and Laravel allows you to build scalable and flexible web applications. Because of its outstanding features, for example, routing, authentication, sessions, caching, and unit testing, Laravel is a framework of choice for many PHP developers.

In this post, you will learn how to install Laravel with LEMP stack on an Ubuntu server.


  1. Before continuing, make sure you are logged in as a user with sudo privileges.

  2. Install a LEMP stack. If you have not yet installed, then you can follow the guide on this post: How to Install Nginx, MySQL, and PHP on Ubuntu.

Step 1 - Installing Required PHP modules

Before you start installing Laravel, you have to install a couple of PHP modules that are needed by the framework.

Run the following command on terminal to install all required PHP modules:

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

Step 2 - Installing Composer

Composer is a dependency manager for PHP and we will utilize it to download the Laravel core and install all important Laravel packages.

To install composer globally, download the Composer installer with curl and then move the composer file to the /usr/local/bin directory using below command:

curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer

Now composer is installed, verify it by checking its version:

composer --version

Step 3 - Creating a Database for the Application

To connect with the database from the Laravel application, we'll make a MySQL user, and grant this user full privileges over the database we want to connect from the Laravel app.

To begin, sign in to the MySQL console as the root user using the following command:

sudo mysql

Create Database:

mysql> CREATE DATABASE database_name;

Create Database User

At the time of this writing, the native MySQL PHP library mysqlnd doesn't support caching_sha2_authentication, which is the default authentication strategy for MySQL 8. We need to set up our database user with the mysql_native_password authentication strategy so as to be able to connect to the MySQL database from PHP.

So, use the following command to make a new user with mysql_native_password authentication method:

mysql> CREATE USER 'database_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

Now we need to give this user permission over the database:

mysql> GRANT ALL ON database_name.* TO 'database_user'@'%';


mysql> exit;

Step 4 - Creating a New Laravel Application

Run the Composer create-project command to install Laravel in the my_app directory:

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

When the installation is finished, access the application’s directory and run Laravel’s artisan command to confim it. 

cd ~/my_app
php artisan

This will show Laravel version and some its commands.

Configuring Laravel

Now, we will edit the .env file to add database credentials and other application configuration options.

Open the .env file in your editor of choice. Here we’ll use vi:

vi .env

The following .env file is example of our application for development:




Step 5 - Create Nginx Virtual Host for Laravel App

First, we need to give the webserver user write access to the storage and cache folders. Use the following command for that:

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

Next, create a virtual host configuration file at /etc/nginx/sites-available :

sudo vi /etc/nginx/sites-available/

The following snippet contains the recommended configuration for Laravel applications on Nginx. Add this to your virtual host file, and don't forget to replace your domain name and project path in this snippet.

server {
    listen 80;
    server_name server_domain_or_IP;
    root /var/www/travel_list/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.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;

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

To enable this new virtual host configuration, create a symbolic link of this in /etc/nginx/sites-enabled:

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

Confirm that the configuration doesn’t contain any syntax errors:

sudo nginx -t

If any errors are reported, go back and recheck your configuration file before continuing.

To apply the changes, reload Nginx with:

sudo systemctl reload nginx

This concludes the installation and configuration of your Laravel with LEMP stack on Ubuntu server. Now in your browser, you can access the application using the server’s domain name or IP address as defined by the server_name in your virtual host.


Please login or create new account to add your comment.

You may also like:

Part #3: Rule objects based custom validation in Laravel

Laravel comes with multiple ways to add custom validation rules to validate form request inputs. I have already explained some of the ways in the following article links:
Harish Kumar

Part #2: How to use Laravel's Validator::extend method for custom validation

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation (...)
Harish Kumar

Part #1: Closure-based Custom Laravel Validation

While I was working with Laravel, validation using closure came to my mind, and I know it will be helpful to you. This tutorial assists you with all what is the difference between (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

Mobile App Development Process

With businesses adopting a mobile-first approach and the growing number of mobile apps, successful mobile app development seems like a quest. But it’s the process that determines (...)
Narola Infotech

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar