Saturday, 9 September 2023

Tutorial blog to install an Apache web server with PHP and MariaDB on Amazon EC2 Linux instance.

 

Tutorial blog to install an Apache web server with PHP and MariaDB on Amazon EC2 Linux instance.

 

Learning Objective:

  • Learn to install an Apache web server, PHP and MariaDB.

Step 1:

Launch an Amazon EC2 Linux instance using Amazon Linux 2023 AMI. Refer to our tutorial blog to create the EC2 instance.

Step 2:

Change the security group from My Linux-SG to My WebServer-SG. Refer to the following screenshots for this change.


­Step 3:

Connect to the EC2 instance and install an Apache web server, PHP and MariaDB software packages as follows.

a) Run the following command to ensure all your software packages are up to date.

sudo dnf update –y

b) Install the Apache web server and PHP packages.

sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel

c) Install the MariaDB packages with the following demand:

sudo dnf install mariadb105-server

d) Start the Apache web server.

sudo systemctl start httpd

e) Use the systemctl command to configure the Apache web server to start at each system boot.

sudo systemctl enable httpd

Step 4:

Test the web server using public IPv4 address.

Step 5:

Set file permissions.

a) Add user (ec2-user) to the apache group using following command.

sudo usermod -a -G apache ec2-user

b) Log out and then log back in again to pick up the new group, and then verify your membership.

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

exit

ii) Reload page to restart server and to verify your membership in the apache group, reconnect to your instance, and then run the following command:

groups

c) Change the group ownership of /var/www and its contents to the apache group.

sudo chown -R ec2-user:apache /var/www

d) To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.

sudo chmod 2775 /var/www && find /var/www -type d -exec sudochmod 2775 {} \;

e) To add group write permissions, recursively change the file permissions of /var/www and its subdirectories:

find /var/www -type f -exec sudo chmod 0664 {} \;

Step 6:

Create a PHP file in the Apache document root.

a) Give the following commands:

cd /var/www/html

vi index.php

b) Copy the below code to the opened file:

<?php

print "Hello! from your most complicated PHP Application \n";

?>

c) Save the opened file using

:wq

d) Do an ls to show file presence

Step 7:

Test the server using following command: (The blue portion is supposed to be your IP address)

http://ip address/index.php

Step 8:

Secure MariaDB server.

a) Start the MariaDB server.

sudo systemctl start mariadb

b) Run mysql_secure_installation.

sudo mysql_secure_installation

a) Type current root password, which is by default nothing, so press Enter.

b) Press n to unix_socket.

c) Type Y to set a password and type it twice to confirm it.

d) Press Y to all the following questions till installation is over.

Step 9:

Give following command to check if database is running:

sudo systemctl status mariadb

Note: If you no longer need this instance make sure to terminate the instance. Click on the drop-down menu beside the actions button. Select the Instance State and click on Terminate. This will terminate your instance.