How to Deploy a PHP Application on Ubuntu VPS (Complete 2026 Guide)
Deploying a PHP application to your own VPS gives you full control over performance, security, and scalability. Unlike shared hosting with its limitations, a VPS allows you to optimize every layer of the stack to match your application needs.
1. Server Preparation
After purchasing a VPS (DigitalOcean, Vultr, or any provider), SSH in and perform initial setup:
# Update system
sudo apt update && sudo apt upgrade -y
# Create non-root user
sudo adduser deployer
sudo usermod -aG sudo deployer
# Setup firewall
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
2. Install Nginx + PHP-FPM
# Install Nginx
sudo apt install nginx -y
# Install PHP 8.3 + extensions
sudo add-apt-repository ppa:ondrej/php -y
sudo apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip -y
# Verify
php -v
sudo systemctl status php8.3-fpm
3. Virtual Host Configuration
# /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.com;
root /var/www/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4. Free SSL with Certbot
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d myapp.com
# Auto-renew is automatic via systemd timer
5. Deploy Code with Git
# On server
cd /var/www
sudo git clone https://github.com/user/myapp.git
sudo chown -R www-data:www-data myapp/
# Setup .env
cp .env.example .env
nano .env # fill in database credentials, API keys, etc
6. Auto-Deploy (Optional)
For automatic deployment on every Git push, create a simple webhook:
# /var/www/myapp/deploy.php
$secret = "your_webhook_secret";
$sig = $_SERVER["HTTP_X_HUB_SIGNATURE_256"] ?? "";
$body = file_get_contents("php://input");
$expected = "sha256=" . hash_hmac("sha256", $body, $secret);
if (!hash_equals($expected, $sig)) { http_response_code(403); exit; }
shell_exec("cd /var/www/myapp && git pull origin main 2>&1");
echo "Deployed!";
Conclusion
With this setup, your PHP application runs in an optimal environment - Nginx as reverse proxy, PHP-FPM for process management, and SSL for security. The same techniques are used by platforms like OTPZap to serve thousands of API requests per minute with sub-100ms response times.