Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7

This how-to guide explains how to install the latest version of the Apache, MySQL 8 or MariaDB 10 and PHP 7 along with the required PHP modules on RHEL / CentOS 7/6 and Fedora 24-29.

This combination of the operating system (Linux) with the web server (Apache), database server (MariaDB/MySQL) and server-side scripting language (PHP) is known as the LAMP stack.

Don’t Miss: How to Install Nginx 1.15, MariaDB 10 and PHP 7 on CentOS 7

Since September 2015, PHP 5.4 is no longer supported by PHP team and it’s reached to end-of-life, still, PHP 5.4 ships with RHEL/CentOS 7/6 with minor version change and Red Hat supports it, so upgrading to a higher version not required. However, it is highly recommended to upgrade your PHP 5.4 to PHP 5.5+ for greater security and performance.

Here is what your current Linux distribution ships with:

PHP Current Version RHEL/CentOS 7 RHEL/CentOS 6
7.3 5.4 5.3

To do this, we will enable the EPEL and Remi repository and use yum and dnf (the new package management tool available in Fedora).

Step 1: Installing EPEL and Remi Repository

EPEL (Extra Packages for Enterprise Linux) is a community based repository offers add-on software packages for RHEL-based Linux distributions.

Remi is a repository where you can find the latest versions of the PHP stack (full featured) for installation in the Fedora and Enterprise Linux distributions.

On RHEL/CentOS 7

# yum update && yum install epel-release
# rpm -Uvh

------ For RHEL 7 Only ------
# subscription-manager repos --enable=rhel-7-server-optional-rpms

On RHEL/CentOS 6

# yum update && yum install epel-release
# rpm -Uvh

------ For RHEL 6 Only ------
# subscription-manager repos --enable=rhel-6-server-optional-rpms

On Fedora 24-29

# rpm -Uvh  [On Fedora 29]
# rpm -Uvh  [On Fedora 28]
# rpm -Uvh  [On Fedora 27]
# rpm -Uvh  [On Fedora 26]
# rpm -Uvh  [On Fedora 25]
# rpm -Uvh  [On Fedora 24]

Step 2: Installing Apache Web Server

Apache is a Free and Open Source HTTP web server that runs on most UNIX-based operating systems as well as on Windows. As such, it can be used to serve static web pages and handle dynamic content. Recent reports show that Apache is the number one server used in websites and Internet-facing computers.

To install Apache web server, first update the system software packages and install it using following commands.

# yum -y update
# yum install httpd

Once Apache web server installed, you can start enable it to auto start at system boot.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd

If you are running firewalld, make sure to allow Apache traffic on the firewall.

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload

Step 3: Installing PHP Using Remi Repository

PHP (Hypertext Preprocessor) is a Free and Open Source server-side scripting language that is best suited for web development. It can be used to produce dynamic web pages for a website and is most frequently found in *nix servers. One of the advantages of PHP is that it is easily extensible through the use of a wide variety of modules.

To install PHP, first you need to enable Remi repository by installing yum-utils, a collection of useful programs for managing yum repositories and packages.

# yum install yum-utils

Once installed, you can use yum-config-manager provided by yum-utils to enable Remi repository as the default repository for installing different PHP versions as shown.

For example, to install PHP 7.x version, use the following command.

------------- On CentOS & RHEL ------------- 
# yum-config-manager --enable remi-php70 && yum install php       [Install PHP 7.0]
# yum-config-manager --enable remi-php71 && yum install php       [Install PHP 7.1]
# yum-config-manager --enable remi-php72 && yum install php       [Install PHP 7.2]
# yum-config-manager --enable remi-php73 && yum install php       [Install PHP 7.3]

------------- On Fedora ------------- 
# dnf --enablerepo=remi install php70      [Install PHP 7.0]
# dnf --enablerepo=remi install php71      [Install PHP 7.1]
# dnf --enablerepo=remi install php72      [Install PHP 7.2]
# dnf --enablerepo=remi install php73      [Install PHP 7.3]

Next, we are going to install all these following PHP modules in this article. You can search for more PHP-related modules (perhaps to integrate a specific functionality that your web applications need) with the following command:

------ RHEL/CentOS 7/6------
# yum search all php     

------ Fedora ------
# dnf search all php   

Regardless of the distribution, the above commands return the list of packages in the currently enabled repositories that include the word php in the package name and/or the description.

Here are the packages that we will install. Please keep in mind that MySQL connectors (PHP, Perl, Python, Java, etc.) will work unchanged with MariaDB as both systems use the same client protocol and the client libraries are binary compatible.

  1. MariaDB/MySQL (php-mysql) – a dynamic shared object that will add MariaDB support to PHP.
  2. PostgreSQL (php-pgsql) – PostgreSQL database support for PHP.
  3. MongoDB (php-pecl-mongo) – An interface for communicating with the MongoDB database in PHP.
  4. Generic (php-pdo) – A dynamic shared object that will add a database access abstraction layer to PHP.
  5. Memcache (php-pecl-memcache) – Memcached is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory.
  6. Memcached (php-pecl-memcached) – An extension that uses the libmemcached library to provide API for communicating with memcached servers.
  7. GD (php-gd) – A dynamic share object that adds support for using the gd graphics library to PHP.
  8. XML (php-xml) – A dynamic shared objects that adds support to PHP for manipulating XML documents.
  9. MBString (php-mbstring) – An extension to handle multi-byte string in PHP applications.
  10. MCrypt (php-mcrypt) – A Mcrypt library for PHP scripts.
  11. APC (php-pecl-apcu) – APC module used to optimize and cache PHP code.
  12. CLI (php-cli) – Command-line interface for PHP.
  13. PEAR (php-pear) – Application Repository framework for PHP.

