top of page

Tutorial to run LAMP stack within AWS Cloud9

Set up & run LAMP stack within AWS Cloud9 IDE using Ubuntu

Objectives:

  • Learn to set up LAMP stack within AWS Cloud9 IDE

  • Learn to create a PHP based web application

  • Learn to Run LAMP (Linux, Apache HTTP Server, MySQL, and PHP)on cloud9

Prerequisites:

  • You must have an existing AWS Cloud9 EC2 development environment.

  • You have the AWS Cloud9 IDE for the existing environment already open.

  • To know more visit Cloud9 IDE

Step 1: Install the following tools:

  • Apache HTTP Server, a web server host.

  • PHP, a scripting language that is especially suited for web development and can be embedded into HTML.

  • MySQL, a database management system.

  • Ensure that the latest security updates and bug fixes are installed on the instance. To do so run the following command: “sudo apt -y update

  • Check whether Apache HTTP Server is already installed. To do this, run the

apache2 -v” command. If successful, the output contains the Apache HTTP Server version number. If you see an error, then install Apache HTTP Server by running the install command: “sudo apt install -y apache2

  • Confirm whether PHP is already installed by running the “php -v” command. If successful, the output contains the PHP version number. If you see an error, then install PHP by running the install command:

sudo apt install -y php libapache2-mod-php php-xml

  • Confirm whether MySQL is already installed by running the “mysql –version” command. If successful, the output contains the MySQL version number. If you see an error, then install MySQL by running the install command:

"sudo apt install -y mysql-server"

  • After you install Apache HTTP Server, PHP, and MySQL, start Apache HTTP Server, and then confirm it has started, by running the following command (to return to the command prompt, press q):

sudo service apache2 start && sudo service apache2 status

  • Start MySQL, and then confirm it has started, by running the following command: “sudo service mysql start && sudo service mysql status

Step 2: Set up MySQL:

  • Implement MySQL security best practices for the MySQL installation by running the following command in a terminal session in the AWS Cloud9 IDE: “sudo mysql_secure_installation

  • Answer the following questions as specified:

  • Would you like to setup VALIDATE PASSWORD plugin – Type Y, and then press Enter.

  • There are three levels of password validation policy – Type 0, 1, or 2, and then press Enter. Choose as per requirement.

  1. New password – Type a password, and then press Enter.

  2. Re-enter new password – Type the password again, and then press Enter. (Be sure to store the password in a secure location for later use.)

  3. Do you wish to continue with the password provided – Type Y, and then press Enter.

  4. Remove anonymous users – Type Y, and then press Enter.

  5. Disallow root login remotely – Type Y, and then press Enter.

  6. Remove test database and access to it – Type Y, and then press Enter.

  7. Reload privilege tables now – Type Y, and then press Enter.

  • To interact directly with MySQL, start the MySQL command line

  • (“sudo mysql -uroot -p”) client as the root user by running the following command. When prompted, type the root user's password that you set earlier, and then press Enter. (The prompt changes to mysql> while you are in the MySQL command line client.)

  • To exit the MySQL command line client, run the following command (The prompt changes back to $.): “exit;

Step 3: Set up a website:

  • Set up the default website root for the Apache HTTP Server (/var/www/html) with recommended owners and access permissions. To do this, run the following six commands, one at a time in the following order, in a terminal session in the AWS Cloud9 IDE. To understand what each command does, read the information after the # character after each command:

  1. sudo groupadd web-content

  2. sudo usermod -G web-content -a ubuntu

  3. sudo usermod -G web-content -a www-data

  4. sudo chown -R ubuntu:web-content /var/www/html

  5. sudo find /var/www/html -type f -exec chmod u=rw,g=rx,o=rx {} \;

  6. sudo find /var/www/html -type d -exec chmod u=rwx,g=rx,o=rx {} \;

  • Create a PHP-based webpage named index.php in the default website root folder for the Apache HTTP Server (which is /var/www/html) by running the following command:

  • sudo touch /var/www/html/index.php && sudo chown -R ubuntu:web-content /var/www/html/index.php && sudo chmod u=rw,g=rx,o=rx /var/www/html/index.php && sudo printf '%s\n%s\n%s' '<?php' 'phpinfo();' '?>' >> /var/www/html/index.php

  • Run the following commands in the exact order

  1. MY_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)

  2. MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text)

  3. aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --cidr 0.0.0.0/0 --port 80

  4. aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --ip-permissions IpProtocol=tcp,Ipv6Ranges='[{CidrIpv6=::/0}]',FromPort=80,ToPort=80

  5. MY_SUBNET_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SubnetId' --output text)

  6. MY_NETWORK_ACL_ID=$(aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$MY_SUBNET_ID --query 'NetworkAcls[].Associations[0].NetworkAclId' --output text)

  7. aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10000 --cidr-block 0.0.0.0/0 --port-range From=80,To=80

  8. aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10100 --ipv6-cidr-block ::/0 --port-range From=80,To=80

a. Get the URL to the index.php file within the web server root.To do this, run the following command (“MY_PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) && echo http://$MY_PUBLIC_IP/index.php”) and use a new web browser tab or a different web browser separate from the AWS Cloud9 IDE to go to the URL that is displayed. If successful, the webpage displays information about Apache HTTP Server, MySQL, PHP, and other related settings.

Step 4: Clean up:

  • Run the following commands in the exact order:

  1. MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text)

  2. aws ec2 revoke-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --cidr 0.0.0.0/0 --port 80

  3. aws ec2 revoke-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --ip-permissions IpProtocol=tcp,Ipv6Ranges='[{CidrIpv6=::/0}]',FromPort=80,ToPort=80

  4. MY_SUBNET_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SubnetId' --output text)

  5. MY_NETWORK_ACL_ID=$(aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$MY_SUBNET_ID --query 'NetworkAcls[].Associations[0].NetworkAclId' --output text)

  6. aws ec2 delete-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --rule-number 10000

  7. aws ec2 delete-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --rule-number 10100


Note: If you no longer need this instance make sure to terminate the instance.

Click on the drop-down menu beside the actions button.

Select the Instance State and click on Terminate.

This will terminate your instance.



UbuntuLAMP
.pdf
Download PDF • 321KB


Was this document helpful? How can we make this document better? Please provide your insights. You can download PDF version for reference.

Recent Posts

See All

17 Comments


Gokul M
Gokul M
Apr 10

It is easy to understand to run LAMP stack within cloud9

Like

Gokulnath
Gokulnath
Mar 20

Good blog

Like

Atchaya B
Atchaya B
Mar 20

Well defined sir

Like

very useful sir

Like

Easy to follow

Like
bottom of page