Setting Up a Laravel Project on Ubuntu VPS with Nginx, Let's Encrypt, and MariaDB

Are you ready to take your Laravel project live on the web? If you're running your application on an Ubuntu VPS and want to ensure secure and efficient hosting, you've come to the right place. In this guide, we'll walk through the process of setting up your Laravel project with Nginx, Let's Encrypt for HTTPS, and MariaDB for your database.

This is a no complex straightforward fastest deployment setup for learning purposes only, for a more managed approach, you have to setup CI/CD pipeline with test, staging, and production servers.

Prerequisites

Before we dive in, make sure you have the following:

  • An Ubuntu-based VPS (Virtual Private Server) with SSH access.
  • A Laravel project ready to deploy.
  • A domain name pointed to your VPS's IP address.

Step 1: SSH into Your VPS

If you're using Windows, you can use PowerShell or PuTTY to connect to your server.

Open your terminal and SSH into your VPS by running the following command:

ssh username@your_server_ip

Replace username with your server's username and your_server_ip with your VPS's IP address.

Step 2: Update and Upgrade

Let's ensure your server is up to date. Run these commands:

sudo apt update

sudo apt upgrade

This ensures that you have the latest package information and updates installed.

Step 3: Install Nginx

Nginx is a powerful web server and reverse proxy that we'll use to serve your Laravel application. Install it with:

sudo apt -y install nginx

Once Nginx is installed, start and enable it:

sudo systemctl start nginx

sudo systemctl enable nginx

Step 4: Install MariaDB

MariaDB is an excellent choice for your database needs. Install it with:

sudo apt install -y mariadb-server mariadb-client

During the installation, you'll be prompted to set a root password. Make sure to choose a strong one.

Start and enable MariaDB:

sudo systemctl start mariadb
			
sudo systemctl enable mariadb

You can secure your MariaDB installation by running, the command-line installation wizard will guide you through the process and ask for your inputs:

sudo mysql_secure_installation

Step 5: Create a Database and User

Log into the MariaDB shell as the root user:

sudo mysql -u root -p

Create a database for your Laravel project. Replace dbname with your desired database name:

CREATE DATABASE dbname;

Create a user and grant privileges. Replace username and password with your chosen credentials:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 6: Install PHP and Required Extensions

Install PHP and necessary extensions:

sudo apt install -y php-fpm php-mysql php-json php-curl php-mbstring php-xml php-zip

Step 7: Configure Nginx

Create a new Nginx server block configuration file for your Laravel project:

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

Add the following configuration, replacing your_domain with your domain name and your_project_path with the path to your Laravel project's public directory:

server {
			listen 80;
			server_name your_domain;

			root /your_project_path/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/php7.2-fpm.sock;
			}

			location ~ /\.ht {
			deny all;
			}

			error_log /var/log/nginx/laravel_error.log;
			access_log /var/log/nginx/laravel_access.log;
			}
		

Save the file and create a symbolic link to enable the configuration:

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

Test the configuration:

sudo nginx -t

If the test is successful, reload Nginx:

sudo systemctl reload nginx

Step 8: Install Let's Encrypt Certificates

To secure your Laravel project with HTTPS, we'll use Let's Encrypt. Install the Certbot client:

sudo apt install -y certbot python3-certbot-nginx

Obtain and install a certificate for your domain:

sudo certbot --nginx -d your_domain

Follow the prompts to configure Nginx to use the certificate.

Step 9: Configure Laravel .env

Navigate to your Laravel project directory and edit the .env file to configure your database settings:

cd /your_project_path
			
nano .env

Update the DB_DATABASE, DB_USERNAME, and DB_PASSWORD fields with your database information.

Step 10: Assign Proper permissions:

Run the following commands to set the proper file permissions:

sudo chown -R www-data:www-data /your_project_path 

sudo chown -R 755 /your_project_path/bootstrap

sudo chmod -R 755 /your_project_path/storage

Step 11: Migrate and Seed

Run Laravel's migration and seeding commands to set up your database:

php artisan migrate
			
php artisan db:seed

Step 12: Restart Nginx and PHP-FPM

Restart Nginx and PHP-FPM to apply the changes:

sudo systemctl restart nginx
			
sudo systemctl restart php7.2-fpm

Step 13: Access Your Laravel Project

Open your web browser and visit your domain over HTTPS. You should see your Laravel project up and running, securely hosted on your Ubuntu VPS.

Congratulations! You've successfully set up a Laravel project on an Ubuntu VPS using Nginx, Let's Encrypt for HTTPS, and MariaDB. Your project is now live on the web and ready for the world to see. Happy coding!