🌐 Install Nginx + PHP-FPM on a VPS#
Learn how to install and configure Nginx with PHP-FPM on your VPS to create a high-performance web server. This stack is ideal for WordPress, Laravel, Symfony, and other PHP applications.
🎯 Objectives#
- ✅ Install Nginx and PHP-FPM on Ubuntu/Debian
- ⚡ Optimize configuration for performance
- 🔧 Configure virtual hosts for multiple sites
- 🛠️ Test and validate the installation
- 🔒 Apply security best practices
🧰 Prerequisites#
- VPS running Ubuntu 20.04+ or Debian 11+
- Root or sudo access
- Active SSH connection
- Domain name pointing to your VPS (optional for testing)
1️⃣ System preparation#
📦 System update#
# Package update sudo apt update && sudo apt upgrade -y # Essential tools installation sudo apt install -y curl wget gnupg2 software-properties-common
🔧 Adding PHP repository (for latest version)#
# Adding Ondrej repository for PHP sudo add-apt-repository ppa:ondrej/php -y sudo apt update
2️⃣ Nginx installation#
📥 Web server installation#
# Nginx installation sudo apt install nginx -y # Start and enable automatic startup sudo systemctl start nginx sudo systemctl enable nginx # Status verification sudo systemctl status nginx
🌐 Basic test#
Open your browser and go to http://your-server-ip
. You should see the Nginx welcome page.
# Verify that Nginx is listening on port 80 sudo netstat -tlnp | grep :80
3️⃣ PHP-FPM installation#
📥 PHP and essential extensions installation#
# PHP 8.2 installation with FPM and common extensions sudo apt install -y php8.2-fpm php8.2-cli php8.2-common php8.2-mysql \ php8.2-zip php8.2-gd php8.2-mbstring php8.2-curl php8.2-xml \ php8.2-bcmath php8.2-intl php8.2-opcache php8.2-readline # Start and enable PHP-FPM sudo systemctl start php8.2-fpm sudo systemctl enable php8.2-fpm # Status verification sudo systemctl status php8.2-fpm # Verify the socket works sudo ls -la /run/php/
4️⃣ Nginx configuration for PHP#
🗂️ Directory structure#
# Create structure for your sites sudo mkdir -p /var/www/html/mysite sudo chown -R www-data:www-data /var/www/html/ sudo chmod -R 755 /var/www/html/
📝 Site configuration (virtual host)#
# Create configuration file sudo nano /etc/nginx/sites-available/mysite
Complete configuration:
server { listen 80; listen [::]:80; # Replace with your domain name server_name mysite.com www.mysite.com; # Site root directory root /var/www/html/mysite; index index.php index.html index.htm; # Site-specific logs access_log /var/log/nginx/mysite_access.log; error_log /var/log/nginx/mysite_error.log; # PHP configuration location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Security - block access to sensitive files location ~ /\.ht { deny all; } location ~ /\.git { deny all; } # Optimization - static file caching location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|zip)$ { expires 1y; add_header Cache-Control "public, immutable"; } # URL handling (for WordPress, Laravel, etc.) location / { try_files $uri $uri/ /index.php?$query_string; } }
✅ Site activation#
# Site activation (create symbolic link) sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/ # Remove default site (optional) sudo rm /etc/nginx/sites-enabled/default # Configuration test sudo nginx -t # Nginx reload sudo systemctl reload nginx
5️⃣ Complete installation test#
📄 PHP test file creation#
# Create a PHP test file sudo nano /var/www/html/mysite/info.php
<?php phpinfo(); ?>
🌐 Browser tests#
- PHP test:
http://your-domain/info.php
⚠️ Security: Delete the
info.php
file after testing!
sudo rm /var/www/html/mysite/info.php
6️⃣ Service changes application#
🔄 Apply changes#
# Restart services sudo systemctl restart php8.2-fpm sudo systemctl restart nginx # Verify proper operation sudo systemctl status nginx php8.2-fpm
7️⃣ Maintenance and monitoring#
📊 Monitoring commands#
# Service status sudo systemctl status nginx php8.2-fpm # Real-time logs sudo tail -f /var/log/nginx/error.log sudo journalctl -fu php8.2-fpm # Active PHP-FPM processes sudo ps aux | grep php-fpm # Active connections sudo netstat -an | grep :80
🔧 Management commands#
# Restart without interruption sudo systemctl reload nginx sudo systemctl reload php8.2-fpm # Configuration test before application sudo nginx -t sudo php-fpm8.2 -t
✅ Installation summary#
Your Nginx + PHP-FPM server is now operational with:
- ✅ Nginx optimized and secured
- ✅ PHP 8.2 with essential extensions
- ✅ PHP-FPM operational
- ✅ Virtual hosts configured
- ✅ Basic security applied