How To Create AWS EC2 Instance And Host PHP Applications

Hello everyone,
 
Recently, I had to host a PHP application and I was scratching my head as to howI could do that. I went through documentation and videos and I was able to figure it out. So, I want to share how I did it and we will start with what  AWS EC2 is.
 

What is AWS?

 
So in one line, AWS stands for Amazon Web Services and it is the cloud computing platform from Amazon, for more you can read here.
 

What is EC2? 

 
EC2 stands for Amazon Elastic Compute Cloud (Amazon EC2) and it is a web service that provides various compute capacity in the cloud. It lets us create the desired computing environment quickly. For example, if you need a server up and running for you to test or deploy your application it will take only a minute or two to set up your server in EC2. 
 
So let us create a virtual machine and host a PHP application. At first, you need to have an AWS account. if you're a student then there is an easier way to get an AWS account using GitHub Student Developer Pack you can check it out here
 
Step 1
 
Go to AWS and click on AWS Management Console under My Account then choose Root user or IAM user based on the description and enter your email.
How To Create AWS EC2 Instance And Host PHP Applications 
Click on next and then enter your password and click on Sign in. 
 
How To Create AWS EC2 Instance And Host PHP Applications
Step 2
 
After signing in you will see this screen under the compute click on EC2 then you will be presented with the below screen.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Click on launch instance.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 3
 
Then choose your preferred server image, I want to install an Ubuntu server so I will proceed with this.
 
How To Create AWS EC2 Instance And Host PHP Applications
Then I will choose the free tier. What is the free tier now?
 
In this tier, for the first 12 months following your AWS sign-up date, you get up to 750 hours of micro instances each month. When your free usage tier expires or if your usage exceeds the free tier restrictions, you pay standard, pay-as-you-go service rates. 
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 4
 
Click on Configure instance details I will leave everything default here.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 5
 
Click on next to add storage. The default provided storage is 8 GB you can add more as per your need. 
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 6
 
Now click on the next to add a tag. A tag consists of a case-sensitive key-value pair. For example, you could define a tag with key = Name and value = Webserver. So I am adding PHP instance.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 7
 
Click on next Configure Security Group 
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 8
 
Then click on review and launch now.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
You will be asked to choose a key pair for authorization purposes. Choose to create a new key-pair and enter the name of your Key Pair and then click on download Key Pair. You need to keep it safe.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Click on view instance and you will see your instance running
 
How To Create AWS EC2 Instance And Host PHP Applications 
You will see the newly created instance is running.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 9
 
Choose your instance and click on connect, now we will be using SSH client in our case it will be putty.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
if you're on windows then you can download putty here and if you're Linux then you can install putty using your terminal. For Linux, these are the commands.
  1. sudo apt-get update
  2. sudo apt-get install -y putty
  3. putty
Now to connect you need to convert your keypairname.pem file to keypairname.ppk for authorization purposes. You can read how to do it. So after converting launch Putty and follow the steps to connect.
 
Step 10
 
Open putty and enter the public IP of your EC2 instance, in my case, it is 3.19.62.96. In putty, go to SSH and click auth and locate your keypairnname.ppk.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
This is my putty screen: 
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Click on open and a Putty security alert window will be prompted click on accept and wait for some time.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
After this, you must see the login and you need to enter your username which is ubuntu by default. Enter your username and hit enter and wait for some time.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
If everything looks good then you will see the below screen and finally, you're running your Ubuntu server in EC2. 
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Step 11: Now install the Apache server and PHP to run the PHP application 
 
In order to run a PHP application, we need to install an apache server. So follow the command to install the apache server and PHP.
  1. sudo apt update
  2. sudo apt install apache2
  3. sudo apt install php libapache2-mod-php php-mysql
Now open your web browser and type your public IP in my case again it is 3.19.62.96 and you must see the below Apache2 Ubuntu Default Page. This means we have installed Apache on the Ubuntu server and it is working fine.
 
How To Create AWS EC2 Instance And Host PHP Applications
 
Now, whenever a user goes to your web app the Apache server will return HTML files; that means index.html. So we are going to change the configuration and make it look for index.php first and then index.html if index.php is not available. So for doing that below are the commands.
 
sudo nano /etc/apache2/mods-enabled/dir.conf
 
You will see something like this:
  1. <IfModule mod_dir.c>  
  2.    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm  
  3. </IfModule>   
Now move the index.php to the first position like below and press CTRL+O and CTRL+X.
  1. <IfModule mod_dir.c>  
  2.    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm  
  3. </IfModule>   
Now restart the apache server and check the status of the server. You must see active(running).
  1. sudo systemctl restart apache2
  2. sudo systemctl status apache2
Step 12: Create a PHP application that displays 'Hello World' and host.
 
Now we are going to write a simple PHP program that displays 'Hello World' and host it on our server. The following are the commands for that.
  1. cd /var/www/html
  2. sudo nano index.php
Now write this code and press CTRL+O and CTRL+X.
  1. <?php  
  2.    echo "Hello World";  
  3. ?>   
Now go to your web browser and enter your public IP and you must see "Hello World".
 
How To Create AWS EC2 Instance And Host PHP Applications
I hope this is helpful, if you have any query, feedback or suggestions please do let me know. Stay Safe. 


Similar Articles