How to install Apache php on Amazon Linux 2023

Step 1: Connect to your Amazon Linux Instance To get started, you need to connect to your Amazon Linux instance via SSH. Open a terminal window and type the following command:

ssh ec2-user@your-instance-public-ip

Replace “your-instance-public-ip” with the public IP address of your Amazon Linux instance.

Step 2: Update the Package Repository Before installing Apache and PHP, it’s a good practice to update the package repository on your Amazon Linux instance. To do this, run the following command:

sudo dnf update -y

This will update all the packages on your instance to the latest version.

Step 3: Install Apache and PHP Next, we need to install Apache and PHP on Amazon Linux 2023. To do this, run the following command:

sudo dnf install httpd mod_ssl php php-mysqlnd -y

This command will install Apache, PHP, and the PHP MySQL extension, which is required to connect to a MySQL database.

Step 4: Start Apache Once the installation is complete, start the Apache service using the following command:

sudo systemctl start httpd

This command will start the Apache service on your Amazon Linux instance.

Step 5: Verify Apache Installation To verify that Apache is installed and running correctly, use the following command to check the status of the Apache service:

sudo systemctl status httpd

If Apache is running correctly, you will see output similar to the following:

bashCopy code● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-04-28 14:29:09 UTC; 1 day 8h ago
 Main PID: 30676 (httpd)
   CGroup: /system.slice/httpd.service
           ├─30676 /usr/sbin/httpd -DFOREGROUND
           ├─30677 /usr/sbin/httpd -DFOREGROUND
           ├─30678 /usr/sbin/httpd -DFOREGROUND
           ├─30679 /usr/sbin/httpd -DFOREGROUND
           ├─30680 /usr/sbin/httpd -DFOREGROUND
           └─30681 /usr/sbin/httpd -DFOREGROUND

Step 6: Enable Apache at Boot To ensure that Apache starts automatically when your Amazon Linux instance boots up, use the following command:

sudo systemctl enable httpd

This command will enable Apache to start automatically when your instance starts up.

Step 7: Test PHP To test that PHP is installed and working correctly, create a PHP test file in the Apache document root directory using the following command:

sudo nano /var/www/html/info.php

Add the following code to the file:

<?php
phpinfo();
?>

Save and close the file.

Step 8: Test the PHP Test File Open a web browser and navigate to your Amazon Linux instance’s public IP address followed by /info.php. For example, if your instance’s public IP address is 12.34.56.78, enter http://12.34.56.78/info.php in your web browser’s address bar.

If everything is working correctly, you should see a page with information about your PHP installation.

SSL Setup

To install an SSL certificate use Letsencrypt. To do that, first, install Certbot

sudo dnf install python3 augeas-libs

Then Set up a Python virtual environment

sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip

Run this command on the command line on the machine to install Certbot.

sudo /opt/certbot/bin/pip install certbot certbot-apache

Execute the following instruction on the command line on the machine to ensure that the certbot command can be run.

sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.

sudo certbot --apache

That’s it! You have successfully installed Apache and PHP on Amazon Linux 2023. You can now use Apache and PHP to host websites and web applications on your Amazon Linux instance.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply