Deploying a PHP Application on VPS: Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction



Deploying a PHP application on a Virtual Private Server (VPS) is a key stage in transitioning from local development to production. Unlike shared hosting, a VPS gives you full control over the runtime environment, which is critical for modern frameworks (Laravel, Symfony) and high-load projects.



In this guide, we will walk through the complete deployment cycle of a PHP application on a VPS with Ubuntu 22.04. You will learn how to set up the server, install the necessary components, and automate the deployment process. All code examples are ready to use.



1. VPS Preparation and Initial Setup



1.1. Connecting to the Server



After purchasing a VPS, you will receive an IP address, a login (usually root), and a password. Connect via SSH:



ssh root@your_ip_address


If you are using SSH keys (recommended), add your public key to the server.



1.2. System Update and User Creation



Update the packages and create an unprivileged user for work:



apt update && apt upgrade -yadduser deployerusermod -aG sudo deployer


Now connect under the new user:



ssh deployer@your_ip_address


1.3. Firewall Setup



Use UFW for basic protection:



sudo ufw allow OpenSSHsudo ufw allow 'Nginx Full'sudo ufw enablesudo ufw status


2. Installing the LEMP Stack (Linux, Nginx, MySQL, PHP)



2.1. Installing Nginx



sudo apt install nginx -ysudo systemctl start nginxsudo systemctl enable nginx


Check the operation by opening http://your_ip_address in a browser. You should see the Nginx welcome page.



2.2. Installing PHP and PHP-FPM



For modern frameworks, use PHP 8.x:



sudo apt install php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip php8.1-gd -y


Check the version and start PHP-FPM:



php -vsudo systemctl start php8.1-fpmsudo systemctl enable php8.1-fpm


2.3. Installing MySQL



sudo apt install mysql-server -ysudo mysql_secure_installation


Create a database for your application:



sudo mysql -u root -pCREATE DATABASE your_database_name;CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user'@'localhost';FLUSH PRIVILEGES;EXIT;


3. Virtual Host Configuration and Application Deployment



3.1. Creating an Nginx Virtual Host



Create a configuration file for your domain:



sudo nano /etc/nginx/sites-available/yourdomain.com


Insert the following configuration (adapt it to your project):



server {    listen 80;    server_name yourdomain.com www.yourdomain.com;    root /var/www/yourdomain.com/public;

index index.php index.html;

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

location ~ \\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; }

location ~ /\\.ht { deny all; }}


Activate the site:



sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl reload nginx


3.2. Deploying Code via Git



Install Git and clone the repository:



sudo apt install git -ycd /var/wwwsudo git clone https://github.com/yourusername/your-repo.git yourdomain.com


Set up permissions:



sudo chown -R www-data:www-data /var/www/yourdomain.comsudo chmod -R 755 /var/www/yourdomain.com/storagesudo chmod -R 755 /var/www/yourdomain.com/b                

Blogs

Book Recommendations