Type something to search...
How to Install PHP and MariaDB on Amazon Linux 2

How to Install PHP and MariaDB on Amazon Linux 2

Introduction

We will learn how to set up PHP and MariaDB on Amazon Linux 2 in this tutorial. We will also discover how to set up PHP so that it functions with the Apache web server. We will also discover how to set up MariaDB so that it functions with PHP.

Prerequisites

To follow along with this tutorial, you will need:

  • An Amazon Linux 2 EC2 instance with a public IP address.
  • A non-root user with sudo privileges.
  • A domain name pointing to the public IP address of your EC2 instance.
  • Apache web server installed and running. How to Install PHP and MariaDB on Amazon Linux 2.

Installing PHP/MariaDB, setting up MariaDB, and running a basic PHP demo

Step 1 — Installing PHP

PHP is a free and open-source scripting language that is used to create dynamic web pages. It is the most popular web scripting language in the world.

At first, we will enable amazon-linux-extras so that we can specify the PHP version that we want to install.

Terminal window
sudo amazon-linux-extras enable php7.4 -y

Next, we will install PHP.

Terminal window
sudo yum install php php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap} -y

We will now verify that PHP has been installed.

Terminal window
php -v

The output should look like this:

Terminal window
PHP 7.4.30 (cli) (built: Jun 23 2022 20:19:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Step 2 — Installing MariaDB

MariaDB is a free and open-source relational database management system (RDBMS) that is used to store data for dynamic web pages. It is a fork of MySQL.

At first, we will install MariaDB.

Terminal window
sudo yum install mariadb-server -y

Next, we will start MariaDB.

Terminal window
sudo systemctl start mariadb

We will configure MariaDB so that it starts automatically when the system boots.

Terminal window
sudo systemctl enable mariadb

We will now secure MariaDB.

Terminal window
sudo mysql_secure_installation

You will be prompted to enter the current root password for MariaDB. Press Enter to continue.

Terminal window
Enter current password for root (enter for none):

Next, you will be prompted to set a new root password for MariaDB. Enter a new password and press Enter.

Terminal window
Set root password? [Y/n]

You will be prompted to remove anonymous users. Press Y and then press Enter.

Terminal window
Remove anonymous users? [Y/n]

You will be prompted to disable remote root login. Press Y and then press Enter.

Terminal window
Disallow root login remotely? [Y/n]

You will be prompted to remove the test database and access to it. Press Y and then press Enter.

Terminal window
Remove test database and access to it? [Y/n]

You will be prompted to reload the privilege tables now. Press Y and then press Enter.

Terminal window
Reload privilege tables now? [Y/n]

Step 3 — Configuring PHP to Work with Apache

At first, we need to restart Apache.

Terminal window
sudo systemctl restart httpd

We will create a directory for our PHP files.

Terminal window
sudo mkdir /var/www/html/php

Next, we will create a PHP file.

Terminal window
sudo vi /var/www/html/php/index.php

We will add the following content to the file.

1
<?php
2
phpinfo();

We will now open the file in a web browser.

Terminal window
http://your_domain_name/php/index.php

You should see the following output:

phpinfo

Step 4 — Configuring MariaDB to Work with PHP

At first, we will create a database for our PHP files.

Terminal window
sudo mysql -u root -p
1
CREATE DATABASE php;

Next, we will create a user for our PHP files.

1
CREATE USER 'php'@'localhost' IDENTIFIED BY 'password';

We will grant all privileges to the user.

1
GRANT ALL PRIVILEGES ON php.* TO 'php'@'localhost';

We will now exit MariaDB.

1
exit

We will create a PHP file.

Terminal window
sudo vi /var/www/html/php/db.php

We will add the following content to the file.

1
<?php
2
$db = new mysqli('localhost', 'php', 'password', 'php');
3
if ($db->connect_error) {
4
die('Connection failed: ' . $db->connect_error);
5
}
6
echo 'Connected successfully';

We will now open the file in a web browser.

Terminal window
http://your_domain_name/php/db.php

You should see the following output:

phpdb

Conclusion

In this tutorial, we learned how to set up PHP and MariaDB on Amazon Linux 2. We also learned how to set up PHP so that it functions with the Apache web server. We also learned how to set up MariaDB so that it functions with PHP.

Resources

Related Posts

Check out some of our other posts

How To Create A Custom VPC Using AWS CLI

How To Create A Custom VPC Using AWS CLI

Introduction In the sample that follows, an IPv4 CIDR block, a public subnet, and a private subnet are all created using AWS CLI instructions. You can run an instance in the public subnet and con

read more
How to Install and Setup FireWall on Amazon Linux 2

How to Install and Setup FireWall on Amazon Linux 2

Introduction We will learn how to install and setup FireWall on Amazon Linux 2 in this tutorial. We will also discover how to set up FireWall so that it functions with the Amazon Linux 2. Pre

read more
How to Install Apache Web Server on Amazon Linux 2

How to Install Apache Web Server on Amazon Linux 2

Introduction In this tutorial, we will learn how to install Apache web server on Amazon Linux 2. We will also learn how to configure Apache web server to run simple HTML web page. Prerequisit

read more
How to Install WordPress on Amazon Linux 2

How to Install WordPress on Amazon Linux 2

Introduction We will learn how to install WordPress on Amazon Linux 2 in this tutorial. We will also discover how to set up WordPress so that it functions with the Apache web server. We will also

read more
How To Create An AWS EC2 Instance Using AWS CLI

How To Create An AWS EC2 Instance Using AWS CLI

Introduction We will learn how to create an AWS EC2 instance using AWS CLI in this tutorial. We will also discover how to set up an AWS EC2 instance so that it functions with the Apache web serve

read more
How to Run an Apache Web Server Using Docker on an AWS EC2 Instance

How to Run an Apache Web Server Using Docker on an AWS EC2 Instance

Introduction In this post, we will learn how to run an Apache web server using Docker on an AWS EC2 instance. We will use the following tools:AWS EC2 [Docker]

read more