Deploy a PHP Website Using Amazon EC2 Instance

People often want to test their websites on a server where actual users can visit them, or maybe they want to host their websites somewhere with high-level security without requiring many configurations.

EC2 allows hosting a website securely without any hustle as it is easy to understand and has a powerful computing capacity. PHP website on an EC2 instance using Apache2 and then allowing traffic to the EC2 instances by editing the inbound rules.

Deploy PHP Website Using EC2

Firstly, let’s launch an EC2 instance by searching EC2 in the Search Bar of the AWS Management console. Click on the “Launch Instance” drop-down button; it will open a list select “Launch instance”:

Give your EC2 instance a “Name”:

And choose the “Amazon Machine Image” as “Ubuntu”; Amazon Machine Image is an operating system for your EC2 instance. Also, choose its Architecture as 64 bits:

Click on “Create new key pair” to generate key pair for the secure login and connectivity:

Write the name of the key pair and leave the rest of the options as default. Click on the “Create key pair” button:

Scroll down and do some configuration in the Network settings of the EC2 instance. Make sure that your “Auto-assign public IP” is “enable”, “Allow SSH traffic from” is “selected” and set on “Anywhere”, also select the option “Allow HTTPS traffic from the internet”:

Note: Make sure the HTTPS traffic is enabled so that the client web browser can communicate with this EC2.

Now click on “Launch instance”:

You will get a “Success” message that your EC2 instance has been launched successfully:

Now from the Instance Dashboard click on “instances” under the category of “instances”, and search for the instance you just created and click on “Instance ID”:

Press the “Connect” button:

In the “Connect to instance”, go to “SSH client” tab and copy the “Example” command:

Paste the command in the Command Prompt or in PowerShell of your system, and hit Enter to run the command:

You will be successfully logged into your Ubuntu Amazon Machine Image EC2 instance:

Update the system before any installation by typing this command:

$ sudo apt-get update

Next step is to install Apache 2 to host the PHP website using the following command:

$ sudo apt install apache2

Press “y” to continue when the prompt displays for continuity.

After the successful installation of Apache2, install PHP and its dependencies by typing

$ sudo apt install php php-mysql

Press “y” to continue when the prompt displays for continuity.

Now list all the files in this directory of the Apache2 web server using the command:

$ ls -la /var/www/

Let’s change our working directory to “html” in the Apache directory:

$ cd /var/www/html/

It’s time to create the PHP file using the touch command:

$ sudo touch index.php

Now open this “index.php” file using any editor, here we are using the nano editor:

$ sudo nano index.php

Once the file is opened, it’s time to type the PHP code for your website:


echo "PHP Application";


Save the file using “ctrl + s” and “ctrl + x” to go back to the terminal.

Note: Apache2 always looks for index.html to run by default; if you want any other file to get opened by default, then name it as index along with its extension and change the name of index.html.

Use the mv command:

$ sudo mv index.html index.html.temp

Let’s list all the files using the ls command to verify the changes:

$ ls

The PHP website is deployed successfully, and it’s time to test it, so go to “Instance summary” and click on “open address”:

The hyperlink will open PHP website in a new tab of the browser:

Your PHP website has been successfully deployed on AWS.


To deploy a PHP website using EC2, launch the EC2 instance using the Ubuntu Amazon Machine Image, and make sure to allow HTTPS traffic. Connect with your EC2 using the SSH client and install Apache2 and PHP along with the dependencies. After doing so, create the PHP file with the website code and change it. After making changes, deploy the website to test it.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.