Introduction: Why VPS is the Ideal Solution for a PHP Project
\nDeveloping 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.
\nIn 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\n1. Initial VPS Setup and Connection
\n\n1.1. Choosing and Purchasing a VPS
\nFor 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.
\nAfter purchase, you will receive an IP address, root password (or SSH key).
\n\n1.2. Connecting via SSH and Creating a User
\nConnect to the server (replace 192.168.1.1 with your IP):
ssh root@192.168.1.1\nCreate an unprivileged user for daily work:
\nadduser deployer\nusermod -aG sudo deployer\nSet up SSH keys for secure login:
\nmkdir -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\nDisable root login via password, edit /etc/ssh/sshd_config:
PermitRootLogin prohibit-password\nPasswordAuthentication no\nRestart SSH: systemctl restart sshd
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
ufw allow OpenSSH\nufw allow 'Nginx Full'\nufw enable\n\n2. Installing and Configuring the LEMP Stack (Linux, Nginx, MySQL, PHP)
\n\n2.1. Installing Nginx
\napt install nginx -y\nsystemctl enable nginx\nsystemctl start nginx\nCheck it works by opening the server's IP address in a browser. You should see the Nginx welcome page.
\n\n2.2. Installing PHP 8.x and PHP-FPM
\nAdd the repository for the latest PHP version:
\napt 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\nCheck PHP-FPM status:
\nsystemctl status php8.2-fpm\n\n2.3. Installing MySQL (MariaDB)
\napt install mariadb-server -y\nsystemctl enable mariadb\nsystemctl start mariadb\nRun the security script:
\nmysql_secure_installation\nCreate a database and user for the application:
\nmysql -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\n2.4. Configuring an Nginx Virtual Host
\nCreate a configuration file for our domain (e.g., myapp.com):
nano /etc/nginx/sites-available/myapp\nFile contents:
\nserver {\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: