DEV Community

Cover image for How to Install PHP and MariaDB on Amazon Linux 2
Mohammad Abu Mattar
Mohammad Abu Mattar

Posted on

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 Apache Web Server 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.

sudo amazon-linux-extras enable php7.4 -y
Enter fullscreen mode Exit fullscreen mode

Next, we will install PHP.

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

We will now verify that PHP has been installed.

php -v
Enter fullscreen mode Exit fullscreen mode

The output should look like this:

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
Enter fullscreen mode Exit fullscreen mode

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.

sudo yum install mariadb-server -y
Enter fullscreen mode Exit fullscreen mode

Next, we will start MariaDB.

sudo systemctl start mariadb
Enter fullscreen mode Exit fullscreen mode

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

sudo systemctl enable mariadb
Enter fullscreen mode Exit fullscreen mode

We will now secure MariaDB.

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

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

Enter current password for root (enter for none):
Enter fullscreen mode Exit fullscreen mode

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

Set root password? [Y/n]
Enter fullscreen mode Exit fullscreen mode

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

Remove anonymous users? [Y/n]
Enter fullscreen mode Exit fullscreen mode

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

Disallow root login remotely? [Y/n]
Enter fullscreen mode Exit fullscreen mode

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

Remove test database and access to it? [Y/n]
Enter fullscreen mode Exit fullscreen mode

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

Reload privilege tables now? [Y/n]
Enter fullscreen mode Exit fullscreen mode

Step 3 — Configuring PHP to Work with Apache

At first, we need to restart Apache.

sudo systemctl restart httpd
Enter fullscreen mode Exit fullscreen mode

We will create a directory for our PHP files.

sudo mkdir /var/www/html/php
Enter fullscreen mode Exit fullscreen mode

Next, we will create a PHP file.

sudo vi /var/www/html/php/index.php
Enter fullscreen mode Exit fullscreen mode

We will add the following content to the file.

<?php
  phpinfo();
Enter fullscreen mode Exit fullscreen mode

We will now open the file in a web browser.

http://your_domain_name/php/index.php
Enter fullscreen mode Exit fullscreen mode

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.

sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode
CREATE DATABASE php;
Enter fullscreen mode Exit fullscreen mode

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

CREATE USER 'php'@'localhost' IDENTIFIED BY 'password';
Enter fullscreen mode Exit fullscreen mode

We will grant all privileges to the user.

GRANT ALL PRIVILEGES ON php.* TO 'php'@'localhost';
Enter fullscreen mode Exit fullscreen mode

We will now exit MariaDB.

exit
Enter fullscreen mode Exit fullscreen mode

We will create a PHP file.

sudo vi /var/www/html/php/db.php
Enter fullscreen mode Exit fullscreen mode

We will add the following content to the file.

<?php
  $db = new mysqli('localhost', 'php', 'password', 'php');
  if ($db->connect_error) {
    die('Connection failed: ' . $db->connect_error);
  }
  echo 'Connected successfully';
Enter fullscreen mode Exit fullscreen mode

We will now open the file in a web browser.

http://your_domain_name/php/db.php
Enter fullscreen mode Exit fullscreen mode

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

Top comments (3)

Collapse
 
abdrehman98 profile image
Abdul Rehman

Hi @mkabumattar

In step 3 content of index.php should be:

<?php

phpinfo( );

?>

Collapse
 
mkabumattar profile image
Mohammad Abu Mattar

Hi, @abdrehman98

It is generally recommended to include the closing ?> at the end of a PHP script to prevent accidental whitespace or newline characters from being added to the output, which can cause issues with headers. However, leaving it off does not necessarily introduce any drawbacks to the code itself. It's a matter of personal preference.

Collapse
 
abdrehman98 profile image
Abdul Rehman

Noted. I am php noob thanks for explaining this.