Support Online
Skip to main content

Installing Laravel with Nginx (LEMP) on Ubuntu

Installing Laravel with Nginx (LEMP) on Ubuntu

This guide explains how to prepare the Laravel application for production using the LEMP stack (Linux, Nginx, MySQL, PHP) on an Ubuntu server.
The goal is to establish a secure, performant and scalable Laravel infrastructure.

What Will You Learn in This Guide?

  • Installing the necessary PHP 8.2+ extensions for Laravel
  • Create a secure MySQL database and user
  • Configure Nginx according to Laravel routing logic
  • Implement SSL and basic performance optimizations

1. Installing Required PHP Extensions

Laravel needs some basic PHP extensions.

sudo apt update
sudo apt install php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-bcmath php8.2-curl php8.2-zip php8.2-gd
  • This command installs all the PHP components required for Laravel to run.

2. MySQL Database Configuration

  1. Create a separate database and restricted authorized user for the application.

CREATE DATABASE genixnode_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'genix_user'@'localhost' IDENTIFIED BY 'guclu_sifre_buraya';
GRANT ALL PRIVILEGES ON genixnode_db.* TO 'genix_user'@'localhost';
FLUSH PRIVILEGES;
  • This configuration allows local access only and is suitable for production.

3. Creating the Laravel Application

  1. Create a new Laravel project with Composer.

cd ~
composer create-project laravel/laravel travellist
sudo mv ~/travellist /var/www/travellist

4. File Permissions and Environment Settings

  1. Give Laravel the necessary permissions to write cache and logs.

sudo chown -R www-data:www-data /var/www/travellist
sudo chmod -R 775 /var/www/travellist/storage
sudo chmod -R 775 /var/www/travellist/bootstrap/cache
  1. Then update the database information in the .env file.

5. Nginx Configuration

  1. Create an Nginx server block for Laravel.

server {
listen 80;
server_name ornek.com;
root /var/www/travellist/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;
}
}
  • This configuration redirects all requests to Laravel's main input file.

Frequently Asked Questions (FAQ)

1. Why is Nginx preferred for Laravel? Nginx manages high concurrent connections more efficiently with low memory consumption.

2. What does 502 Bad Gateway error mean? It usually indicates that the PHP-FPM service is not running or the socket path does not match.

3. How to improve performance in production environment? Laravel configuration and route caching should be used.


php artisan config:cache
php artisan route:cache

Result

With this guide, you have set up a secure and optimized Laravel environment on Ubuntu. Thanks to Nginx performance and PHP-FPM architecture, your application is production ready.

You can try this configuration immediately on the GenixNode infrastructure for your professional projects.