Support Online
Skip to main content

Laravel Installation: LEMP Stack Configuration with Nginx on Ubuntu 22.04

In this guide, you will learn step by step how to install and configure the LEMP (Linux, Nginx, MySQL, PHP-FPM) stack for your Laravel application using Nginx and MySQL on Ubuntu 22.04 server. In the end, you will get a working Laravel demo application connecting with the database.

What Will You Learn in This Guide?

You will learn how to install Laravel on Ubuntu 22.04, install PHP modules, create a MySQL database, and serve your Laravel application with Nginx. This method provides an ideal environment for going live with modern PHP applications.

Prerequisites and Preparation

Before you start, make sure you have the following components installed on Ubuntu 22.04:

  • sudo is an authorized user
  • LEMP stack (Nginx, MySQL 8, PHP-FPM)
  • Composer (for Laravel dependencies)

1. Installing Required PHP Modules

Laravel needs some PHP modules. By installing these modules, we make Laravel work.

Installing PHP Modules

Update the package manager and install the required modules:

sudo apt update
sudo apt install php-mbstring php-xml php-bcmath php-curl

php-mbstring: Character encoding support

php-xml: XML processing support

php-bcmath: Precise math calculations

php-curl: cURL support for HTTP requests


2. Creating a Database and User for Laravel

Let's create the database that your Laravel application will use on MySQL.

MySQL Configuration

Log in to MySQL as root:


sudo mysql

Create a new database:


CREATE DATABASE genixnode_liste;
Yeni bir kullanıcı oluşturun ve yetki verin:

CREATE USER 'genix_user'@'%' IDENTIFIED WITH mysql_native_password BY 'GucluSifre123!';
GRANT ALL ON genixnode_liste.* TO 'genix_user'@'%';
FLUSH PRIVILEGES;

Create a places table to add sample data:


CREATE TABLE genixnode_liste.yerler (
id INT AUTO_INCREMENT,
isim VARCHAR(255),
ziyaret_edildi BOOLEAN,
PRIMARY KEY(id)
);
INSERT INTO genixnode_liste.yerler (isim, ziyaret_edildi)
VALUES ("Rize", false),
("Marmaris", true),
("Nevsehir", false),
("Kars", true);

The database and user are ready, now we can move on to configuring for Laravel.


3. Creating a New Laravel Application

We will create the Laravel project with Composer.

Creating the Laravel Project

Go to the home directory and create the Laravel application:


cd ~
composer create-project --prefer-dist laravel/laravel genixnode_app

To check that Laravel is installed correctly:


cd genixnode_app
php artisan

This command will run the Laravel command line tool and provide information that the application is installed correctly.


4. Configuring Laravel Application

Let's update Laravel's .env file with database information.

Editing the .env File

Open the .env file with the nano editor:


nano .env

Update the database information as follows:


APP_NAME=GenixNodeListesi
APP_ENV=development
APP_KEY=APPLICATION_UNIQUE_KEY_DONT_COPY
APP_DEBUG=true
APP_URL=http://tr1-node01.ornek.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=genixnode_liste
DB_USERNAME=genix_user
DB_PASSWORD=GucluSifre123!

Save the changes and close the file.


5. Setting Up Nginx Virtual Host

In order to run the application over the web, we must configure Nginx.

Moving Files and Permission Settings

Move the Laravel application file to /var/www directory:


sudo mv ~/genixnode_app /var/www/genixnode_app

Give write permissions to Laravel's storage and bootstrap/cache directories:


sudo chown -R www-data:www-data /var/www/genixnode_app/storage
sudo chown -R www-data:www-data /var/www/genixnode_app/bootstrap/cache

Nginx Configuration

Create a new virtual host file:


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

Add the following Nginx configuration:


server {
listen 80;
server_name tr1-node01.ornek.com;
root /var/www/genixnode_app/public;

index index.php index.htm index.html;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}

Enable Configuration

Create symbolic link to virtual host file:


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

Check for configuration errors:


sudo nginx -t

Reinstall Nginx:


sudo systemctl reload nginx

6. Customizing the Home Page to Pull Data from the Database

We will pull data from the database by customizing the Laravel route file (routes/web.php) and view file (travellist.blade.php).

Update Route File

Edit the main route file like this:


nano /var/www/genixnode_app/routes/web.php

Add the following code:


<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
$ziyaret_edildi = DB::select('select * from yerler where ziyaret_edildi = ?', [1]);
$gidilecek = DB::select('select * from yerler where ziyaret_edildi = ?', [0]);

return view('genixnode_liste', ['ziyaret_edildi' => $ziyaret_edildi, 'gidilecek' => $gidilecek ] );
});

Creating the View File

Create a new Blade template:


nano /var/www/genixnode_app/resources/views/genixnode_liste.blade.php

Add the following HTML and Blade code:


<html>
<head>
<title>GenixNode Seyahat Listesi</title>
</head>

<body>
<h1>Gezilecek Yerler Listem</h1>
<h2>Ziyaret Etmek İstediklerim</h2>
<ul>
@foreach ($gidilecek as $yeni_yer)
<li>{{ $yeni_yer->isim }}</li>
@endforeach
</ul>

<h2>Daha Önce Gittiğim Yerler</h2>
<ul>
@foreach ($ziyaret_edildi as $yer)
<li>{{ $yer->isim }}</li>
@endforeach
</ul>
</body>
</html>

Frequently Asked Questions (FAQ)

  1. Why should I use Nginx?

Nginx is a high-performance and lightweight web server. It quickly runs modern PHP applications like Laravel.

  1. What settings should I change in the .env file?

You need to update information such as database username, password and database name in the DB_ fields in the .env file.

  1. Why is the public directory used in Nginx configuration?

The root directory of your Laravel application often contains hidden files. It is important for security to ensure that the web server only operates through the public directory.


Result

By building your Laravel application on the LEMP stack, you get a powerful and portable development environment. Now you can see that your application works smoothly with the database and can be served quickly with Nginx.