LAMP on AWS

Setup an AWS Cloud LAMP stack. Before you begin, be sure that you've got access to AWS account Amazon EC2.

Make and Launch an EC2 Instance

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html

An instance is a virtual server in the AWS Elastic Compute Cloud (EC2). You launch an instance from an Amazon Machine Image (AMI). The AMI provides the operating system, application server, and applications for your instance. After you launch your instance, you can connect to it and use it.

The instance receives a public DNS name that you can use to contact the instance from the internet. The instance also receives a private DNS name that other instances within the same VPC can use to contact the instance.

  • The Choose an Amazon Machine Image (AMI) page displays a list of basic configurations, called Amazon Machine Images (AMIs), that serve as templates for instances. Select the Amazon Linux 2 AMI (HVM), SSD Volume Type AMI. Notice that these AMIs are marked "Free tier eligible."

  • On the Choose an Instance Type page, select the t2.micro instance type, which is selected by default. The t2.micro instance type is eligible for the free tier. Click the Review and Launch button.

  • On the Review Instance Launch page, under Security Groups, you'll see that the wizard created and selected a security group for you. Click the Edit security groups link to use a different group.
  • Click the Launch button.

  • In the popup window, select Create a new key pair and then type a name for the key
  • Click the Download Key Pair button and save the .pem file. This file is used to connect later.
  • Click the Launch Instances button.

  • A Launch Status page lets you know that your instance is launching. Choose View Instances to close the confirmation page and return to the console.

Setup the AWS Security Group

Security groups enable you to control traffic to your instance, including the kind of traffic that can reach your instance. Allow all IP addresses to access your instance using HTTP or HTTPS, so that external users can browse the content on your web server.

The following inbound rules allow HTTP and HTTPS access from any IP address:

Type Protocol Port Range Source IPs
HTTP TCP 80 Anywhere 0.0.0.0/0
::/0
HTTPS TCP 443 Anywhere 0.0.0.0/0
::/0
  • Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  • From the console dashboard, choose Security Groups.
  • Select the security group to edit (for this example we use launch-wizard-1), choose Actions > Edit inbound rules.
  • Choose Add rule and fill-in the rule values.
  • Add or remove as many rules as needed.
  • When finished editing rules, click the Save rules button.

Connect to the Instance with SSH

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

  • Open the Amazon EC2 console at: https://console.aws.amazon.com/ec2/
  • Find the instance you want to launch from the list. Note Public IPv4 DNS value for the instance. You will need this in a moment.
  • On your local machine open the Terminal app.
  • Change the permissions on the local .pem file you downloaded from the instance.
chmod 400 /path/to/my/keys_file.pem
  • Use ssh to login to the instance. The -i stands for identity_file, and points to the local .pem file you downloaded from the instance. The .pem file contains the private key used for authentication.

    By default the instance user is named ec2-user. The Public IPv4 DNS for our example instance is ec2-54-177-128-251.us-west-1.compute.amazonaws.com.

ssh -i /path/to/my/keys_file.pem ec2-user@ec2-54-177-128-251.us-west-1.compute.amazonaws.com

Update and Install Software

  • Connect to the instance with SSH.
  • You can view your version of Amazon Linux using the following command:
cat /etc/system-release
  • Update the instance software:
sudo yum update -y
  • Install the lamp-mariadb10.2-php7.2 and php7.2 Amazon Linux Extras repositories to get the latest versions of the LAMP MariaDB and PHP packages for Amazon Linux 2.
sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
  • Install the Apache web server and MariaDB along with all related dependencies:
sudo yum install -y httpd mariadb-server
  • View the current versions of a package using the following command:
yum info package_name

Setup the Apache Web Server

Start the Apache web server:

sudo systemctl start httpd

Use the systemctl command to configure the Apache web server to start at each system boot:

sudo systemctl enable httpd

Verify that httpd is enabled at startup:

sudo systemctl is-enabled httpd

In an incognito browser window (to avoid DNS cache) go to the public URL for the server: http://ec2-54-151-6-15.us-west-1.compute.amazonaws.com/

You should see the default Apache server page:

Setup MariaDB

Start the mysql server (to stop: sudo systemctl stop mariadb):

sudo systemctl start mariadb

See the server version:

 mysql -V

Use the systemctl command to configure the MariaDB server to start at each system boot:

sudo systemctl enable mariadb

Verify that mariadb is enabled at startup:

sudo systemctl is-enabled mariadb

Secure mysql and set a root password:

sudo mysql_secure_installation

Set the root users password and record it for later use, and then answer y to all questions.

Setup Git

  • Install git on an EC2 instance.
sudo yum install git -y
  • Check the git version:
git version
  • Set your name and email address:
git config --global user.name "Jane Donut";
git config --global user.email janed@gmail.com
  • Run the following command to store the github login credentials locally to avoid entering them for every action. After running the command, on the next successful authentication, the credentials will be recorded to the users home folder: ~/.git_credentials.
sudo git config --global credential.helper store
  • Run the following command from within a repo directory to see repo-level and global-level configs. To see just the global configs, run the following command from outside a repo directory:
git config --list

Stop and Start an EC2 Instance

You can stop and start your instance if it has an Amazon EBS volume as its root device. The instance retains its instance ID, EBS volumes, any Elastic IP addresses, and its private DNS, IPv4 and any IPv6 addresses. Note: Public DNS and IPs change every time the instances is started.

  • Open the Amazon EC2 console at: https://console.aws.amazon.com/ec2/
  • In the navigation pane, choose Instances and then select the instance.
  • Choose Instance state > Stop instance. If this option is disabled, either the instance is already stopped or its root device is an instance store volume. It can take a few minutes for the instance to stop.
  • To start the stopped instance, select the instance, and choose Instance state > Start instance. It can take a few minutes for the instance to enter the running state.

Terminate an EC2 Instance

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html
Permanently deleting an instance is referred to as terminating an instance. As soon as the state of an instance changes to shutting-down or terminated, you stop incurring charges for that instance. You can't connect to or restart an instance after it has been terminated. However, you can launch additional instances using the same AMI.