An elastic LAMP stack is a collection of open source software that are used to run dynamic websites and applications. The software components that make up the stack are Linux, Apache HTTP server, MySQL database and PHP or Perl or Python programming languages. Reference to a stack is made because of the dependence that exists in the layers. The operating system forms the base on which the HTTP server sits on. The database is the repository of the data served and one of the programming languages is used to enable data processing and user interaction.
In this tutorial the focus will be on demonstrating how to set up a LAMP stack on AWS EC2. To follow along the tutorial you need an AWS account, a SSH client, and basic knowledge of a terminal and SSH. Follow this link https://us-east-2.console.aws.amazon.com/console/ to sign in to your console. From the screen that appears select Launch a virtual machine as shown in the image below.
We would like to use an Amazon machine image (AMI) within the free tier so ensure the free tier only filter is selected.
Scroll down and select an Ubuntu AMI.
Select a free tier instance
Click on review and launch to get a chance to review instance settings. Once you are satisfied with your instance click on launch. Before you can launch your instance create a key pair to enable you to SSH into your instance. Download your key pair and store it safely.
To start your instance click on launch instance
To access your instance you need an SSH client. On a windows machine download and install PuTTY which is available here https://www.putty.org/. On Linux OpenSSH is available.
The pem key file provided by AWS is not supported in PuTTY so we need to convert pem to ppk format. This conversion is done using PuTTYgen utility. Open the utility, click on load and select the pem file you saved earlier. After loading click on save private key and specify a location where the key will be saved.
To connect to our instance we need to specify user name and public DNS of the instance in the format username@DNS. The user name of an Ubuntu AMI is ubuntu. The public DNS is part of an instance description.
Open PuTTY and provide user name and DNS as instructed earlier. Expand SSH field, click on auth, select the key you saved earlier and click on open.
Click on Yes when you get a security alert.
After a successful connection a command line session will be available
After connecting to your instance begin by updating the system using the command sudo apt-get update. The first requirement that we will install is Apache. This is completed using the command sudo apt-get install apache2.
After successful installation of Apache we check for syntax errors using the command sudo apache2ctl configtest. If you get syntax OK output as shown below you do not need to make any configuration changes. If you got any warnings you need to add a primary domain name to ServerName entry in main configuration file
We need to check HTTP and HTTPS traffic are allowed through the firewall. Use the command sudo ufw app list to check Apache application profile.
Check the ports that are allowed to receive traffic in Apache Full profile using the command sudo ufw app info “Apache Full”. The ports that allow traffic are 80 and 43.
To allow incoming traffic we use the command sudo ufw allow in “Apache Full”.
The second requirement is installing MySQL database using the command sudo apt-get install mysql-server. During installation you will be prompted to enter a password.
After installation we need to run a script to improve the security of our database. The command is mysql_secure_installation.
During database securing process you will be requested to enable features aimed at improving database security. The first feature you will be requested to enable is VALIDATE PASSWORD PLUGIN. Enabling this feature will lead to rejection of passwords that do not meet a set criteria. The second feature you will be required to set is the security level which can be low, medium or strong. The other features you will be requested to enable are removing anonymous users and test database, disabling remote root logins and reloading changes so they can be immediately effected.
To install PHP we use the command sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql.
When requests are sent to server Apache will prefer to serve index.html. It is preferable to change this behavior so index.php file is served first. To make this change we need to edit dir.conf so index.php is in the first position. Open dir.conf using the command sudo nano /etc/apache2/mods-enabled/dir.conf.
To effect the changes use the command sudo systemctl restart apache2.
After effecting changes you have a working LAMP stack. To test the configuration is working we can place a php file that will display web server information on web root directory. Use the command sudo nano /var/www/html/test.php to create the file. After creating the file add the code snippet below
Use the link http://instance_address/test.php to check your stack is correctly configured.
After setting up a LAMP stack there are other useful utilities that can be installed. One of those is phpMyAdmin which is installed using the command sudo apt-get install phpmyadmin php-mbstring php-gettext
We need to enable apache2 server and select yes to the prompt whether to use dbconfig-common. To complete set up we need to enable mcrypt and mbstring using the commands below
sudo phpenmod mcrypt
sudo phpenmod mbstring
Restart Apache using the command sudo systemctl restart apache2
The objective of this tutorial was how to set up a LAMP stack. Creating an instance and downloading a key pair was demonstrated. Converting a key pair and connecting to an instance using PuTTY SSH client was demonstrated. Installing Apache, MySQL, PHP and phpMyAdmin was demonstrated.