How to install the Laravel PHP Framework with Nginx on Ubuntu 24.04

This article guides you through installing the Laravel PHP Framework with Nginx support on Ubuntu 24.04, ensuring a robust and efficient environment for web application development. Instructions cover Nginx and MariaDB installation, creating a Laravel database, setting up PHP-FPM, installing Laravel via Composer, and configuring Nginx for Laravel. Additional guidance on SSL setup is provided.

This article explains installing the Laravel PHP Framework with Nginx support on Ubuntu 24.04.

Installing the Laravel PHP framework with Nginx on Ubuntu provides a robust and efficient environment for securely developing and deploying web applications. Ubuntu, a popular and well-supported Linux distribution, ensures you have all the necessary tools and packages for web development.

Combined with Laravel and Nginx, you’re creating a powerful stack for building and hosting modern web applications backed by a strong and reliable community.

The steps below walk you through installing the Laravel PHP framework with Nginx on Ubuntu 24.04.

Install Nginx HTTP server on Ubuntu

Laravel requires a web server. This post will install and use the Nginx web server to run Laravel.

To do that, open the Ubuntu terminal and run the commands below to install the Nginx web server.

sudo apt update
sudo apt install nginx

Once Nginx is installed, the commands below can start, stop, and enable the Nginx web server to start automatically when your server boots up.

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl enable nginx

You can test the Nginx web server by opening your web browser and browsing to the server’s localhost or IP address.

http://localhost

When you see the “Welcome to nginx!” page, it means the Nginx HTTP server is successfully installed.

Additional help on installing Nginx on Ubuntu is in the link below.

How to install Nginx on Ubuntu

Install the MariaDB database server on Ubuntu

The next component required to run Laravel is a database server. This post will install and use the MariaDB database server.

To install and use the MariaDB database server, use the instructions below.

Open the Ubuntu terminal and run the commands below to install the MariaDB database server.

sudo apt update
sudo apt install mariadb-server

Once the MariaDB database server is installed, use the commands below to stop, start, and enable the MariaDB server to start automatically when the server boots.

sudo systemctl stop mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb

Run the following commands to validate and test if the MariaDB database server is installed successfully.

sudo mariadb

Once you run the commands above, it will log you onto the MariaDB console and display a message similar to the one below.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.11.2-MariaDB-1 Ubuntu 23.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

The message tells you that the server is installed successfully.

Additional help on installing MariaDB.

Create a Laravel database

Upon successfully installing the MariaDB database server, create a blank database on the server specifically for the Laravel application.

As part of the setup, we will create a laraveldb database and a user account called laraveldbuser.

Finally, we’ll grant the laraveldbuser full access to the laraveldb database.

All the database steps above can be done using the commands below:

But first, log on to the MariaDB database server:

sudo mariadb

Then run the commands below to complete the steps:

CREATE DATABASE laraveldb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER laraveldbuser@localhost IDENTIFIED BY 'type_your_password_here';
GRANT ALL ON laraveldb.* TO laraveldbuser@localhost WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit

Ensure to replace ‘type_your_password_here ‘with your password.

Install PHP-FPM on Ubuntu Linux

The last component you will need to run Laravel is PHP-FPM. The Laravel application is PHP-based and supports the latest versions of PHP.

Run the commands below to install PHP-FPM.

sudo apt install php-fpm php-intl php-mysql php-curl php-cli php-zip php-xml php-gd php-common php-mbstring php-xmlrpc php-json php-sqlite3 php-soap php-ldap php-zip

Additional help on installing PHP

How to install PHP on Ubuntu Linux

Install Laravel via Composer

Let’s begin downloading and configuring the Laravel files on Ubuntu Linux.

First, create these directories to set up Laravel.

  • /var/www/.cache (Composer cache)
  • /var/www/.config (additional Composer configuration),
  • /var/www/laravelapp (Laravel project).
sudo mkdir -p /var/www/{.cache,.config,laravelapp}
sudo chown -R www-data:www-data /var/www/{.cache,.config,laravelapp}

You may want to use the GitHub repository to get Laravel’s latest release. To get started, install Composer, Curl, and other dependencies.

sudo apt install curl git
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

After installing curl and Composer above, change the directory to the Nginx root directory and download the Laraval packages from Github.

cd /var/www/laravelapp/
sudo -u www-data composer create-project laravel/laravel .

Once you have completed all the above steps, continue configuring the Nginx web server below to serve the Laravel content.

First, open the ‘.env‘ file using the nano editor using the command below.

sudo -u www-data nano .env

Then, change the default ‘APP_URL‘ to match your domain name. In this example, Laravel will run on the domain ‘laravel.example.com‘.

APP_URL=http://laravel.example.com

Change the default ‘DB_CONNECTION‘ to ‘mysql‘ and uncomment, and change the database details with the information created above.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laraveldbuser
DB_PASSWORD=strong_password_here

Save and exit the file.

Lastly, run the command below to migrate the database for your Laravel project.

sudo -u www-data php artisan migrate

When you are done, run the command below to make the Nginx server owner of the Laraval files in its root directory.

sudo chown -R www-data:www-data /var/www/{.cache,.config,laravelapp}

Next, run the commands below to create an Nginx server block file for Laravel.

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

Then, copy and paste the content block below into the Nginx server block.

server {
listen 80;
listen [::]:80;
server_name laravel.example.com;
root /var/www/laravelapp/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index 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/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.(?!well-known).* {
deny all;
}
}

Save the file.

Then, run the commands below to enable the virtual host and restart the Nginx server.

sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

Setup Let’s Encrypt SSL/TLS for Laravel

You may want to install an SSL/TLS certificate to secure your Laravel site. Secure your Laravel installation with HTTPS from Let’s Encrypt.

Please read the post below for additional resources on installing and creating Let’s Encrypt SSL certificates for Nginx.

How to set up Let’s Encrypt SSL certificate for Nginx on Ubuntu Linux

Once you have restarted the Nginx web server, open your browser and browse to the server hostname or IP address defined in the Nginx server block.

http://laravel.example.com

Your Laravel site should be set up and ready to use.

That should do it!

Conclusion:

  • Installing the Laravel PHP framework with Nginx on Ubuntu 24.04 provides a robust and efficient environment for developing and deploying web applications.
  • The combination of Laravel, Nginx, and Ubuntu creates a powerful stack for hosting modern web applications backed by a strong and reliable community.
  • Following the outlined steps ensures the successful installation and configuration of Nginx, MariaDB, PHP-FPM, and Laravel, with additional guidance on setting up Let’s Encrypt SSL/TLS for enhanced security.
  • By leveraging this comprehensive guide, one can confidently set up a secure and robust environment to develop and host Laravel applications.
Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version