You are currently viewing How do you deploy the Laravel application on Ubuntu VPS (virtual private server) with CloudPanel in 2024?
Laravel app deploy

How do you deploy the Laravel application on Ubuntu VPS (virtual private server) with CloudPanel in 2024?

Deploying a Laravel application on an Ubuntu VPS can be smooth and efficient when using CloudPanel, a modern server management tool designed for PHP applications. This guide will walk you through the steps to deploy your Laravel application on an Ubuntu VPS using CloudPanel.

Step 1: Set Up CloudPanel

If you haven’t installed CloudPanel yet, here’s a quick overview:

  1. Connect to Your VPS: Use SSH to connect to your VPS.
ssh root@your-server-ip

2. install Cloudpanel :

curl -sSL https://installer.cloudpanel.io/ce/v2/install.sh | sudo bash

This will install CloudPanel and all necessary dependencies, including MySQL, Nginx, and PHP.

Access CloudPanel: Once installed, you can access CloudPanel via your web browser by visiting http://your-server-ip:8443. Log in with the credentials provided during installation.

Create a web application:

  • Goto site Section and create a new site this will give a user and directory path like “/home/sitename/htdocs/site.com/public” Make sure you will access the username and password

Create a Database :

  • Goto database section and create a database ‘site-db’ and create a username and password generate.

Step 2: Deploy Laravel framework Project

Firstly, your site-user which is created in creating a web application step and SSH key added in your GitHub SSH setting then you will be able to pull the project via SSH. How to create a ssh key on Github?

git clone ssh-url

otherwise, you can pull the project using the HTTP link.

create .env file: set all environment variables in it. especially database credentials which are created in a database step.

Install a Dependencies: using composer and run this command in your project directory

composer install

Composer is a dependency management tool for PHP, primarily used to manage libraries that your PHP project depends on. It allows developers to specify the libraries their project requires, and Composer will handle the installation, updating, and versioning of those libraries. Instead of manually downloading and including each library, Composer automates the process, ensuring that the correct versions of the libraries are used and that they are all compatible with each other. Composer also provides a centralized repository called Packagist, where developers can discover and share PHP packages, further simplifying the development process. It has become an essential tool in modern PHP development, enabling easier management of complex projects. composer install

Key generate for APP_KEY in .env file using this key

php artisan key:generate

Run migration with seed using this command

php artisan migrate:fresh --seed

Create storage link in your public directory

php artisan storage:link

Give permission to your project directory and note down these command make sure run in your project directory.

sudo chown -R site-user:www-data .

sudo find . -type f -exec chmod 664 {} \;   
sudo find . -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
php artisan config:cache
php artisan optimize
 

Step 3: Install Supervisor

Supervisor is a process control system that allows you to monitor and manage background processes in Unix-like operating systems, such as Linux. It is particularly useful for ensuring that critical applications, such as web servers, queue workers, or other long-running processes, remain running and automatically restart if they fail.

Supervisor operates by running as a daemon, which is a background process itself, and it can start, stop, and restart other processes as needed. It uses configuration files to specify the commands for starting and managing these processes, allowing you to easily control them via a web-based interface, command line tools, or through API calls.

sudo apt update && sudo apt install supervisor
sudo systemctl status supervisor
sudo nano /etc/supervisor/conf.d/your-site.conf

/your-site.conf file code is here

[program:your-site]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /home/site/htdocs/your-site/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/home/site/htdocs/your-site/storage/logs/your-site-worker.log
stopwaitsecs=3600

add your worker to the supervisor

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart all

Conclusion:

Deploying a Laravel apps on an Ubuntu VPS using CloudPanel is a straightforward process that can be accomplished with a few steps. CloudPanel simplifies server management, allowing you to focus on your application rather than server configuration. By following this guide, you should have a fully functional application running on your VPS in no time.

Leave a Reply