Deploy a PHP Website on AWS EC2 with Apache and MySQL: A Beginner's Guide
Cloud computing can seem complicated when you're just starting with DevOps. Terms such as EC2, SSH, security groups, web servers, and databases can make the learning process confusing.
Deploy a PHP Website on AWS EC2 with Apache and MySQL: A Beginner's Guide
Introduction
Cloud computing can seem complicated when you're just starting with DevOps. Terms such as EC2, SSH, security groups, web servers, and databases can make the learning process confusing.
In this tutorial, we'll build a simple PHP web application on an Amazon EC2 instance running Ubuntu.
By the end of this guide, we will have:
- An Ubuntu EC2 instance running on AWS
- Apache as the web server
- PHP for processing the application
- MySQL as the database
- A PHP application accessible through the internet
The goal is to understand how these components work together in a simple real-world deployment.
Architecture
The basic architecture for this project is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Internet
|
v
+-------------+
| Browser |
+-------------+
|
| HTTP
v
+-------------+
| AWS EC2 |
| Ubuntu |
+-------------+
|
v
+-------------+
| Apache |
+-------------+
|
v
+-------------+
| PHP |
+-------------+
|
v
+-------------+
| MySQL |
+-------------+
The browser sends a request to the EC2 instance. Apache receives the request and serves the PHP application. If the application needs database information, PHP communicates with MySQL.
Prerequisites
Before starting, you should have:
- An AWS account
- Basic Linux terminal knowledge
- Basic understanding of SSH
- A simple PHP application
- An SSH client such as OpenSSH
You should also understand that AWS resources can incur charges depending on your account, region, and usage. Always check the current AWS pricing and free-tier/Free Plan conditions before launching resources.
Step 1: Launch an EC2 Instance
First, sign in to the AWS Management Console and open Amazon EC2.
Choose Launch instance.
For this tutorial, we can use:
- Operating system: Ubuntu Server
- Instance type: a small instance suitable for your AWS account
- Authentication: SSH key pair
- Network: default VPC is sufficient for this basic tutorial
Give the instance a meaningful name, for example:
1
php-web-server
Create or select an SSH key pair and download the private key.
Important: Keep the private key secure. Do not upload it to GitHub.
Screenshot
Add a screenshot of your EC2 instance configuration here.
Step 2: Configure the Security Group
A security group controls which network traffic can reach the EC2 instance.
For our basic web server, we need:
| Protocol | Port | Purpose |
|---|---|---|
| SSH | 22 | Connect to the server |
| HTTP | 80 | Access the website |
For SSH, restricting access to your own IP address is preferable when practical rather than allowing SSH from anywhere.
HTTP can be opened to the internet because visitors need to access the website.
Screenshot
Add a screenshot of the inbound security group rules here.
Step 3: Connect to the EC2 Instance
After the instance is running, copy its public IPv4 address.
From your terminal, connect using SSH:
1
ssh -i your-key.pem ubuntu@YOUR_EC2_PUBLIC_IP
For example:
1
ssh -i myserver.pem ubuntu@203.0.113.10
The exact username can depend on the AMI you selected. Ubuntu images commonly use the
ubuntu user.After connecting successfully, you should see a terminal belonging to your EC2 instance.
We are now working directly on our AWS server.
Step 4: Update Ubuntu
Before installing packages, update the package information:
1
sudo apt update
Then upgrade installed packages if needed:
1
sudo apt upgrade -y
Why do we do this?
apt update refreshes information about available packages.apt upgrade installs available updates for packages already installed on the system.Keeping the server updated is an important basic administration practice.
Step 5: Install Apache
Apache will act as our web server.
Install it with:
1
sudo apt install apache2 -y
Check its status:
1
sudo systemctl status apache2
If Apache is running, you should see a status similar to:
1
Active: active (running)
You can also enable Apache to start automatically after a reboot:
1
sudo systemctl enable apache2
Step 6: Test Apache
Open a web browser and enter your EC2 public IPv4 address:
1
http://YOUR_EC2_PUBLIC_IP
If everything is configured correctly, you should see the Apache default page.
This confirms that:
1
Internet → AWS EC2 → Apache
is working.
Screenshot
Add a screenshot of the Apache default page here.
Step 7: Install PHP
Now we need PHP because our application is written in PHP.
Install PHP:
1
sudo apt install php libapache2-mod-php php-mysql -y
The packages provide PHP, Apache's PHP integration, and MySQL support for PHP.
Check the installed PHP version:
1
php -v
You should see the installed PHP version.
Step 8: Create a Simple PHP Test Page
Apache's default website files are normally stored in:
1
/var/www/html
Move into that directory:
1
cd /var/www/html
Create a PHP file:
1
sudo nano info.php
Add:
1
2
3
phpinfo();
Save the file.
Now open:
1
http://YOUR_EC2_PUBLIC_IP/info.php
If PHP is working correctly, the PHP information page should appear.
Screenshot
Add a screenshot of the PHP information page here.
Security note: Thephpinfo()page exposes configuration information. It should only be used temporarily for testing and should be removed afterward.
Remove it with:
1
sudo rm /var/www/html/info.php
Step 9: Install MySQL
Now we need a database server.
Install MySQL:
1
sudo apt install mysql-server -y
Check its status:
1
sudo systemctl status mysql
If it is running, you should see:
1
Active: active (running)
You can also enable it at boot:
1
sudo systemctl enable mysql
Step 10: Create a Database
Open the MySQL shell:
1
sudo mysql
Create a database:
1
CREATE DATABASE php_app;
Create a database user:
1
CREATE USER 'phpuser'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
Grant the required permissions:
1
GRANT ALL PRIVILEGES ON php_app.* TO 'phpuser'@'localhost';
Apply the privileges:
1
FLUSH PRIVILEGES;
Exit MySQL:
1
EXIT;
The application can now use:
1
2
Database: php_app
User: phpuser
Step 11: Deploy the PHP Application
Now we can place our PHP application inside Apache's document root.
For this tutorial, we can create a simple application:
1
cd /var/www/html
Create the main PHP file:
1
sudo nano index.php
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$host = "localhost";
$user = "phpuser";
$password = "StrongPasswordHere";
$database = "php_app";
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
}
echo "<h1>Hello from AWS EC2!</h1>";
echo "<p>Apache, PHP and MySQL are working together.</p>";
$conn->close();
Save the file.
Now open:
1
http://YOUR_EC2_PUBLIC_IP
You should see:
1
2
3
Hello from AWS EC2!
Apache, PHP and MySQL are working together.
At this point, we have successfully connected our web application to MySQL.
How the Request Works
When a user visits our website, several components work together.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
User's Browser
|
| HTTP request
v
AWS EC2 Instance
|
v
Apache Web Server
|
v
PHP Application
|
| Database query
v
MySQL Database
|
| Result
v
PHP
|
v
Apache
|
v
User's Browser
Understanding this flow is more important than simply memorizing the installation commands.
Step 12: Check the Services
We can verify that the important services are running.
Check Apache:
1
sudo systemctl status apache2
Check MySQL:
1
sudo systemctl status mysql
Check PHP:
1
php -v
Check which ports are listening:
1
sudo ss -lntp
For a basic setup, Apache should be listening for HTTP traffic on port
80.Troubleshooting
Website is not opening
First check Apache:
1
sudo systemctl status apache2
Then check whether port 80 is listening:
1
sudo ss -lntp | grep :80
Also check your EC2 security group and make sure HTTP port 80 is allowed.
SSH connection fails
Check:
- EC2 instance is running
- Public IPv4 address is correct
- SSH port 22 is allowed
- Correct username is being used
- Private key permissions are correct
- Correct
.pemfile is being used
PHP code is displayed instead of executed
Check whether PHP is installed:
1
php -v
Then check Apache's PHP integration and restart Apache:
1
sudo systemctl restart apache2
MySQL connection fails
Check MySQL:
1
sudo systemctl status mysql
Then verify:
- Database name
- Username
- Password
- Hostname
- PHP MySQL extension
Security Considerations
This tutorial is intentionally simple for learning purposes.
For a production deployment, additional security measures should be considered, including:
- Restricting SSH access
- Using HTTPS/TLS
- Secure database credentials
- Regular system updates
- Proper file permissions
- Application input validation
- Database query protection
- Backups
- Monitoring and logging
- Avoiding unnecessary open ports
Also, never commit private SSH keys or database passwords to a public GitHub repository.
What We Learned
In this tutorial, we deployed a simple PHP application on an AWS EC2 instance.
We learned how to:
- Launch an EC2 instance
- Configure a security group
- Connect to Ubuntu using SSH
- Install Apache
- Install PHP
- Install MySQL
- Create a database and database user
- Deploy a PHP application
- Connect PHP to MySQL
- Troubleshoot basic web-server problems
The most important concept is understanding how these components work together:
1
2
3
4
5
6
7
8
9
AWS EC2
|
Ubuntu
|
Apache
|
PHP
|
MySQL
This simple deployment is a useful starting point for learning more advanced DevOps topics such as Git, CI/CD, Docker, reverse proxies, infrastructure as code, monitoring, and container orchestration.
Conclusion
Deploying a web application manually on an EC2 instance is a good way to understand the fundamentals of cloud deployment.
Instead of treating AWS EC2 as just a virtual machine, we can think of it as the infrastructure on which our Linux server and application stack run.
Once these fundamentals are clear, the next step is to automate the
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article