DEV Community

Anish Ansari
Anish Ansari

Posted on • Updated on

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.
Login

Click on next and then enter your password and click on Sign in.

Login

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.

List

Click on launch instance.

Launch

Step 3
Then choose your preferred server image, I want to install an Ubuntu server so I will proceed with this.

Image

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.

Free tier

Step 4

Click on Configure instance details I will leave everything default here.

Detals

Step 5

Click on next to add storage. The default provided storage is 8 GB you can add more as per your need.

Storage

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.

Keyvalue

Step 7

Click on next Configure Security Group

Security

Step 8

Then click on review and launch now.

Launch

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.

Keypairdialog

Click on view instance and you will see your instance running

Status

You will see the newly created instance is running.

INstance

Step 9

Choose your instance and click on connect, now we will be using SSH client in our case it will be putty.

INstance

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.

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
sudo apt-get install -y putty
Enter fullscreen mode Exit fullscreen mode
putty

Enter fullscreen mode Exit fullscreen mode

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.

Putty

This is my putty screen:
Putty

Click on open and a Putty security alert window will be prompted click on accept and wait for some time.

Accept

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.

terminal

If everything looks good then you will see the below screen and finally, you're running your Ubuntu server in EC2.

Putty

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.

sudo apt update
Enter fullscreen mode Exit fullscreen mode
sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode
sudo apt install php libapache2-mod-php php-mysql
Enter fullscreen mode Exit fullscreen mode

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.

Apache

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
Enter fullscreen mode Exit fullscreen mode

You will see something like this:

<IfModule mod_dir.c>  
   DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm  
</IfModule>

Enter fullscreen mode Exit fullscreen mode

Now move the index.php to the first position like below and press CTRL+O and CTRL+X.

<IfModule mod_dir.c>  
   DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm  
</IfModule> 
Enter fullscreen mode Exit fullscreen mode

Now restart the apache server and check the status of the server. You must see active(running).

sudo systemctl restart apache2
sudo systemctl status apache2
Enter fullscreen mode Exit fullscreen mode

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.

cd /var/www/html
sudo nano index.php

Enter fullscreen mode Exit fullscreen mode

Now write this code and press CTRL+O and CTRL+X.

<?php  
   echo "Hello World";  
?> 
Enter fullscreen mode Exit fullscreen mode

Now go to your web browser and enter your public IP and you must see "Hello World".

Apache

I hope this is helpful, if you have any query, feedback or suggestions please do let me know. Stay Safe.

Buy Me A Coffee

Top comments (0)