Installing a LAMP Web Server on Amazon EC2

In this blog post we will understand procedures that will help you install the Apache web server with PHP and MySQL support on your Amazon EC2 Ubuntu instance. We will go through procedure, step by step.

Prerequisites

We assume that you have registered for Amazon AWS account. If you have not registered, first of all register for it through this link .

After you have registered login to Amazon AWS account and go to AWS Management Console. From services, go to Amazon EC2

Launch Amazon EC2 Instance

First of all we have to create and launch Amazon EC2 instance. Click on Launch Instance. It will ask you to select Amazon Machine Image in Step 1

 

 

 

 

From here select Ubuntu Server. In next step it will ask you to select instance type. Depending on your application size and traffic, you can choose instance. For now we will go for default selected micro instance.

Click on Review and Launch. It will take you to final step, to review. From there click on Launch. It will show a pop up to create key pair. This will be used to connect with instance using SSH. Create and download key pair and save it to desktop. Now click on Launch Instance.

It will launch instance, once the instance is launched, click on View Instances. Once you click on running instance, it will show you all the details of instance like Public DNS, public IP etc. IP will use to connect with SSH.

Now, instance is up and running, we will connect with SSH and install necessary components.

Connect to instance with SSH

To connect we will use key pair created while we were creating instance. Go to terminal and go to directory, where you downloaded key and run following command.

chmod 400 YourKey.pem

This will change key permission to make it readable. Now connect to instance using public IP of your instance.

ssh -i “YourKey.pem” ubuntu@YourIP

Install Apache and PHP 7.0

sudo apt-get install apache2 php7.0 libapache2-mod-php7.0

Now verify it.

a2query -m php7.0

Load PHP module.

sudo a2enmod php7.0

Restart apache.

sudo service apache2 restart

New version of ubuntu comes with PHP 7 but to access it you have to install php command lines. Run following command.

sudo apt-get install php7.0-cli

Now test it with

php -v

Create Test PHP

Go to your apache root directory with following command

cd /var/www/html

There will be one index.html file. We have to remove this and create new file for testing.

sudo rm index.html

sudo vim index.php

This will open vim editor, press i and now you can type in file. Let’s create simple php file.

<?php
echo “Hello AWS”;
?>

press esc and : and type wq this will save file.

View test PHP file in Browser

To view file in browser, copy the public DNS and paste in browser. But wait it won’t work because Amazon EC2 instances have inbound rules where you have to configure what types of connections can be made to your instance. By default only SSH is allowed, we have to add HTTP connection.

For that go to security groups and in bound rule.

Now refresh browser and you will see Hello AWS message.

Leave a Reply

Your email address will not be published. Required fields are marked *