Install these following necessary PHP modules with the command below.

------ On RHEL/CentOS 7/6 ------
# yum --enablerepo=remi install php-mysqlnd php-pgsql php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

------ On Fedora ------
# dnf --enablerepo=remi install php-mysqlnd php-pgsql php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

Step 4: Installing MySQL or MariaDB Database

In this section, we will show you installation of both databases MySQL and MariaDB, so its up-to you what to choose based on your requirements.

Installing MySQL 8 Database Server

MySQL is one of the world’s most popular open source relational database management system (RDBMS) that runs any server by providing multi-user access to multiple databases. MySQL runs with Apache.

To install latest MySQL 8.0 version, we will install and enable official MySQL Yum software repository using the following commands.

# rpm -Uvh        [On RHEL/CentOS 7]
# rpm -Uvh     [On RHEL/CentOS 6]
# rpm -Uvh    [On Fedora 29]
# rpm -Uvh    [On Fedora 29]
# rpm -Uvh    [On Fedora 29]
# rpm -Uvh    [On Fedora 29]
# rpm -Uvh    [On Fedora 29]
# rpm -Uvh    [On Fedora 29]

After installing the MySQL Yum software repository for your Linux platform, now install the latest version of MySQL (currently 8.0) using the following command.

# yum install mysql-community-server      [On RHEL/CentOS]
# dnf install mysql-community-server      [On Fedora]

After successful installation of MySQL, it’s time to start the MySQL server with the following command.

# service mysqld start

Check out our article on how to secure MySQL 8 database installation.

Installing MariaDB 10 Database Server

MariaDB is a fork of the well-known MySQL, one of the world’s most popular Relational Database Management System (RDBMS). It is entirely developed by the community and as such it is intended to remain FOSS and compatible with the GPL.

If you are or have been, a MySQL user, migrating to MariaDB will be a very straightforward process: the popular commands to connect to, backup and restore, and manage databases are identical in both RDBMSs.

In latest RHEL/CentOS 7 distribution, MariaDB is a drop-in replacement for MySQL and in RHEL/CentOS 6 MySQL remains same and you’re not allowed to install MariaDB on RHEL/CentOS 6 from default repository, but you can install MariaDB using official MariaDB repository.

To enable the MariaDB repository on RHEL/CentOS 7 distributions, create a file named /etc/yum.repos.d/mariadb.repo with the following contents:

name = MariaDB
baseurl =

Note: As i said above, you can also install MariaDB on RHEL/CentOS 6 using official MariaDB repository as stated above.

After enabling MariaDB repository, then do:

------ On RHEL/CentOS 7 ------
# yum --enablerepo=remi install httpd MariaDB-client MariaDB-server

------ On Fedora ------
# dnf --enablerepo=remi install httpd MariaDB-client MariaDB-server

Step 5: Enable/Start Apache and MySQL/MariaDB

On SystemD

------ Enable Apache and MariaDB on Boot ------
# systemctl enable httpd
# systemctl enable mariadb

------ Start Apache and MariaDB ------
# systemctl start httpd
# systemctl start mariadb

On SysVinit

------ Enable Apache and MySQL on Boot ------
# chkconfig --levels 235 httpd on
# chkconfig --levels 235 mysqld on

------ Start Apache and MySQL ------
# /etc/init.d/httpd start
# /etc/init.d/mysqld start

Step 6: Verifying PHP Instalation

Let’s stick with the classic way of testing PHP. Create a file called test.php under /var/www/html and add the following lines of code to it.

The phpinfo() function shows a great deal of information about the current PHP installation:


Now point your web browser to http://[server]/test.php and check the presence of the installed modules and additional software by scrolling down the page (replace [server] with your domain or the IP address of your server). Your output should be similar to:

Check PHP 7 Info
Check PHP 7 Info

Congratulations! You now have a latest working installation of a LAMP stack. If something did not go as expected, feel free to contact us using the form below. Questions and suggestions are also welcome.

Note: you can also install MariaDB in other distributions by creating a custom repository following the instructions provided here.

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

197 thoughts on “Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7”

  1. Ravi,

    If am using yum conflict will come, it will install dependency all packages include apache, so am using source file .

  2. Thanks for the article, I have using php 5.6 with apache 2.4, i need to install mysqli extension, i have done with below commands.

    $ cd extname
    $ phpize
    $ ./configure
    $ make
    # make install# its seem installed correctly 

    and also i have added path in php.ini, but still its not install mysqli, php -i | grep mysqli is given mysqli with details, please help me to do correctly .

  3. Pingback: PhpMyAdmin 4.2.2 Released - Install for Apache or Nginx on RHEL/CentOS 6.5/5.9 Fedora 20-12
  4. Pingback: Install Latest Nginx 1.10.1, MariaDB 10 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 20-23
  5. Pingback: Install PrestaShop (Free Online Ecommerce Shopping Store) On RHEL/CentOS And Fedora
    • @Victor,

      Have you enabled said Remi repository on the system, before trying to install/update Apache? Which version of Apache, you currently have on the system?

  6. I had some issues when i tried to install Apache, Mysql and PHP using this method but now i guess it’s working.

    I would like to know how i could test my webserver ? I’m not using any GUI at my Red Hat Server. I saw you posting to test using . But how could i do that in my pc? I tried to use the server ip address and i don’t have any success. :(

    Could you help me?

  7. hi, when I try to create a phpinfo.php and view it from the browser level, i wasn’t able to see the same page as you.
    Instead I am seeing things like


Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.