How to install Laravel with LEMP stack on Ubuntu

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

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

How To Install NVM (Node Version Manager) on Ubuntu System?

This tutorial will assist you with installing NVM on the Ubuntu machine. Additionally, allow you to install different node versions and other useful examples.
Harish Kumar

Install phpMyAdmin Manually with Nginx server on Ubuntu

In this guide, I will show you how to install and configure phpMyAdmin with Nginx, MySQL, and PHP8.0 (LEMP) on an Ubuntu system. phpMyAdmin is a free and open-source database (...)
Harish Kumar

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

Laravel Sanctum API Token Authentication Tutorial with example

Laravel Sanctum is a popular package for API Token Authentication. There are many other packages available to authenticate the APIs request in Laravel. For example, We are already (...)
Harish Kumar

Create SPA authentication Using Laravel Sanctum and Vue.js

In this guide, we will focus on SPA authentication in a simple Vue.js app using Laravel Sanctum. Laravel Sanctum provides a featherweight authentication system for SPAs (single (...)
Harish Kumar