Logo

How to install Laravel 9 with LEMP stack on Ubuntu Server 22.04

Jan 31, 2023

Here is a step-by-step guide to install Laravel with a LEMP stack on Ubuntu 22.04:

Update your Ubuntu system

sudo apt update
sudo apt upgrade

Install Nginx

sudo apt install nginx

  • Check the status of Nginx
sudo systemctl status nginx

  • Start the Nginx service:
sudo systemctl start nginx

  • Enable the Nginx service to start automatically on boot:
sudo systemctl enable nginx

Install MariaDB

sudo apt-get install mariadb-server

Secure MariaDB installation

sudo mysql_secure_installation

Log in to the MariaDB console

sudo mysql

Create a database for Laravel:

CREATE DATABASE laravel;

Create a database user and grant privileges:

CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel.* TO 'laraveluser'@'localhost';
FLUSH PRIVILEGES;

Exit the MariaDB console:

exit;

Install PHP and required extensions

Add the Ondrej sury PPA repository:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Install PHP and required extensions

sudo apt-get install php8.2 php8.2-fpm php8.2-cli php8.2-curl php8.2-zip php8.2-mysql php8.2-mbstring php8.2-xml php8.2-bcmath

Install composer

Download the Composer installer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Run the installer:

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

Remove the installer:

php -r "unlink('composer-setup.php');"

Verify the Composer installation:

composer --version

Create Laravel project

cd /var/www
composer create-project --prefer-dist laravel/laravel myproject

Set proper permissions for the Laravel project:

sudo chown -R www-data:www-data /var/www/myproject
sudo chmod -R 755 /var/www/myproject/storage

Configure Nginx for Laravel

Create a new Nginx server block configuration file for the Laravel project

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

Add the following content to the file:

server {
  listen 80;

  server_name laravel.example.com;

  root /var/www/myproject/public;

  index index.php;


  location / {

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

  }


  location ~ \.php$ {

    include snippets/fastcgi-php.conf;

    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;

  }


  location ~ /\.ht {

    deny all;

  }

}

Save and close the file.

Create a symbolic link from the new server block configuration file to the sites-enabled directory:

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

Test the Nginx configuration:

sudo nginx -t

If the test is successful, restart the Nginx service:

sudo systemctl restart nginx

Update the Laravel .env file to use a MySQL database

Open the Laravel .env file and Update the database connection information:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=password

Migrate the database tables:

php artisan migrate

Test your Laravel installation in a web browser