How to Install Apache 2, PHP7 and MariaDB 10.2 on CentOS 7.x

In this tutorial, I’m going to focus on work from the command line to install Apache PHP7 and MariaDB on Linux. This will be the same for all version of Linux – only the package manager will be different.

So in these examples I’m using “yum” for CentOS. If you’re using Ubuntu, you need to use “apt-get” etc.

Step 1: Enabling the EPEL Repository

Some of the required packages are might not available through the main CentOS repository, you must enable the EPEL repository (Extra Packages for Enterprise Linux). As root, run following commands:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
rpm --import https://fedoraproject.org/static/0608B895.txt
yum -y install epel-release

Step 2: Removing the Previous PHP Version

It is necessary to remove the existing PHP packages (if there are any) since Apache will need the PHP7 packages to run PHP scripts:

yum -y remove php-cli mod_php php-common

Step 4: Enabling the MariaDB 10.2 Repository

By default, Yum will install MariaDB 5.5. In order to force Yum to install MariaDB 10.3, you must create a file named MariaDB.repo in /etc/yum.repos.d:

nano /etc/yum.repos.d/MariaDB.repo

Insert the following content into the newly created file:

[mariadb]
name = MariaDB-10.2.3
baseurl = http://yum.mariadb.org/10.2.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Step 5: Installing the Required Modules

You can install all the required packages using a single yum command (I assume you’re logged into root):

yum -q -y install httpd mod_ssl mod_php71u php71u-cli php71u-mbstring 
php71u-mcrypt php71u-mysqlnd php71u-json MariaDB-server MariaDB-client 
MariaDB-devel

This will install the required packages and their dependencies from the repositories. please note that you should change this command depending on your specific Linux falvour .

Confirm the download of packages and wait for it to finish. And you’re done! Apache, PHP 7, and MariaDB 10.2 are installed.

Step 6: Check if the httpd Service is Running

Package installation doesn’t mean that it’s automatically running. To verify whether or not Apache is running on your server, type the following command:

systemctl status httpd

You will see, the current status is “inactive”. This means that Apache isn’t running yet and won’t be able to service any requests. So we need to manually start it.

Step 7: Starting Apache HTTPD Server

To start Apache, use the following:

systemctl start httpd,/pre.

This won’t generate any output, but Apache will start running in the background. Using the previous step, you can now check once again to see if the httpd service is active. If so, it’ll highlight the status in green as shown here

Step 8: Open up your Firewall Port

Apache uses port 80 by default and port 443 for SSL to receive incoming connections. Depending upon your firewall setup, these ports might not be open. For example If you’re using ConfigServer Firewall , the list of open ports is located in the following file:

/etc/csf/csf.conf

Open it and scroll down till you see the line starting with TCP_IN. It’ll show you a list of ports separated by a comma. Make sure that 80 and 443 are among the opened ports:
After that, you’ll probably need to restart the firewall for the rules to take effect.

csf -r

Now that port 80 is open and Apache is running, we can test to see if everything is working.

Step 9: Testing Apache

To test if all is well, simply open a browser and visit your server by typing in the IP address or the domain name into the address bar using http. If all the above steps have gone perfect, you should see a testing page.
Step 10: Setting Apache to Run on Boot

Even though Apache is running right now, httpd will not persist through server reboots. To make that happen, we need to enable the service. Doing this is simple. Just use:

systemctl enable httpd

Step 11: Testing PHP 7.1

To make sure that PHP7 is working correctly, create a file named phpinfo.php and save it to /var/www/html:

nano /var/www/html/phpinfo.php

Add the following content to the PHP file and save it:

<?php phpinfo(); ?>

Now open http://[your server hostname]/phpinfo.php using your web browser. You should get the information page for PHP.
Once done make sure to delete the phpinfo.php file as it exposes your PHP configuration to the whole world:

rm /var/www/html/phpinfo.php

Step 12: Starting the MariaDB Server

In order for the MariaDB server to start automatically at boot, use this command:

systemctl enable mariadb

Next, start the MariaDB server:

systemctl start mariadb

Step 13: Securing the MariaDB Server

As root, run the mysql_secure_installation script to harden your MariaDB server:

mysql_secure_installation

Make sure to specify the MariaDB root password, remove the anonymous users and disallow remote root login.

Step 14: Creating a New MariaDB User and Database

We’re now going to create a test database as well as a MariaDB user. To do so, we’ll use the “mysql” command line tool. Type this command and enter your MariaDB root password when prompted:

mysql -u root -p

Now we’ll create a database named “cppldb” and a user named “cppl”:

MariaDB [(none)]> create database cppldb;
MariaDB [(none)]> create user 'cppl'@localhost identified by 
'password';
MariaDB [(none)]> grant all on maindb.* to 'cppl' identified by 
'password';
MariaDB [(none)]> exit;

Step 15: Installing phpMyAdmin

phpMyAdmin is a PHP application that allows you to manage MySQL and MariaDB databases through a web interface.

Since the phpMyAdmin package for CentOS depends on PHP 5 packages, it will not be possible to install it using Yum. Here’s how to install phpMyAdmin manually

cd /root/temp
wget https://files.phpmyadmin.net/phpMyAdmin/4.6.5.2/phpMyAdmin
-4.6.5.2-all-languages.tar.gz
tar xvzf phpMyAdmin-4.6.5.2-all-languages.tar.gz
mv phpMyAdmin-4.6.5.2-all-languages phpmyadmin
mv phpmyadmin /var/www/html/
chown -R nobody.nobody /var/www/html/phpmyadmin

Next you need to create a configuration file. Copy the sample file provided with phpMyAdmin:

cp /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html
/phpmyadmin/config.inc.php
nano /var/www/html/phpmyadmin/config.inc.php

Set the value for the $cfg[‘blowfish_secret’] parameter. The minimum length for the password is 32 characters:

$cfg['blowfish_secret'] = 'my_blowfish_secret_32_chars';

Save the file and exit the editor. Open your web browser to http://[your server hostname]/phpmyadmin and use the cppl account credentials you created earlier to log in.

you should now be able to manage your databases through phpMyAdmin.

So that’s it, you now have a brand new web server running Apache 2.4, PHP 7.1 and MariaDB 10.2. This is a very basic setup so feel free to give your opinion below if you experience any issues or to discuss your ideas and experiences.