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:
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:
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:
Once Nginx is installed, start and enable it:
Step 4: Install MariaDB
MariaDB is an excellent choice for your database needs. Install it with:
During the installation, you'll be prompted to set a root password. Make sure to choose a strong one.
Start and 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:
Step 5: Create a Database and User
Log into the MariaDB shell as the root user:
Create a database for your Laravel project. Replace dbname
with your desired database name:
Create a user and grant privileges. Replace username
and password
with your chosen credentials:
Step 6: Install PHP and Required Extensions
Install PHP and necessary extensions:
Step 7: Configure Nginx
Create a new Nginx server block configuration file for your Laravel project:
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:
Save the file and create a symbolic link to enable the configuration:
Test the configuration:
If the test is successful, 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:
Obtain and install a certificate for 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:
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:
Step 12: Restart Nginx and PHP-FPM
Restart Nginx and PHP-FPM to apply the changes:
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!