How to install Laravel with LEMP stack on Ubuntu

Harish Kumar · · 3232 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:

Laravel Pint & VS Code: Automate Your Code Formatting

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects. (...)
Harish Kumar

Laravel Clockwork: A Deep Dive into Debugging, Profiling Skills and Best Practices

In the world of web development, building complex applications often comes with the challenge of identifying and resolving performance bottlenecks. This is where a reliable debugging (...)
Harish Kumar

Effortless PDF Generation in Laravel: A Guide to Using Spatie/Browsershot Package

Looking to generate or export PDF files using Laravel? Look no further! In this article, we'll guide you through using the Laravel spatie/browsershot package to seamlessly convert (...)
Harish Kumar

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