DEV Community

Oluwaseun Olaleye
Oluwaseun Olaleye

Posted on

πŸ“˜ LAMP Stack Implementation on AWS (EC2) > Linux | Apache | MySQL | PHP

🧭 Introduction

The LAMP stack is a popular open-source web development platform used for hosting dynamic websites and web applications. It consists of four main components:

  • Linux: The operating system
  • Apache: The web server
  • MySQL: The relational database management system
  • PHP: The programming language (can also be replaced with Python or Perl)

This guide provides step-by-step instructions on how to set up and configure a LAMP stack on an AWS EC2 instance running Ubuntu 24.04 LTS.

πŸ”§ Step 0: Preparing the Environment

Before we install the LAMP stack, we need to launch and prepare an EC2 instance.

βœ… Tasks:

  1. Launch EC2 Instance:
    • Log in to your AWS Management Console.
    • Navigate to EC2 > Instances > Launch Instance.
    • Choose an Ubuntu 24.04 LTS AMI.
    • Select the t2.micro instance type (Free Tier eligible).
    • Choose a region closest to your location (e.g., eu-west-2a).
    • Create a new key pair or use an existing one to SSH into the server.
    • Open necessary ports (SSH - 22, HTTP - 80) in the Security Group.

  1. Connect to the Instance: Use the .pem file and connect via SSH:
   ssh -i "your-key-name.pem" ubuntu@<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode

Replace your-key-name.pem with your actual key file and with the public IP of your instance.

βš™οΈ Step 1: Update the Package Manager

After connecting to your EC2 instance, it is recommended to update the package manager to ensure that all packages are up to date.

🌐 Step 2: Install Apache2

Apache is a widely used open-source web server software that will serve your web pages to users over the internet.

βš™οΈ Enable and Check Apache Service

To ensure Apache starts automatically on system boot and to verify that it is running properly, use the following commands:

🌍 Step 3: Test Apache Externally (From Browser)
To confirm that Apache is accessible over the internet, open your browser and visit the public IP of your EC2 instance:
http://:80
πŸ” Example:
http://13.53.216.202:80
You should see the Apache2 Ubuntu Default Page, indicating that your server is successfully serving web pages.

πŸ—„οΈ Step 2: Install MySQL

To handle data for your web application, you need a Relational Database Management System (RDBMS).

In our case, we will install MySQL. MySQL is a widely used, open-source RDBMS that integrates seamlessly in PHP-based environments.


sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

The -y flag automatically confirms the installation prompt.

Once installed, you can proceed to secure the database and start using it to create users, schemas, and tables for your web application.

πŸ” Step 3: Log in to MySQL Console

To access the MySQL server as the administrative root user, run the following command:

sudo mysql
Enter fullscreen mode Exit fullscreen mode

Running this command connects your shell to the MySQL server as the root user, thanks to the elevated privileges granted by sudo

πŸ”‘ Step 4: Set a Password for the Root User

Once inside the MySQL shell, assign a password to the root user and configure the authentication method to mysql_native_password.
or the purpose of this tutorial, we'll use PassWord.1 as the password.

πŸ” Step 5: Log In to MySQL Console Using Root Password

After assigning a password to the root user, you should now log in using that password to verify access.

sudo mysql -p
Enter fullscreen mode Exit fullscreen mode

The -p flag tells MySQL to prompt you for a password. You should see output like this:

Step 6 - Install PHP

  1. Install php. So far, we have installed Apache to serve our web contents, and we installed Mysql to assist us store and manage our data. Now, we will install Php to process codes inorder to display dynamic content to the consumer user.

To set up php on our server, we are going to need the following installed:

php package
php-mysql (this is a PHP module that allows PHP to communicate with MySQL databases)
libapache2-mod-php _(this helps Apache to handle and understand PHP files) to get this all set up in the machine, run:

Confirm the PHP version by running:

php -v
Enter fullscreen mode Exit fullscreen mode

Step 7 - Create a virtual host

  1. _First, create a document directory for the new website you are about to create near the default web dir (/var/www/html).
sudo mkdir /var/www/projectlamp
Enter fullscreen mode Exit fullscreen mode

Assign the ownership of the directory to the current user in the session

sudo chown -R $USER:$USER /var/www/projectlamp
Enter fullscreen mode Exit fullscreen mode

2. Create a configuration file for your new website

sudo vim /etc/apache2/sites-available/projectlamp.conf
Enter fullscreen mode Exit fullscreen mode
<VirtualHost *:80>
    ServerName projectlamp
    ServerAlias www.projectlamp 
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/projectlamp
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Our site is ready, but Apache needs to serve content from the web root (i.e: /var/www/my_project_lamp), but it is empty. So we will now create an index.html file and then try to access our site via our public IP to test it

sudo echo 'Hello LAMP from hostname' $(TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-hostname) 'with public IP' $(TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectlamp/index.html
Enter fullscreen mode Exit fullscreen mode

8. Try to access your new website using your server's public IP

http://13.51.242.66
Enter fullscreen mode Exit fullscreen mode

Step 5 - Enable PHP on the website.

sudo vi /etc/apache2/mods-enabled/dir.conf
Enter fullscreen mode Exit fullscreen mode

  1. Reload Apache You need to reload Apache so that the changes you have made would apply.
sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

3. Utilize the php test script to ensure that you have configured Apache to handle and process requests for PHP files.

Create a new file inside the custom web root folder (/var/www/my_project_lamp)

vi /var/www/projectlamp/index.php
Enter fullscreen mode Exit fullscreen mode

4. Refresh the page and note down any change you observe

The page served here provides you with information about the server from the perspective of PHP. This is very useful in terms of debugging and to make sure that all necessary settings are applied correctly.

When you are done checking the information about the server through this page, It is very important that you remove the file you have created (i.e: index.php) so as to protect the sensitive information about the Php enviroment and the server. This can be repeated when necessary if the information is needed.

sudo rm /var/www/projectlamp/index.php
Enter fullscreen mode Exit fullscreen mode

Conclusion

If you are considering/evaluating what stack to utilize in deploying a website and make it accessible to many people, the LAMP stack we just experienced is a very flexible, robust, and efficient stack to assist you in deploying your developed web applications.

To know how to set up, configure, and maintain a LAMP environment, kindly go through the guidelines stipulated in this documentation and it should help you know how to get the job done.

Top comments (0)