Deploying a PHP Application on a VPS: A Step-by-Step Guide from A to Z

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: Why VPS is the Best Choice for a PHP Project?

Developing a web application in PHP is only half the battle. The second, equally important part, is competent deployment. If you have outgrown shared hosting with its limitations and want full control over the environment, a Virtual Private Server (VPS) is the ideal solution. In this article, we will walk through the path from purchasing a server to launching your PHP application in production mode. You will learn how to set up the Nginx + PHP-FPM + MySQL stack, configure a domain, an SSL certificate, and automate code updates.



Step 1: Initial VPS Setup and Connection

1.1 Choosing an OS and Initial Login

We recommend using Ubuntu 22.04 LTS — stable and with long-term support. After purchasing a VPS, you will receive an IP address and root password.

Connect via SSH:

ssh root@your_server_IP


1.2 Creating a User and Security Setup

Working as root is bad practice. Let's create a separate user with sudo privileges:

adduser deployerusermod -aG sudo deployer

Now let's configure SSH keys for convenient and secure login. On your local machine, generate a key (if you don't have one):

ssh-keygen -t rsa -b 4096

Copy the public key to the server:

ssh-copy-id deployer@your_server_IP

After verifying key-based login, disable password login and root access:

sudo nano /etc/ssh/sshd_config# Change:PermitRootLogin noPasswordAuthentication no# Restart SSH:sudo systemctl restart sshd


Step 2: Installing and Configuring the LEMP Stack (Nginx, PHP, MySQL)

2.1 Installing Nginx

Update packages and install the web server:

sudo apt update && sudo apt upgrade -ysudo apt install nginx -y

Check the status:

sudo systemctl status nginx


2.2 Installing PHP 8.x and Required Extensions

Add the repository with the latest PHP version:

sudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/phpsudo apt updatesudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip -y


2.3 Installing MySQL (MariaDB)

sudo apt install mariadb-server mariadb-client -ysudo mysql_secure_installation

Create a database and user for your application:

sudo mysql -u rootCREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password';GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';FLUSH PRIVILEGES;EXIT;


Step 3: Configuring a Virtual Host (Server Block) in Nginx

Create a configuration file for our domain (e.g., myapp.ru):

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

Insert the following template for PHP-FPM:

server {    listen 80;    server_name myapp.ru www.myapp.ru;    root /var/www/myapp/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.2-fpm.sock; }

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

Activate the site and reload Nginx:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/sudo nginx -t  # Check configurationsudo systemctl reload nginx


Step 4: Deploying Application Code (Git + Composer)

4.1 Cloning the Repository

Create a directory for the project and clone the code (e.g., from GitHub):

sudo mkdir -p /var/www/myappsudo chown -R deployer:depl                

Blogs

Book Recommendations