LAMP (Linux, Apache, MySQL, PHP) web server on an Amazon EC2 Linux instance

Share This:
AWS Amazon Linux 2

This post will guide you through the creation of an AWS EC2 Linux instance, the installation and configuration of a LAMP web server within that instance, This guide assumes the developer already has an active AWS account.

Launching your AWS Instance

To start with you need to create an EC2 Linux instance via Amazon Web Services. Log in to your AWS account and from the web console chose EC2 under the Compute heading.

AWS Dashboard Compute EC2

From within the EC2 dashboard, you need to select Launch Instance.

Launch EC2 Instance

A setup wizard should begin in which the user selects an Amazon Machine Image or AMI and then configures it. First, Step 1 allows the user to select the image. Choose the option titled Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type — ami-6b8cef13 (Latest as of writing this post). It should be covered under the free tier choices.

Next, Step 2 prompts the user to choose an Instance Type. Choose the version titled “t2.micro” under the “Type” column. It should have the subtext stating it is “Free tier eligible”. It may be selected by default. Once the Instance Type is selected, click “Next: Configure Instance Details” down in the bottom right of the wizard.

Skip Step 4 and Step 5 by clicking the Next button down at the bottom right of the page, until you arrive at Step 6. Configure Security Group. From here we need to open some ports for our LAMP web server. The SSH rule should be added by default, if not, select SSH from the drop-down menu under Type, then under the source column, choose “Anywhere”. Next, choose add rule, then select HTTP from the Type drop-down menu and again “Anywhere” under the source column. After that, select Add Rule again, and this time chose HTTPS from the Type drop-down menu and again choose “Anywhere” as the Source. Finally, add the last rule by choosing MYSQL/AURORA from the Type drop-down menu and “Anywhere” under source. Check that your security group configuration looks similar to the screenshot below before choosing “Review and Launch”.

AWS Security Groups

On the next screen review your security group one last time and ensure it matches the settings outlined above. Once everything looks fine choose Launch.

Once “Launch” is selected, the user is prompted to create a new Key Pair group. To this choose “Create a new key pair”. Under “Key Pair Name” type in a name of your choosing, then click the “Download Key Pair” button. You will be then prompted to save this key. Make sure to save this somewhere safe, but also somewhere you will remember. Amazon does not allow users to log into machines via a username and passwords, instead, you will use keys. So it’s important not to misplace or lose the key. Once that is completed and your key is saved select launch instance.

AWS Key Pair

The instance can take a while to configure, but once its finished select View Instances from the bottom right to view your own instances and some of their configurations. At the moment their should only be one instance present. It make take a few minutes for your machine to instantiate, when its ready to go, it will display a green dot beside the word running under the “Instance State” column.

Install and start the LAMP webserver

If you not already, connect to your instance.

To ensure that all of your software packages are up to date, perform a quick software update on your instance. This process may take a few minutes, but it is important to make sure that you have the latest security updates and bug fixes. The -y option installs the updates without asking for confirmation. If you would like to examine the updates before installing, you can omit this option.

sudo yum update -y

Now that your instance is current, you can install the Apache web server, MySQL, and PHP software packages.

sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd

Start the Apache web server.

sudo service httpd start

Test your web server. In a web browser, type the public DNS address (or the public IP address) of your instance. If there is no content in /var/www/html, you should see the Apache test page. You can get the public DNS for your instance using the Amazon EC2 console (check the Public DNS column; if this column is hidden, choose Show/Hide Columns (the gear-shaped icon) and choose Public DNS).

Apache httpd serves files that are kept in a directory called the Apache document root. The Amazon Linux Apache document root is /var/www/html, which by default is owned by root.

ls -l /var/www

To allow the ec2-user account to manipulate files in this directory, you must modify the ownership and permissions of the directory. There are many ways to accomplish this task. In this tutorial, you add the ec2-user user to the apache group, to give the apache group ownership of the /var/www directory and assign write permissions to the group.

Setting file permissions

Add your user (in this case, ec2-user) to the apache group.

sudo usermod -a -G apache ec2-user

Log out and then log back in again to pick up the new group, and then verify your membership.

Log out (use the exit command or close the terminal window):


To verify your membership in the apache group, reconnect to your instance, and then run the following command:


Change the group ownership of /var/www and its contents to the apache group.

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

To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.

sudo chmod 2775 /var/www


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

To add group write permissions, recursively change the file permissions of /var/www and its subdirectories:

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

Now, the ec2-user user (and any future members of the apache group) can add, delete, and edit files in the Apache document root. Now you are ready to add content, such as a static website or a PHP application.

Test your LAMP server

Create a PHP file in the Apache document root.

echo "<?php phpinfo(); ?>" > /opt/bitnami/apps/wordpress/htdocs/phpinfo.php

If you get a “Permission denied” error when trying to run this command, try logging out and logging back in again to pick up the proper group permissions that you configured in Set File Permissions.

In a web browser, type the URL of the file that you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example:

Where my.public.dns.amazonaws.comis your own public DNS.

You should see the PHP information page:

PHP Info

If you do not see this page, verify that the /opt/bitnami/apps/wordpress/htdocs/phpinfo.php file was created properly in the previous step. You can also verify that all of the required packages were installed with the following command. The package versions in the second column do not need to match this example output.

sudo yum list installed httpd24 php70 mysql56-server php70-mysqlnd

Delete the phpinfo.php file. Although this can be useful information, it should not be broadcast to the internet for security reasons.

rm /opt/bitnami/apps/wordpress/htdocs/phpinfo.php

Secure the database server

Start the MySQL server.

sudo service mysqld start

Run mysql_secure_installation.

sudo mysql_secure_installation

When prompted, type a password for the root account. Type the current root password. By default, the root account does not have a password set. Press Enter. Type Y to set a password, and type a secure password twice. Make sure the password chosen here will be one your remember, it will be used later to connect your LAMP application to the MySQL database.

  1. When prompted press y to remove anonymous user accounts.
  2. Type n to allow remote root login.
  3. Type Y to remove the test database.
  4. Type Y to reload the privilege tables and save your changes.

You have successfully installed LAMP stack on Amazon Linux 2.

Share This:

Popular on WPSteam Right Now!

Leave a Reply

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

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 22 other subscribers

I need help with...

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 22 other subscribers

I need help with...