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! ππ