How to install Laravel with LEMP stack on Ubuntu

Harish Kumar · · 3582 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.

Prerequisites

  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 https://getcomposer.org/installer | 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'@'%';

Now FLUSH PRIVILEGES and exit.

mysql> FLUSH PRIVILEGES;
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:

APP_NAME=Laravel
APP_ENV=development
APP_KEY=APPLICATION_UNIQUE_KEY_DONT_COPY
APP_DEBUG=true
APP_URL=http://domain_or_IP

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user
DB_PASSWORD=password

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/my_app.com

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/my_app.com /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.

0

Please login or create new account to add your comment.

0 comments
You may also like:

PHP OPCache: The Secret Weapon for Laravel Performance Boost

OPCache, a built-in PHP opcode cache, is a powerful tool for significantly improving Laravel application speed. This guide will demonstrate how to effectively utilize OPCache to (...)
Harish Kumar

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar

PHP Generators: Efficient Data Handling and Iteration Techniques

PHP Generators offer a powerful and memory-efficient way to handle large datasets and complex iteration scenarios in your applications. They provide a more elegant solution compared (...)
Harish Kumar

Compress and Download Files in Laravel Using ZipArchive with Examples

In web development, file compression is essential for optimizing data transfer and storage. Laravel provides tools for creating and downloading compressed files. This guide explores (...)
Harish Kumar

Implementing Multi-Authentication with Guards in Laravel

This guide provides a detailed explanation of how to implement multi-authentication using guards in a Laravel application. This is useful for applications that need to support (...)
Harish Kumar