Deploying LAMP in AWS Part 1: Create Fresh OS & LAMP Image

  • On May 14, 2018

Introduction

Amazon cloud services boast one of the most reliable and cost-effective cloud computing services in the world. Coupled with its security and many cloud products, it is very enticing to developers. Amazon EC2 referred to us as Amazon Elastic Compute Cloud is a web service that provides a secure and scalable compute allocation in the cloud.

So let’s see how we can deploy LAMP using instances.

Creating an instance

Login to our AWS account.Select Compute.

Click on EC2

Next, on the left-hand side, click on Launch instance

We are going to create a Linux based instance; Select Amazon Linux AMI

Select the free tier type. (Choose what suits your installation), click on Configure Instance details.

To Launch an AWS Instance, a VPC is required. Click on Create a New Default VPC

Click Create

On the Network, Click refresh to display the created VPC.Leave the rest as default.Click Add Storage

Select Add New storage as per your requirements. In this article, leave as default then Click Add Tags

Select or type the description of the tag to categorise the instance created.

Configure firewall rules to allow traffic as needed. SSH and TCP are the two rules required to complete this installation. By default, SSH is added. To add another rule; click on add rule.

Add the HTTP, and leave the rest as they are. Click save.

Review AWS Instance configuration.If all settings are correct Click Launch

A prompt select create a new key pair. Type the name of the Key. Click on Download Key Pair.

Note: Remember, this is the only chance for you to download the key pair.

Note: store it in a safer place. Click Launch Instance.

Now whenever you need to use your instance, click on instances and it will direct you to where you will see a list of instances.

Now it takes a short time for instances to launch.Click on view instance.

Click on the instance created an go to its properties. At the bottom of the page, note the public DNS name and the public IP.

Connect to EC2 instance

The next step is to connect with the instance via SSH. You can use PuTTY or an alternative SSH client; you can also use your system’s browser. Remember, if we did not launch our instance with a key pair, this step would not work.

We are going to go with the PuTTY method, so make sure you install it. You can get it here. After installation, go to the putty key generator. Select the load button.

On the window, navigate to where the key is saved. Once we import it, click on save private key.

Save the key as *.ppk

. From the Instance window, Click on Connect.Details on how to connect using Putty are displayed.

Once the file is saved, open the main PuTTY, Copy from the highlighted;

Paste the details copied on the Hostname. The port 22, and the connection type is SSH.

Next, under the category, select SSH and then Auth.Upload the key we generated Click on browse and import the key.

Finally, click open. You should see the following;

Installing the Lamp sever

Update the packages available on the server. Use the following command;

$ sudo yum update -y

Once completed, we can now install our MySQL,php and apache. Use the following command:

$ sudo yum install -y httpd24 php70 mysql56 php70-msqlnd

Once installed you can now start it using the following command;

$ sudo service httpd start

To configure Apache to start whenever the instance reboots type;

$ sudo chkconfig httpd on

To verify that http is running type the following;

$ chkconfig –list httpd

Testing the Apache

Go to the instance we created. Copy the IPV4 address

Once you have done that and you go back and refresh the page, you should see the Amazon test page.

It shows that apache is configured and running.

Configuring MySQL

Start MySQL server by typing;

$ sudo service mysqld start

Run MySQL in_secure configuration

$ sudo mysql_secure_installation

When prompted for a current password; Enter as by default the root password is blank.Type Yes to set a root password.

Enter the New password and confirm.

Type Y for all the prompts;

Set file permissions

Type the following to list all directories;

$ ls -l /var/www

Default Apache root directory is /var/www/html and is owned by root.

$ sudo usermod -a -G apache ec2-user

Log out of the SSH session and Log in again.

To verify membership of ec2-user;Type

$ groups

To change ownership of /var/www to the Apache group

$ sudo chown -R ec2-user:apache /var/www

Add group write permissions and set group ID.

$ sudo chmod 2775 /var/www

$ find /var/www -type d -exec sudo chmod 2775 {} \;

Add group write permissions, recursively change permissions on /var/www and its sub-directories.

$ find /var/www -type f -exec sudo chmod 0664 {} \;

Ec2-user and any member of Apache group should be able to add content to the Apache directories.

Test PHP

Create the basic PHP.info file and see if it is displayed. Type the following command;

$ echo “<?php phpinfo(); ?>” > /var/www/html/phpinfo.php

To verify just add /phpinfo.php to the Amazon ami test page;

http://ec2-13-58-35-244.us-east-2.compute.amazonaws.com/phpinfo.php

Congratulations we have completed setting up our LAMP server.

Install phpMyAdmin

Written in PHP, It offers a graphical interface, and that means you can run commands via your browser without you having to log in to the server. You can easily create and run a website with no prior knowledge of SQL.

Run this commands to install the dependencies;

$ sudo yum install php70-mbstring.x86_64 php70-zip.x86_64 -y

Restart Apache

$ sudo service httpd restart

Download the latest phpMyAdmin package from here.Using wget command get the link of the latest package.

wget https://files.phpmyadmin.net/phpMyAdmin/4.8.0.1/phpMyAdmin-4.8.0.1-all-languages.tar.gz

Extract the downloaded phpMyAdmin file.

tar -xvzf phpMyAdmin-4.8.0.1-all-languages.tar.gz

mv phpMyAdmin-4.8.0.1-all-languages phpMyAdmin

Restart Apache service.

sudo service mysqld start

Launch phpMyAdmin

http://my.public.dns.amazonaws.com/phpMyAdmin

In this tutorial;

http:// ec2-13-58-35-244.us-east-2.compute.amazonaws.com/phpMyAdmin

Conclusion

You can now launch your web applications and create your own story. With phpMyAdmin, administration of the database is made more accessible. The LAMP is a free and mature stack and takes a short time to install and configure. However, it is not the best when there are high traffic and many concurrent users.

Next steps:

Next you will have to create a snapshot of your instance, then prepare a procedure for pre-patching it with the latest OS updates (“A Golden AMI”) before it is ready to be deployed using Cloud Formation.

Deploying LAMP in AWS Part 2: Creating a Golden AMI coming soon!