In this blog, we'll walk you through the process of Dockerizing a simple PHP application and deploying it on an AWS EC2 instance. The key twist? Our PHP app will seamlessly interact with a MySQL database hosted on AWS RDS. Buckle up as we break down the steps to achieve this seamless integration.
- Create an ec2 instance
Instance name: php_web_application
- Connect to the ec2 instance via ssh
Run the following commands in the ec2 instance terminal to install PHP
sudo apt update
sudo apt install apache2
sudo apt install php libapache2-mod-php php-mysql
These are the steps that we run to install PHP in our ec2 instance.
Then using the public IP of the instance, check if Apache is installed or not. If installed it will show like this welcome page
If the welcome page is not loaded in the browser then it may be that the Apache server is not started, so you can try the following commands to verify it.
#verify apache version
apache2 -v
#check status of the apache2
sudo systemctl status apache
#restart apache
sudo systemctl restart apache
#stop apache
sudo systemctl stop apache
#start the apache
sudo systemctl start apache
Now we are going to create our simple-php application in this ec2 instance. So go inside the /var/www/html/ page then check what are the files present there.
We have an index.html file inside /var/www/html. It’s time to create an index.php file for the code of the PHP application. Use the sudo nano file_name command to create and open index.php.
So what we have done is we created a PHP file named "index.php" and added the content Hello World from Subash. The welcome page of apche2 is moved to the index.html_bkp file. In this way, we will see our output on the welcome page of apache2.
Let's verify the application is accessible.
- Set up a PHP application with Amazon RDS
To deploy our PHP application using RDS, start by creating an RDS instance. Simply search for RDS in the search bar and click on the RDS option.
In the RDS dashboard click on Create database then:
Inside the Create database wizard, select the option Standard create
Select MySQL and choose any versionSelect Templates according to your need: Free-tier
Set the name of the RDS database, root username, and its password
Select the storage type and provide the allocated storage. Select the option of enabling storage autoscaling, depending upon the need of your PHP application.
In the connectivity, select the option Connect to an EC2 compute resource and select the EC2 instance from the drop-down list.
Click on the Create Database button
Hence we created a database in AWS RDS.
Now to connect the database with the web application, open the SSH client command prompt and install MySQL client
#mysql client
sudo apt install mysql-client-core-8.0
#install mariadb client
sudo apt install mariadb-client-core-10.6 -y
Now navigate to the html directory by using the command cd /var/www/html.
In the directory create a file dbinfo.inc and add the credentials of the RDS database instance endpoint, username, password, and database name in the file.
Now, let’s confirm whether the RDS is connecting or not by typing this command and providing the password of the RDS:
mysql -u master_username -p -h 'db_instance_endpoint'
In the output above it is visible that RDS is working.
create the sample database which will store the data of our PHP application
Here we created a database named "Sample" to store our data.
Verify PHP application page is accessible
Now let's add some data to our database.
As it is visible in the output above our PHP application is successfully deployed, and the data is stored in the database, as well as displayed in the table.
Dockerize our application
Install docker in ec2 instance
Sudo apt install -y docker.io
As we installed the docker, we can see the docker version and the docker status running (Active) in our ec2 instance.
Write the Dockerfile in the same directory i.e /var/www/html
# Use an official PHP runtime as a parent image
FROM php:7.4-apache
# Set the working directory to /var/www/html
WORKDIR /var/www/html
# Copy the current directory contents into the container at /var/www/html
COPY . /var/www/html
# Install mysqli extension
RUN docker-php-ext-install mysqli
# Expose port 80 for Apache
EXPOSE 80
# Start Apache when the container is launched
CMD ["apache2-foreground"]
Build the image
docker build -t my-php-app .
Run the container
sudo docker run -itd -p 8080:80 --name php-app my-php-app
Our container is running and let's verify it in the browser
We can see our application running at port 8080 in the ec2 instance public IP.
However, the port 8080 was not assigned in the security rule. If we did not add this port manually, we would not be able to get this output.
Select the instance and click on security. In the security tab, select the security groups and edit the inbound rules
After adding the inbound rule, try to access the app, it will successfully load the application.
Thank you for reading this post. I will try to write up the new blogs.
Happy Learning!