LAMP Stack Installation on Amazon Linux 2023

Ayaan BordoloiAyaan Bordoloi
6 min read

Introduction

"LAMP" stack is a group of four different open-source software technologies used to build websites and web applications. "LAMP" stands for Linux OS; Apache web server; MySQL database server; and PHP programming language.

In this blog, we will install the LAMP stack on Amazon Linux 2023.

Launching an EC2 instance

The first step of this tutorial is to launch an EC2 instance using Amazon Linux 2023.

To do so, follow the steps below-

  1. Log in to your AWS console and navigate to the EC2 service.

  2. Click on "Launch instance".

  3. Name your instance

  4. Choose your AMI as Amazon Linux 2023

  5. Choose your instance type as t2.micro and create a new key pair of the RSA type in .pem format.

  6. In the Network Settings, create a new security group and leave the network settings at their default values.

  7. Choose the storage as 8 GiB and click "Launch instance."

Connect to your EC2 instance

After launching the Amazon Linux 2023 instance we need to connect to it through a SSH client.

Open your command terminal and run the following command to connect to your ec2 instance.

ssh -i Downloads/[securitykey].pem ec2-user@[ip address of your instance]

You will find the public IPV4 address of your instance in the details section of your running instance.

After connecting to your instance you should see the following-

Now that we are done creating and connecting to our instance, we will proceed with the rest of the LAMP tutorial.

Step 1: Installing the LAMP software packages

  1. First, we will need to ensure that all our software packages have been updated using the following command.

     [ec2-user ~]$ sudo dnf update -y
    

    The -y option installs the updates without asking for confirmation.

  2. We will install the latest versions of the Apache web server and PHP packages.

    
     [ec2-user ~]$ sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
    
  3. Install the MariaDB software packages.

     [ec2-user ~]$ sudo dnf install mariadb105-server
    

    dnf install command is used to install multiple software packages and all the related dependencies at the same time.

Step 2: Starting Apache web server & Updating Firewall

  1. Start the Apache web server with the following command.

     [ec2-user ~]$ sudo systemctl start httpd
    
  2. Enable the Apache web server to start at each system boot.

     [ec2-user ~]$ sudo systemctl enable httpd
    
  3. Updating Firewall:

    • Go to instances and click on your running EC2 instance.

    • Below, you will see the security options. Click on them and proceed thereafter.

      Click on the security groups shown above.

    • Click on Edit inbound rules.

    • Add the HTTP rule and save it to allow inbound HTTP (port 80) connections to your instance

  4. To check if the Firewall has been updated to allow inbound HTTP connections, open a new tab in your browser and paste the IPV4 address of your instance.

    You should see the following -

Step 3: Adding EC2-user to Apache Group

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.

To allow the ec2-user account to manipulate files in this directory, we must modify the ownership and permissions of the directory.

In this tutorial, we add ec2-user to the apache group to give the apache group's ownership of the /var/www directory and assign write permissions to the group.

  1. Add ec2-user to the apache group.

     [ec2-user ~]$ sudo usermod -a -G apache ec2-user
    
  2. Log out and then log back in again to verify.

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

        [ec2-user ~]$ exit
      
    • To verify reconnect to your instance, and then run the following command:

        [ec2-user ~]$ groups
        ec2-user adm wheel apache systemd-journal
      
  3. Change the group ownership of /var/www and its contents to the apache group.

     [ec2-user ~]$ sudo chown -R ec2-user:apache /var/www
    
  4. Add group write permissions and set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.

     [ec2-user ~]$ sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
    
  5. Add group write permissions, recursively change the file permissions of /var/www and its subdirectories:

     [ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;
    

    Now, ec2-user can add, delete, and edit files in the Apache document root.

Step 4: Testing the LAMP server

Now that our server is running and we have added the ec2-user to the Apache group, we should be able to create PHP files in the /var/www/html directory that is available from the internet.

  1. Create a PHP file in the Apache document root using the following command.

     [ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
    
  2. 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:

     http://ec2-44-204-189-254.compute-1.amazonaws.com/phpinfo.php
    

    Note: ec2-44-204-189-254.compute-1.amazonaws.com is my public DNS address. You will find your own DNS address in your running instance.

  3. You should see the following page:

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

     [ec2-user ~]$ rm /var/www/html/phpinfo.php
    

    Now you have a fully functional LAMP server.

Step 5: Securing Database server

The command mysql_secure_installation guides you in configuring a root password and eliminating any vulnerable elements from your installation.

  1. Start the MariaDB server.

     [ec2-user ~]$ sudo systemctl start mariadb
    
  2. Run mysql_secure_installation.

     [ec2-user ~]$ sudo mysql_secure_installation
    
    • When prompted for a password press ENTER since by default, the root account does not have a password set.

    • Type Y to set a password, and type a secure password twice.

    • Type Y for all the questions asked thereafter.

We have successfully secured the database in our LAMP server.

Conclusion

With the help of this blog, we have effectively completed the installation process for all the essential software components needed to construct the LAMP stack on Amazon Linux 2023. By having the LAMP stack in place, we have everything we need to start creating a web application.

In case you have any questions regarding this article, please feel free to comment in the comments section.

I would like to give a shoutout to the Guys in the Cloud team & Rushabh Mahale for all the support and guidance.

2
Subscribe to my newsletter

Read articles from Ayaan Bordoloi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayaan Bordoloi
Ayaan Bordoloi

Studying Electronics and Communication Engineering at Chandigarh Engineering College. Learning Devops and Data Structures & Algorithms. Exploring new technologies and blogging.