1. Launch EC2 Instance & Connect
- Choose Ubuntu 22.04 or Amazon Linux 2
- Open ports: 22 (SSH), 80 (HTTP), 443 (HTTPS)
- SSH into your EC2 instance:
ssh -i "your-key.pem" ec2-user@your-ec2-ip
2. Install PHP & Git
sudo apt update
sudo apt install php php-fpm php-mysql git -y
php -v
3. Clone Your PHP App
git clone https://github.com/yourusername/your-php-app.git
cd your-php-app
4. Option A: Use PHP-FPM
sudo apt install php-fpm -y
🔧 Configure PHP-FPM
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
# Change the user and group to www-data:
user = www-data
group = www-data
listen = /var/run/php/php7.4-fpm.sock
🔧 Start PHP-FPM
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
5. Option B: Run Without PHP-FPM
php -S 0.0.0.0:8000 -t public
6. Apache (httpd) as Reverse Proxy with HTTPS
sudo apt update
sudo apt install apache2 -y
sudo a2enmod proxy
sudo a2enmod proxy_fcgi
sudo a2enmod ssl
sudo nano /etc/apache2/sites-available/your-php-app.conf
ServerName yourdomain.com
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/your-php-app/$1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName yourdomain.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/your-php-app/$1
sudo a2ensite your-php-app
sudo systemctl reload apache2
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache
7. Nginx as Reverse Proxy
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/your-php-app
server {
listen 80;
server_name yourdomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sudo ln -s /etc/nginx/sites-available/your-php-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl enable nginx
🔒 Secure with HTTPS
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
8. Final Tips
- Use
.env
for secrets and configs - Monitor app logs using
tail -f /var/log/apache2/error.log
- Ensure firewall rules allow ports 80/443
🎉 Congrats! Your PHP app is now live on AWS EC2 with a secure production setup.