Lesson 4 of 8
VPS Deployment - Server Setup & Deploy Pipeline
Deploying PHP to a production VPS involves setting up the server stack (Nginx + PHP-FPM + MySQL), securing it, configuring environment variables, and automating deployments. Zero-downtime deployment ensures users never see an error while you deploy.
VPS Deployment - From Server to Live
# ── Server setup (Ubuntu 22.04 LTS) ────────────────────────────
# 1. Update system
sudo apt update && sudo apt upgrade -y
# 2. Install Nginx, PHP-FPM, MySQL, Certbot
sudo apt install -y nginx php8.2-fpm php8.2-cli php8.2-mbstring \
php8.2-pdo php8.2-mysql php8.2-redis php8.2-opcache \
php8.2-xml php8.2-curl php8.2-zip \
mysql-server redis-server certbot python3-certbot-nginx unzip
# 3. Harden MySQL
sudo mysql_secure_installation
sudo mysql -e "CREATE DATABASE blog CHARACTER SET utf8mb4;"
sudo mysql -e "CREATE USER 'blog_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';"
sudo mysql -e "GRANT ALL ON blog.* TO 'blog_user'@'localhost';"
# 4. Install Composer globally
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# 5. UFW Firewall - only allow needed ports
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
# ── PHP-FPM configuration ─────────────────────────────────────
# /etc/php/8.2/fpm/php.ini - production overrides
# display_errors = Off
# log_errors = On
# error_log = /var/log/php/error.log
# post_max_size = 20M
# upload_max_filesize = 20M
# memory_limit = 256M
# max_execution_time = 30
# Opcache (as configured in Module 11)
# opcache.enable=1
# opcache.validate_timestamps=0
# opcache.memory_consumption=256
# ── Deploy script - deploy.sh ─────────────────────────────────
#!/bin/bash
set -euo pipefail # Exit on error, undefined vars, pipe failures
APP_DIR="/var/www/html/blog"
RELEASE_DIR="$APP_DIR/releases/$(date +%Y%m%d%H%M%S)"
SHARED_DIR="$APP_DIR/shared"
echo "→ Deploying to $RELEASE_DIR"
# 1. Clone or pull latest code
git clone git@github.com:user/blog.git "$RELEASE_DIR"
cd "$RELEASE_DIR"
# 2. Composer install - no dev dependencies, optimised autoloader
composer install --no-dev --optimize-autoloader --no-interaction
# 3. Link shared directories (uploads, logs, .env are outside release)
ln -s "$SHARED_DIR/.env" "$RELEASE_DIR/.env"
ln -s "$SHARED_DIR/storage" "$RELEASE_DIR/storage"
# 4. Run database migrations
php artisan migrate --force # --force required in production
# 5. Clear and warm caches
php artisan config:cache
php artisan route:cache
php artisan view:cache
# Or for vanilla PHP:
# php cli/blog cache:clear --force
# 6. Swap symlink atomically - near zero-downtime!
ln -sfn "$RELEASE_DIR" "$APP_DIR/current"
# 7. Reload PHP-FPM (not restart - keeps existing workers until their requests finish)
sudo systemctl reload php8.2-fpm
# Also resets Opcache bytecode cache
# 8. Keep only 5 most recent releases (cleanup old)
ls -dt "$APP_DIR/releases"/*/ | tail -n +6 | xargs rm -rf
echo "✓ Deployment complete!"
# ── Directory structure on server ─────────────────────────────
# /var/www/html/blog/
# ├── current -> releases/20260320143000/ ← symlink (updated atomically)
# ├── releases/
# │ ├── 20260320130000/ ← previous release (kept for quick rollback)
# │ └── 20260320143000/ ← current release
# └── shared/
# ├── .env ← never in git!
# └── storage/ ← uploads, logs - persisted across deploys
# Nginx root points to:
# root /var/www/html/blog/current/public;
# ── SSL with Let's Encrypt (free, auto-renewing) ──────────────
sudo certbot --nginx -d blog.example.com -d www.blog.example.com
# Certbot modifies Nginx config and sets up auto-renewal
# crontab: 0 0,12 * * * certbot renew --quietQuick Quiz
Tip
Tip
Practice VPS Deployment Server Setup Deploy Pipeline in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for VPS Deployment - Server Setup & Deploy Pipeline
Practice Task
Note
Practice Task - (1) Write a working example of VPS Deployment Server Setup Deploy Pipeline from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with VPS Deployment Server Setup Deploy Pipeline is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?