# Deploy a dockerized simple PHP application on AWS EC2 instance

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.

1. **Create an ec2 instance**
    

Instance name: php\_web\_application

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703143268010/42690565-7bd0-4e0a-9832-d62d048c5330.png align="center")

1. **Connect to the ec2 instance via ssh**
    

Run the following commands in the ec2 instance terminal to install PHP

```plaintext
sudo apt update
sudo apt install apache2
sudo apt install php libapache2-mod-php php-mysql
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703143662249/a72e95ec-f763-4d3f-8043-d3da158ca99f.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703144131326/7b9df5b8-ae88-4b81-8869-ed7eba689614.png align="center")

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.

```plaintext
#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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703144663699/8918b371-f88a-4c18-b96e-ed0b63dd8620.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703145271599/3ed62ac4-c423-45d5-aa17-441f91fa2311.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703145494060/d0c53045-a39a-41a3-bb56-a0ff2e5651cf.png align="center")

1. **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 **version**
    
* Select 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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703146621701/a3db96cd-683b-43b2-8bca-76bac7d2f0c4.png align="center")

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

```plaintext
#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 *<mark>cd /var/www/html.</mark>*

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703151236590/146ba22c-b9d8-48ea-8677-70a9a9130d8d.jpeg align="center")

Now, let’s confirm whether the RDS is connecting or not by typing this command and providing the password of the RDS:

```plaintext
 mysql -u master_username -p -h 'db_instance_endpoint'
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703147827351/b9996d7f-3cd0-4be1-b84a-5ec3bd4059f4.png align="center")

In the output above it is visible that RDS is working.

1. **create the sample database which will store the data of our PHP application**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703148715596/d9c4a5ea-befd-421a-86ee-6dc56b91e6eb.png align="center")
    
    Here we created a database named "Sample" to store our data.
    
2. **Verify PHP application page is accessible**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703151342094/c301f275-aa80-4ad6-80a3-dd5e041bc78f.png align="center")

Now let's add some data to our database.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703151418045/f8e3055e-a309-4d01-a628-a609877bca63.png align="center")

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.

1. Dockerize our application
    
    Install docker in ec2 instance
    
    ```plaintext
    Sudo apt install -y docker.io
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703151777709/6d6fa2a4-9876-4dcd-a1ce-5e04359ea7ad.png align="center")
    
    As we installed the docker, we can see the docker version and the docker status running (Active) in our ec2 instance.
    
2. Write the Dockerfile in the same directory i.e /var/www/html
    

```plaintext
# 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"]
```

1. Build the image
    
    ```plaintext
    docker build -t my-php-app .
    ```
    
    1. Run the container
        
        ```plaintext
        sudo docker run -itd -p 8080:80 --name php-app my-php-app
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703159871693/1a475f92-11e3-4fb4-ad65-1f6164ee1320.png align="center")
        
        Our container is running and let's verify it in the browser
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703159914833/c0da0921-3387-4443-8e1d-bf26298af28b.png align="center")
        
        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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703162939190/f40e354c-5495-4ff7-913c-5c7edcdecbe4.jpeg align="center")

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!
