---
title: "How to Install PHP and MariaDB on Amazon Linux 2"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/how-to-install-php-and-mariadb-on-amazon-linux-2
---

![Blog post image for How to Install PHP and MariaDB on Amazon Linux 2 - How to install PHP 7.4 and MariaDB on Amazon Linux 2, get PHP working with the Apache web server, secure the database, and run a small PHP page that connects to it.](/_astro/hero.BxVn8-Uf_145W9S.webp)

[Home](/)›[Blog](/blog)›[All Categories](/blog/categories)›[AWS](/blog/categories/aws)

Blog

[Prev in AWSHow to Install Apache Web Server on Amazon Linux 2](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)[Next in AWSHow to Install WordPress on Amazon Linux 2](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

[AWS](/blog/categories/aws)[EC2](/blog/categories/ec2)[PHP](/blog/categories/php)[MariaDB](/blog/categories/mariadb)[Apache](/blog/categories/apache)[LAMP Stack](/blog/categories/lamp-stack)[Linux](/blog/categories/linux)

# How to Install PHP and MariaDB on Amazon Linux 2

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 24 Oct 202203 Mins read04 Mins listen

[Markdown for AI(opens in a new tab)](/post/how-to-install-php-and-mariadb-on-amazon-linux-2/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

How to install PHP 7.4 and MariaDB on Amazon Linux 2, get PHP working with the Apache web server, secure the database, and run a small PHP page that connects to it.

Series

[Amazon Linux 2 LAMP Stack Setup](/series/amazon-linux-2-lamp-stack-setup)3/4

[PreviousHow to Install Apache Web Server on Amazon Linux 2](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)[NextHow to Install WordPress on Amazon Linux 2](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

All posts in this series (4)

Blog4

1.  [How to Install and Setup FireWall on Amazon Linux 2](/blog/post/how-to-install-and-setup-firewall-on-amazon-linux-2)
2.  [How to Install Apache Web Server on Amazon Linux 2](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)
3.  [How to Install PHP and MariaDB on Amazon Linux 2You are here](/blog/post/how-to-install-php-and-mariadb-on-amazon-linux-2)
4.  [How to Install WordPress on Amazon Linux 2](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

### How to Install PHP and MariaDB on Amazon Linux 2

Contents

[Introduction](#introduction)[Prerequisites](#prerequisites)[Installing PHP/MariaDB, setting up MariaDB, and running a basic PHP demo](#installing-phpmariadb-setting-up-mariadb-and-running-a-basic-php-demo)[Step 1: installing PHP](#step-1-installing-php)[Step 2: installing MariaDB](#step-2-installing-mariadb)[Step 3: configuring PHP to work with Apache](#step-3-configuring-php-to-work-with-apache)[Step 4: configuring MariaDB to work with PHP](#step-4-configuring-mariadb-to-work-with-php)[Conclusion](#conclusion)[Resources](#resources)

## [Introduction](#introduction)

In this tutorial we will set up PHP and MariaDB on Amazon Linux 2. We will get PHP working with the Apache web server, secure the MariaDB install, and then connect the two with a small PHP script.

## [Prerequisites](#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](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2).

## [Installing PHP/MariaDB, setting up MariaDB, and running a basic PHP demo](#installing-phpmariadb-setting-up-mariadb-and-running-a-basic-php-demo)

### [Step 1: installing PHP](#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 widely used server-side scripting language on the web.

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

Terminal window

```
1sudo amazon-linux-extras enable php7.4 -y
```

Next, we will install PHP.

Terminal window

```
1sudo 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

```
1php -v
```

The output should look like this:

Terminal window

```
1PHP 7.4.30 (cli) (built: Jun 23 2022 20:19:00) ( NTS )2Copyright (c) The PHP Group3Zend Engine v3.4.0, Copyright (c) Zend Technologies
```

### [Step 2: installing MariaDB](#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.

First, we will install MariaDB.

Terminal window

```
1sudo yum install mariadb-server -y
```

Next, we will start MariaDB.

Terminal window

```
1sudo systemctl start mariadb
```

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

Terminal window

```
1sudo systemctl enable mariadb
```

We will now secure MariaDB.

Terminal window

```
1sudo mysql_secure_installation
```

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

Terminal window

```
1Enter 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

```
1Set root password? [Y/n]
```

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

Terminal window

```
1Remove anonymous users? [Y/n]
```

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

Terminal window

```
1Disallow 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

```
1Remove 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

```
1Reload privilege tables now? [Y/n]
```

### [Step 3: configuring PHP to work with Apache](#step-3-configuring-php-to-work-with-apache)

First, we need to restart Apache.

Terminal window

```
1sudo systemctl restart httpd
```

We will create a directory for our PHP files.

Terminal window

```
1sudo mkdir /var/www/html/php
```

Next, we will create a PHP file.

Terminal window

```
1sudo vi /var/www/html/php/index.php
```

We will add the following content to the file.

```
1<?php2  phpinfo();
```

We will now open the file in a web browser.

Terminal window

```
1http://your_domain_name/php/index.php
```

You should see the following output:

![phpinfo](/assets/blog/0013-how-to-install-php-and-mariadb-on-amazon-linux-2/phpinfo.png)

### [Step 4: configuring MariaDB to work with PHP](#step-4-configuring-mariadb-to-work-with-php)

First, we will create a database for our PHP files.

Terminal window

```
1sudo mysql -u root -p
```

```
1CREATE DATABASE php;
```

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

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

We will grant all privileges to the user.

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

We will now exit MariaDB.

```
1exit
```

We will create a PHP file.

Terminal window

```
1sudo vi /var/www/html/php/db.php
```

We will add the following content to the file.

```
1<?php2  $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

```
1http://your_domain_name/php/db.php
```

You should see the following output:

![phpdb](/assets/blog/0013-how-to-install-php-and-mariadb-on-amazon-linux-2/phpdb.png)

## [Conclusion](#conclusion)

In this tutorial, we learned how to set up PHP and MariaDB on Amazon Linux 2, how to get PHP working with the Apache web server, and how to connect PHP to MariaDB.

## [Resources](#resources)

-   [PHP Manual - Installation on Unix systems](https://www.php.net/manual/en/install.unix.php)
-   [MariaDB Server Documentation - Installing MariaDB Packages with YUM/DNF](https://mariadb.com/kb/en/installing-mariadb-packages-with-yum/)
-   [Amazon EC2 User Guide for Linux Instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)
-   [Tutorial: Installing a LAMP Web Server on Amazon Linux 2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html)
-   [Amazon Linux Extras](https://aws.amazon.com/amazon-linux-2/faqs/#Amazon_Linux_Extras)
-   [Apache HTTP Server Documentation](https://httpd.apache.org/docs/)
-   [`mysql_secure_installation` - MariaDB Knowledge Base](https://mariadb.com/kb/en/mysql_secure_installation/)
-   [PHP Manual - `phpinfo()`](https://www.php.net/manual/en/function.phpinfo.php)
-   [PHP Manual - MySQLi connect](https://www.php.net/manual/en/mysqli.construct.php)
-   [Previous Post: How to Install Apache Web Server on Amazon Linux 2](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)
-   [MySQL CREATE DATABASE Statement](https://dev.mysql.com/doc/refman/8.0/en/create-database.html) (MariaDB syntax is similar)
-   [MySQL CREATE USER Statement](https://dev.mysql.com/doc/refman/8.0/en/create-user.html) (MariaDB syntax is similar)
-   [MySQL GRANT Statement](https://dev.mysql.com/doc/refman/8.0/en/grant.html) (MariaDB syntax is similar)

Was this useful?

## Tags

[#PHP Installation](/blog/tags/php-installation)[#MariaDB Installation](/blog/tags/mariadb-installation)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#Apache Configuration](/blog/tags/apache-configuration)[#LAMP Setup](/blog/tags/lamp-setup)[#MySQL Secure Installation](/blog/tags/mysql-secure-installation)[#PHP Modules](/blog/tags/php-modules)[#Database Setup](/blog/tags/database-setup)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2 "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2 "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2&title=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202&summary=How%20to%20install%20PHP%207.4%20and%20MariaDB%20on%20Amazon%20Linux%202%2C%20get%20PHP%20working%20with%20the%20Apache%20web%20server%2C%20secure%20the%20database%2C%20and%20run%20a%20small%20PHP%20page%20that%20connects%20to%20it.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202%20https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2 "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2&text=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202 "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2&title=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202 "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2&t=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202 "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2&media=&description=How%20to%20install%20PHP%207.4%20and%20MariaDB%20on%20Amazon%20Linux%202%2C%20get%20PHP%20working%20with%20the%20Apache%20web%20server%2C%20secure%20the%20database%2C%20and%20run%20a%20small%20PHP%20page%20that%20connects%20to%20it. "Share on Pinterest")[Email](<mailto:?subject=How%20to%20Install%20PHP%20and%20MariaDB%20on%20Amazon%20Linux%202&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-install-php-and-mariadb-on-amazon-linux-2>)

## Comments

## You might also enjoy

More posts on similar topics

[![How to Install WordPress on Amazon Linux 2](/_astro/hero.CFnKBY7F_2mvbUb.webp)](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

## [How to Install WordPress on Amazon Linux 2](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [WordPress](/blog/categories/wordpress)
-   [LAMP Stack](/blog/categories/lamp-stack)
-   [Linux](/blog/categories/linux)

Introduction This tutorial installs WordPress on Amazon Linux 2 and configures it to run with Apache, PHP, and MariaDB. Prerequisites To follow along with this tutorial, you will need:An

[#WordPress Installation](/blog/tags/wordpress-installation)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#Apache Setup](/blog/tags/apache-setup)+5 tags

[read more](/blog/post/how-to-install-wordpress-on-amazon-linux-2)

[![How to Install Apache Web Server on Amazon Linux 2](/_astro/hero.CFurz9Hr_Z2mT9lT.webp)](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)

## [How to Install Apache Web Server on Amazon Linux 2](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Apache](/blog/categories/apache)
-   [Web Server](/blog/categories/web-server)
-   [Linux](/blog/categories/linux)
-   [Firewall](/blog/categories/firewall)

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 serve a simple HTML web page. Prerequi

[#Apache Installation](/blog/tags/apache-installation)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#HTTPD](/blog/tags/httpd)+4 tags

[read more](/blog/post/how-to-install-apache-web-server-on-amazon-linux-2)

[![How to Install and Setup FireWall on Amazon Linux 2](/_astro/hero.b8oDtFR4_ZdWcUP.webp)](/blog/post/how-to-install-and-setup-firewall-on-amazon-linux-2)

## [How to Install and Setup FireWall on Amazon Linux 2](/blog/post/how-to-install-and-setup-firewall-on-amazon-linux-2)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Linux](/blog/categories/linux)
-   [Firewall](/blog/categories/firewall)
-   [Security](/blog/categories/security)

Introduction This tutorial covers installing and configuring firewalld on Amazon Linux 2, including setting the default zone and managing services and ports. Prerequisites To follow along wit

[#Firewalld](/blog/tags/firewalld)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#EC2 Security](/blog/tags/ec2-security)+5 tags

[read more](/blog/post/how-to-install-and-setup-firewall-on-amazon-linux-2)

[![How to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/_astro/hero.CIYv_zEL_2aFueX.webp)](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

## [How to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Node.js](/blog/categories/nodejs)
-   [Linux](/blog/categories/linux)

Introduction Node.js does not exist in the default Amazon Linux 2 repository. So, we need to add the Node.js repository to the system. In this post, we will learn how to install and configure Node

[#Node.js Installation](/blog/tags/nodejs-installation)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#AWS EC2 Setup](/blog/tags/aws-ec2-setup)+3 tags

[read more](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

[![How to Run an Apache Web Server Using Docker on an AWS EC2 Instance](/_astro/hero.WA3H4xsW_KuMrf.webp)](/blog/post/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](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Docker](/blog/categories/docker)
-   [Apache](/blog/categories/apache)
-   [Web Server](/blog/categories/web-server)

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\](

[#AWS EC2 Docker](/blog/tags/aws-ec2-docker)[#Apache Docker](/blog/tags/apache-docker)[#Containerization](/blog/tags/containerization)+4 tags

[read more](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)

[![How To Create An AWS EC2 Instance Using AWS CLI](/_astro/hero.Dh2x9Phr_14dc4A.webp)](/blog/post/how-to-create-an-aws-ec2-instance-using-aws-cli)

## [How To Create An AWS EC2 Instance Using AWS CLI](/blog/post/how-to-create-an-aws-ec2-instance-using-aws-cli)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [VPC](/blog/categories/vpc)
-   [AWS CLI](/blog/categories/aws-cli)
-   [WordPress](/blog/categories/wordpress)
-   [Linux](/blog/categories/linux)

Introduction In this tutorial we will create an AWS EC2 instance with the AWS CLI. We will build the network around it first, then launch the instance with a user data script that installs the Apa

[#AWS CLI EC2](/blog/tags/aws-cli-ec2)[#AWS CLI VPC](/blog/tags/aws-cli-vpc)[#VPC Configuration](/blog/tags/vpc-configuration)+8 tags

[read more](/blog/post/how-to-create-an-aws-ec2-instance-using-aws-cli)

6 related posts
