Introduction: From Local Development to a Production Server
Developing a PHP application on a local machine is only half the battle. True mastery begins when the code hits the server and starts serving real users. Advanced PHP deployment on a VPS is not just about copying files via FTP. It is a comprehensive process involving environment setup, security, automation, and monitoring.
In this article, we will break down how to configure a VPS for professional PHP application hosting using modern tools and DevOps practices. You will learn how to deploy an application on the Nginx + PHP-FPM stack, set up a CI/CD pipeline, optimize performance, and implement monitoring.
It is assumed you have basic knowledge of Linux, PHP, and web servers. We are moving to an advanced level.
1. VPS Preparation and Environment Setup
Server Selection and Initial Configuration
For advanced deployment, choose a VPS with at least 2 GB of RAM and an SSD drive. Recommended OS are Ubuntu 22.04 LTS or Debian 12. After gaining SSH access, perform basic setup:
- Update packages:
sudo apt update && sudo apt upgrade -y - Create a user with sudo privileges:
sudo adduser deployer && sudo usermod -aG sudo deployer - Configure SSH keys (disable password login)
- Install fail2ban for brute-force protection
- Configure UFW (allow ports 22, 80, 443)
Installing the LEMP Stack (Nginx + PHP-FPM + MySQL)
We use Nginx as the frontend server and PHP-FPM to process PHP. Install all components with a single command:
sudo apt install nginx mysql-server php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip composer git -yCheck the versions:
php -vnginx -vmysql --versionConfiguring PHP-FPM for High Performance
For advanced deployment, modify the PHP-FPM configuration. Edit the file /etc/php/8.3/fpm/pool.d/www.conf (version may vary):
pm = dynamicpm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500Important php.ini settings for production:
memory_limit = 256Mupload_max_filesize = 64Mpost_max_size = 64Mmax_execution_time = 300date.timezone = Europe/MoscowRestart PHP-FPM: sudo systemctl restart php8.3-fpm
2. Application Deployment: From Git to Production
Cloning and Setting Permissions
Create a directory for the project and clone the repository:
sudo mkdir -p /var/www/yourprojectsudo chown -R deployer:deployer /var/www/yourprojectcd /var/www/yourprojectgit clone git@github.com:yourusername/yourproject.git .Set up access permissions:
sudo chown -R www-data:www-data /var/www/yourproject/storagesudo chown -R www-data:www-data /var/www/yourproject/bootstrap/cachesudo chmod -R 775 /var/www/yourproject/storagesudo chmod -R 775 /var/www/yourproject/bootstrap/cacheConfiguring an Nginx Virtual Host
Create a configuration file for your domain:
sudo nano /etc/nginx/sites-available/yourprojectExample of an advanced configuration with caching and compression:
server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourproject/public;
index index.php index.html;
charset utf-8;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \\.php$ { fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
location ~ /\