How to Set Up an AWS Server for PHP in 2025 – Step by Step Guide

How to Set Up an AWS Server for PHP in 2025 – Complete Step by Step

If you’re a PHP developer and want to host your website or web application on AWS, this guide walks you through launching a fully functional PHP + MySQL server on AWS EC2. We’ll use Apache or Nginx, add optional SSL, and create a deployment-ready setup.

Tip: Bookmark this guide for reference while setting up your AWS server.

Why Use AWS for PHP Hosting?

  • Scalability: Start small and scale as your traffic grows.
  • Reliability: AWS servers are highly available with minimal downtime.
  • Security: Control access and firewall rules easily.
  • Free Tier: You can run a t2.micro instance free for 12 months.

Step 1: Create an AWS Account

  1. Go to AWS Console and sign up.
  2. Activate Free Tier (t2.micro, 750 hours/month).
  3. Verify your account with a credit card (Free Tier usage is free).

Tip: Track your usage in the AWS console to avoid unexpected charges.

[Insert screenshot of AWS Console signup here]

Step 2: Launch an EC2 Instance

  1. Go to EC2 → Instances → Launch Instances.
  2. Choose AMI: Ubuntu Server 22.04 LTS.
  3. Instance Type: t2.micro (Free Tier).
  4. Configure Security Group:
    • SSH (22) → your IP only
    • HTTP (80) → anywhere
    • HTTPS (443) → anywhere
  5. Create a Key Pair → download .pem file.

Why: EC2 is your virtual server; security groups act as a firewall.

[Insert screenshot of EC2 launch wizard here]

Step 3: Connect to Your EC2 Instance via SSH

chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@<EC2-Public-IP>

Replace <EC2-Public-IP> with your instance’s public IP.

[Insert screenshot of SSH terminal connection]

Step 4: Update Your Server

sudo apt update -y
sudo apt upgrade -y

Step 5: Install Web Server and PHP

Option 1: Apache

sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql -y
sudo systemctl enable apache2
sudo systemctl start apache2

Option 2: Nginx

sudo apt install nginx php-fpm php-mysql -y
sudo systemctl enable nginx
sudo systemctl start nginx

[Insert screenshot of Apache/Nginx running]

Step 6: Install MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

Secure MySQL by setting root password, removing test databases, and disabling remote root login.

Step 7: Deploy Your PHP App

Option A: Using Git

sudo apt install git -y
cd /var/www/html
sudo git clone https://github.com/your-repo.git .

Option B: Using SCP

scp -i your-key.pem -r /local/project ubuntu@<EC2-IP>:/var/www/html

Set Permissions

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

[Insert screenshot of PHP app deployed]

Step 8: Test PHP

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Open in browser: http://<EC2-Public-IP>/info.php

Step 9: Enable HTTPS (Optional)

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Follow prompts to secure your domain with SSL.

[Insert screenshot of HTTPS enabled]

Step 10: Optional Security Enhancements

  • Enable firewall:
    sudo ufw allow 'Apache Full'
    sudo ufw enable
  • Protect SSH with Fail2Ban:
    sudo apt install fail2ban -y
    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban

Step 11: Your Server is Ready

  • PHP + MySQL server live on AWS
  • Accessible via public IP or domain
  • Ready for future Docker, CI/CD, or Kubernetes deployment

Conclusion

This setup provides a modern, secure, and scalable PHP hosting environment on AWS. Perfect for deploying websites, blogs, or small web apps.

Next Steps: Consider Dockerizing your PHP app or setting up a CI/CD pipeline for automated deployment!

Leave a Comment

Your email address will not be published. Required fields are marked *