In this article step-by-step process is discussed to setup LAMP stack in AWS EC2. This will include :
- Creating an EC2 instance
- Connection to the instance from your system
- Install Ubuntu 18.04
- Install Apache 2.4
- Install PHP 7.2
- Install MySql 5.7
- Install phpMyAdmin 4
- Setting up FTP/SFTP access
AWS EC2 Pricing
Under the Amazon Free-Tier, you can use an EC2 instance for a year, free of cost. After an year, the instance is charged hourly depending upon the type of instance.
Working Environment for this Tutorial
This tutorial uses an Ubuntu system. The steps will work for all flavours of Linux, and also for Mac.
For Windows, the steps are almost the same, the only difference being you need to use PuTTY for SHH connection (rather than command line on Linux systems). See Step 6 below for more.
Step 1 – Launch an AWS EC2 Virtual Machine
Log into the AWS EC2 Management Console, and click on the button Launch Instance
Step 2 – Choosing the OS and Hardware
Clicking on the option from Step 1 takes to the below page. Here you get to choose the OS for the virtual machine and also the other hardware. Select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type.
In the next screen choose the instance type. In this tutorial, t2.micro instance is selected as it is free-tier eligible. Single processor core and 1 GB memory is good enough for general purposes.
Step 3 – Editing Some Basic Configurations
Go for the default settings for Configure Instance, Add Storage and Add Tags screens.
However add 2 rules under Configure Security Group. They are SSH and HTTP.
SSH is required to login to the virtual machine using command line. This is already added to the security group.
HTTP is for the web server that will be installed shortly. To add HTTP rule, click on Add Rule & choose HTTP from the dropdown.
As per AWS documentation, security groups acts as a virtual firewall. In short you can set up the rules regarding IP addresses that can access the added protocols. For the tutorial, both the protocols are open to all IP addresses. But you can also restrict some IP addresses from accessing all protocols. This can be edited later also.
Clicking Review and Launch button in this page takes us to a page where you can review our instance settings. Click on the Launch button.
Step 4 – Creating a Key Pair
The next page will present with options for Key file. This key file is used to connect to the EC2 instance from your system.
Choose the option Create a new key pair and download the key file.
With the key file downloaded, the button for launching the instance became active. And clicking that button is the last step to launch the instance in AWS.
With the successful launch of the instance, you will be redirected to the page which gives a success message that instance has been launched.
Click on the button View Instances and you will be redirected to the EC2 management console.
Step 5 – Locking the Key File
The permission for the downloaded key file needs to be changed so that it is not publicly viewable for SSH to work. It is achieved by :
sudo chmod 400 path/to/key/file
Step 6 – Connecting to Instance
The key file that was downloaded will now be used to connect to the EC2 instance from our system via SSH. In this tutorial a Linux system is used, so we are using the Linux terminal for SSH access.
(For SSH access with Windows see Connecting to Your Linux Instance from Windows Using PuTTY)
- SSH to the instance using the key file and hostname. The hostname here is generated by combining the EC2 username (which is ubuntu for Ubuntu 18.04) with the public DNS Name with a @ character.Example: email@example.comThe following command is used for SSH :
sudo ssh -i "path/to/key/file" ec2-hostnameReplace ec2-hostname with the hostname of the instance.
- Type yes if it gives a connection warning.
- Upon a successful connection, you will see the below screen :
Now you can start installing the necessary software on this EC2 instance.
Step 7 – Installing Apache
- Before installing Apache, you need to update the package list for upgrades and new packages.
sudo apt-get update
- Now Apache can be installed.
sudo apt-get install apache2
- Following the installation of Apache, if you navigate to the public IP of the instance (that can be found from the EC2 Console) from a browser, you will see the familiar Apache Welcome Page. This implies Apache is running perfectly.
Step 8 – Installing MySql
- Install MySql Server
sudo apt-get install mysql-server
- After MySql has been installed, you will need to set the root password of the database and secure it using the following command :
- You will be presented a screen where MySQL asks whether you would like to activate the VALIDATE PASSWORD PLUGIN. For now, keeping things simple, type no.
- In the next type the root password of your choice. Confirm it again.
- In the next screen MySql will ask whether to remove anonymous users. Type yes
- Disallow root login remotely? Type No
- Remove test database and access to it? Type Yes
- Reload privilege tables now? Type Yes
- After the password has been set you can check the whether MySQL is working correctly by logging into the database with the command :
sudo mysql -u root -pPassword is the same that was set in the previous step.If everything went smoothly, you will be welcomed by the mysql prompt.
- Type exit to get out of MySql
Step 9 – Installing PHP with Common Extensions
- Now install PHP and commonly used PHP extensions by using the following command :
sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring
- After PHP has been installed, restart Apache.
sudo service apache2 restart
Step 10 – Installing phpMyAdmin
To install phpMyAdmin execute the following command :
sudo apt install phpmyadmin
Following which you will be presented with options to configure phpmyadmin for the server :
- The first option is to select the proper web server. It is important to note here that although apache2 is highlighted it hasn’t been selected.Select apache2 by pressing the space-bar. Then press Tab key that takes us to the Ok button. Now press enter.
- Next option is to configure the database for phpmyadmin with dbconfig-common. Select the Yes option here.
- Then password is asked for login to phpmyadmin. By default the username is phpmyadmin. You are now setting the password for this user (this is different from the roor user).Press Tab key to go the Ok button.
- Now phpMyAdmin can be accessed from the browser by navigating to
http://[SERVER_PUBLIC_IP]/phpmyadminEnter the login credentials with user phpmyadmin and the associated password.
Step 11 – Allowing Mysql Root Login through phpMyAdmin
This step is optional.
By default you cannot login as root user through phpMyAdmin. To do this perform the below steps :
- Login to MySql root.
sudo mysql -u root -p
- Enter the query to change root login authentication from socket to password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Replace password by the root password you entered while installing MySql.
- Enter the query to put the changes into effect.
Now you can login as root from phpMyAdmin.
Step 12 – Connecting Instance with FTP Client
Very often you will need to use an FTP client to upload and edit codes to the instance. Filezilla is covered here, however the process will be almost similar for any other FTP/SFTP client.
- Open Filezilla. Under File > Site Manager setup the credentials needed to upload files.
- Under Host, provide the Public IP Address of the instance.
- For Protocol, select SFTP.
- For Logon Type, select the option Key file.
- The User is ubuntu.
- For Key file, provide the path to the key file
When the Connect button is clicked, Filezilla attempts to connect to the instance. An “Unknown Host Key” window pops up, click Ok here.
- You will be taken to default directory – /home/ubuntu. You can enter /var/www/html to go to the web server directory.
- Now the the ownership and access modes of the server directory /var/www/html needs to be changed from root to ubuntu. Otherwise you will be unable to write files through FTP.
sudo chown -R ubuntu /var/www/html sudo chmod -R 755 /var/www/html
Following these two commands you can create and upload files from FileZilla to our server directory in the instance.
- For testing you can create an index.php file in the server directory and write a simple PHP code to test the installation.
<?php echo phpinfo(); ?>
Now if you navigate to the public IP address, you should see the familiar page.
This implies that the PHP installation is working perfectly.
Setup is Finished
Congratulations! You have a working EC2 instance with LAMP stack.
In case of any problems during installation, you can always terminate that instance and start a new one.
For any issues, you can comment it below. We will try to solve it.