Deploying a PHP Application: Best Practices for Production πŸš€

Deploying a PHP Application: Best Practices for Production πŸš€

Deploying a PHP application isn’t just about uploading files to a server. To run smoothly in production, your app needs:
βœ… Proper server setup (Nginx/Apache + PHP + MySQL)
βœ… Security hardening πŸ”
βœ… Performance optimization πŸš€
βœ… Automatic deployment with Git & CI/CD
βœ… Error handling & monitoring

By the end of this guide, you’ll know how to deploy your PHP app like a pro! Let’s dive in. πŸš€


1️⃣ Choosing a Hosting Provider

Before deploying, you need a server. Here are some options:

Hosting Type Best For Example Providers
Shared Hosting Small projects Bluehost, Hostinger, GoDaddy
VPS (Virtual Private Server) Medium projects DigitalOcean, Linode, Vultr
Cloud Hosting Scalable applications AWS, Google Cloud, Azure
Managed PHP Hosting Hassle-free deployment Laravel Forge, Cloudways

πŸ”₯ Best choice?
βœ… For small apps β†’ Shared hosting
βœ… For APIs & web apps β†’ VPS (DigitalOcean, Linode, AWS Lightsail)
βœ… For large-scale apps β†’ Cloud hosting (AWS, Google Cloud, Azure)


2️⃣ Setting Up a VPS for PHP Deployment

If you're using a VPS (Ubuntu 22.04), follow these steps to install Nginx, PHP, and MySQL.

1️⃣ Update Your System

sudo apt update && sudo apt upgrade -y

2️⃣ Install Nginx

sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

Visit http://your-server-ip in your browser. You should see the Nginx Welcome Page.

3️⃣ Install PHP 8.x

sudo apt install php8.2 php8.2-fpm php8.2-mysql -y

Check PHP version:

php -v

4️⃣ Install MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

πŸ”₯ Now your server is ready for PHP applications! πŸŽ‰


3️⃣ Deploying Your PHP App to a Server

1️⃣ Upload Your Files via SSH

On your local machine, run:

scp -r /path/to/your/app user@your-server-ip:/var/www/html/

2️⃣ Set Proper File Permissions

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

3️⃣ Configure Nginx for PHP

Create a new Nginx site configuration:

sudo nano /etc/nginx/sites-available/yourdomain.com

Paste this:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/html/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    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;
    }

    location ~ /\.ht {
        deny all;
    }
}

Save and enable the site:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx

πŸ”₯ Now your PHP app is live on your domain! πŸŽ‰


4️⃣ Using Git for Deployment

Instead of manually uploading files, let’s use Git for automated deployments.

1️⃣ Install Git on Your Server

sudo apt install git -y

2️⃣ Clone Your Repository

On your server, navigate to /var/www/html/ and run:

git clone https://github.com/yourusername/yourproject.git .

3️⃣ Set Up Automatic Deployments

Run:

git config --global core.autocrlf input
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Now, add this Git hook to automatically deploy changes when you push to GitHub.

nano /var/www/html/.git/hooks/post-receive

Paste this:

#!/bin/bash
cd /var/www/html
git pull origin main
chown -R www-data:www-data /var/www/html
systemctl restart nginx

Make it executable:

chmod +x /var/www/html/.git/hooks/post-receive

πŸ”₯ Now every time you push to GitHub, your app deploys automatically! πŸš€


5️⃣ Setting Up SSL for HTTPS (Free with Let’s Encrypt)

For secure HTTPS, install a free SSL certificate with Let’s Encrypt.

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Set up auto-renewal:

sudo certbot renew --dry-run

πŸ”₯ Now your website is secure with HTTPS! πŸ”


6️⃣ Optimizing PHP Performance

πŸš€ Speed up your PHP application with these optimizations:

1️⃣ Enable OPcache

Edit php.ini:

sudo nano /etc/php/8.2/fpm/php.ini

Find and update:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0

Restart PHP:

sudo systemctl restart php8.2-fpm

2️⃣ Use a Reverse Proxy (Varnish)

Install Varnish:

sudo apt install varnish -y

Edit /etc/default/varnish and change the port to 80. Then restart:

sudo systemctl restart varnish

πŸ”₯ This speeds up your app by caching requests! πŸš€


7️⃣ Monitoring Errors & Logs

1️⃣ Enable PHP Error Logging

Edit php.ini:

log_errors = On
error_log = /var/log/php_errors.log

Restart PHP:

sudo systemctl restart php8.2-fpm

Check logs:

tail -f /var/log/php_errors.log

2️⃣ Use Supervisor for Background Tasks

sudo apt install supervisor -y

Example worker.conf for queue workers:

[program:queue_worker]
command=php /var/www/html/artisan queue:work
autostart=true
autorestart=true
stderr_logfile=/var/log/queue_worker.err.log
stdout_logfile=/var/log/queue_worker.out.log

Start Supervisor:

sudo systemctl restart supervisor

πŸ”₯ Now background jobs run smoothly!


🎯 Final Checklist for Production Deployment

βœ… Server Set Up (Nginx + PHP + MySQL)
βœ… HTTPS Enabled (SSL with Let’s Encrypt)
βœ… Git for Automatic Deployment
βœ… Performance Optimized (OPcache, Varnish, Caching)
βœ… Monitoring & Logging (Error logs, Supervisor)

πŸš€ Boom! Your PHP app is now production-ready!

πŸ‘‰ Next: Error Handling and Exception Handling in PHP

Happy deploying! πŸŽ‰πŸš€

Leave a Reply