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 0.0.0.0/0”, 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:
Next step is to install Apache 2 to host the PHP website using the following command:
Press “y” to continue when the prompt displays for continuity.
After the successful installation of Apache2, install PHP and its dependencies by typing
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:
Let’s change our working directory to “html” in the Apache directory:
It’s time to create the PHP file using the touch command:
Now open this “index.php” file using any editor, here we are using the nano editor:
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:
Let’s list all the files using the ls command to verify the changes:
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.
Conclusion
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.