loader
Prerequisites

Before beginning:

  1. Launch an AL2023 instance on Amazon EC2 with a public DNS.
  2. Allow SSH (port 22), HTTP (port 80), and HTTPS (port 443) in your security group.
Step 1: Prepare Your LAMP Server

 

1. Connect to Your Instance

Use an SSH client (like PuTTY) to connect to your EC2 instance.

2. Update Software

Run the following command to ensure all packages are up to date:

sudo dnf upgrade -y
3. Install Apache and PHP

Install Apache and the latest PHP version (8.1) with this command:

sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
4. Install MariaDB

Install MariaDB, the database server:

sudo dnf install mariadb105-server
5. Start Apache

Start the Apache web server:

sudo systemctl start httpd

Enable Apache to start on boot:

sudo systemctl enable httpd
6. Check HTTP Port Access

Make sure your security group allows inbound HTTP (port 80) traffic. Update rules in the EC2 console if necessary.

Step 2: Set Up File Permissions

To manage files in Apache’s root directory (/var/www/html):

  1. Add the ec2-user to the apache group:

sudo usermod -a -G apache ec2-user

2. Log out and log back in to apply the changes:

exit

3. Verify group membership:

groups

You should see apache listed.

4. Change ownership and permissions of /var/www:

sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
Step 3: Test Your Web Server
  1. Open your instance’s public DNS in a browser. If the /var/www/html directory is empty, you should see Apache’s test page: “It works!”

  2. Add a test PHP file to verify PHP functionality:

echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

3. Open the file in your browser:

http://<your-public-DNS>/phpinfo.php

You should see a PHP information page.