Deploying a PHP Application on 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 Ideal Solution for a PHP Project

\n

Developing a PHP application is only half the battle. The second, equally important part is a competent deployment to the server. A Virtual Private Server (VPS) provides flexibility, control, and performance unavailable with shared hosting. You get root access, the ability to fine-tune the environment, and complete resource isolation.

\n

In this guide, we will go from purchasing a server to a working application using PHP 8.x, Nginx, MySQL, and Git as an example. All steps are described for the Ubuntu 22.04 LTS operating system, the most popular distribution for web servers.

\n\n

1. Initial VPS Setup and Connection

\n\n

1.1. Choosing and Purchasing a VPS

\n

For a PHP application, the minimum requirements are: 1 vCPU, 1 GB RAM, 20 GB SSD. We recommend providers with a good reputation: DigitalOcean, Linode, Vultr, or local ones like Timeweb, Beget.

\n

After purchase, you will receive an IP address, root password (or SSH key).

\n\n

1.2. Connecting via SSH and Creating a User

\n

Connect to the server (replace 192.168.1.1 with your IP):

\n
ssh root@192.168.1.1
\n

Create an unprivileged user for daily work:

\n
adduser deployer\nusermod -aG sudo deployer
\n

Set up SSH keys for secure login:

\n
mkdir -p /home/deployer/.ssh\ncp ~/.ssh/authorized_keys /home/deployer/.ssh/\nchown -R deployer:deployer /home/deployer/.ssh\nchmod 700 /home/deployer/.ssh\nchmod 600 /home/deployer/.ssh/authorized_keys
\n

Disable root login via password, edit /etc/ssh/sshd_config:

\n
PermitRootLogin prohibit-password\nPasswordAuthentication no
\n

Restart SSH: systemctl restart sshd

\n\n

1.3. Basic Server Security

\n
    \n
  • Update packages: apt update && apt upgrade -y
  • \n
  • Install fail2ban: apt install fail2ban -y
  • \n
  • Configure UFW (firewall):
  • \n
\n
ufw allow OpenSSH\nufw allow 'Nginx Full'\nufw enable
\n\n

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

\n\n

2.1. Installing Nginx

\n
apt install nginx -y\nsystemctl enable nginx\nsystemctl start nginx
\n

Check it works by opening the server's IP address in a browser. You should see the Nginx welcome page.

\n\n

2.2. Installing PHP 8.x and PHP-FPM

\n

Add the repository for the latest PHP version:

\n
apt install software-properties-common -y\nadd-apt-repository ppa:ondrej/php -y\napt update\napt install php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip php8.2-gd -y
\n

Check PHP-FPM status:

\n
systemctl status php8.2-fpm
\n\n

2.3. Installing MySQL (MariaDB)

\n
apt install mariadb-server -y\nsystemctl enable mariadb\nsystemctl start mariadb
\n

Run the security script:

\n
mysql_secure_installation
\n

Create a database and user for the application:

\n
mysql -u root -p\nCREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\nCREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password';\nGRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';\nFLUSH PRIVILEGES;\nEXIT;
\n\n

2.4. Configuring an Nginx Virtual Host

\n

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

\n
nano /etc/nginx/sites-available/myapp
\n

File contents:

\n
server {\n    listen 80;\n    server_name myapp.com www.myapp.com;\n    root /var/www/myapp/public;\n    index index.php index.html;\n\n    location / {\n        try_files $uri $uri/ /index.php?$query_string;\n    }\n\n    location ~ \\.php$ {\n        fastcgi_pass unix:                

Blogs

Book Recommendations