PHP Deployment
6 min read
Deploy PHP with Nginx and PHP-FPM on Linux. Enable OPcache, set display_errors to Off, use HTTPS, and store secrets in environment variables. Automate deployments with GitHub Actions for consistency.
Deploying PHP Applications
# Nginx + PHP-FPM (recommended)
sudo apt install nginx php8.3-fpm php8.3-mysql
# /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 $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
# Deployment checklist
# 1. Set APP_ENV=production
# 2. Disable display_errors
# 3. Enable OPcache
# 4. composer install --no-dev --optimize-autoloader
# 5. Set correct file permissions
# 6. Enable HTTPS (certbot)