How to Install Apache, MySQL/MariaDB and PHP on CentOS 8

This is one of the most commonly used Software Stack for building your web applications. This is also known as LAMP (Linux, Apache, MySQL/MariaDB, PHP/Perl/Python) Stack. See how IBM explains it.

Install Apache Web Server

First, we will start by installing the Apache web server. To complete the installation, use the following command:

yum install httpd

Output:

[root@server ~]# yum install httpd
Last metadata expiration check: 0:30:50 ago on Tue 22 Oct 2019 03:03:52 PM EDT.
Dependencies resolved.
================================================================================
Package           Arch   Version                               Repository
                                                                          Size
================================================================================
Installing:
httpd             x86_64 2.4.37-12.module_el8.0.0+185+5908b0db AppStream 1.7 M
Installing dependencies:
apr               x86_64 1.6.3-9.el8                           AppStream 125 k
apr-util          x86_64 1.6.1-6.el8                           AppStream 105 k
centos-logos-httpd
                  noarch 80.5-2.el8                            AppStream  24 k
httpd-filesystem  noarch 2.4.37-12.module_el8.0.0+185+5908b0db AppStream  35 k
httpd-tools       x86_64 2.4.37-12.module_el8.0.0+185+5908b0db AppStream 102 k
mod_http2         x86_64 1.11.3-3.module_el8.0.0+185+5908b0db  AppStream 158 k
mailcap           noarch 2.1.48-3.el8                          BaseOS     39 k
Installing weak dependencies:
apr-util-bdb      x86_64 1.6.1-6.el8                           AppStream  25 k
apr-util-openssl  x86_64 1.6.1-6.el8                           AppStream  27 k

Transaction Summary
================================================================================
Install  10 Packages

Once the installation is complete, enable Apache (to start automatically upon system boot), start the web server and verify the status using the commands below.

systemctl enable httpd

systemctl start httpd

systemctl status httpd

Output:

[root@server ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@server ~]# systemctl start httpd
[root@server ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor prese>
Active: active (running) since Tue 2019-10-22 15:39:11 EDT; 14s ago
Docs: man:httpd.service(8)

To make your pages available to public, you will have to edit your firewall rules to allow HTTP and HTTPS requests on your web server by using the following commands.

firewall-cmd --permanent --zone=public --add-service=http 

firewall-cmd --permanent --zone=public --add-service=https 

firewall-cmd --reload

Output:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@server ~]# firewall-cmd --reload
success

Verify that the web server is running and accessible by accessing your server’s IP address from your browser,

http://IP_address

image

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

yum install mariadb-server mariadb

Output:

[root@server ~]# yum install mariadb-server mariadb
Last metadata expiration check: 1:28:38 ago on Tue 22 Oct 2019 03:03:52 PM EDT.
Dependencies resolved.
================================================================================
Package                    Arch   Version                      Repository
                                                                          Size
================================================================================
Installing:
mariadb                    x86_64 3:10.3.11-2.module_el8.0.0+35+6f2527ed
                                                         AppStream 6.2 M
mariadb-server             x86_64 3:10.3.11-2.module_el8.0.0+35+6f2527ed
                                                         AppStream  16 M
Installing dependencies:
mariadb-common             x86_64 3:10.3.11-2.module_el8.0.0+35+6f2527ed                                                                                                                          AppStream  62 k
mariadb-connector-c        x86_64 3.0.7-1.el8                  
AppStream 148 k

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB and verify the status using the commands below.

systemctl enable mariadb

systemctl start mariadb

systemctl status mariadb

Output:

[root@server ~]# systemctl enable mariadb
[root@server ~]# systemctl start mariadb
[root@server ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.3 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor pre>
Active: active (running) since Tue 2019-10-22 16:35:50 EDT; 50s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
    Main PID: 2630 (mysqld)

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

Output:

[root@server ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

[root@server ~]# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
[root@server ~]#

Done!

Install PHP

To install PHP on your RHEL 8 use the command below.

yum install php php-mysqlnd php-pdo php-gd php-mbstring

Output:

[root@server ~]# yum install php php-mysqlnd php-pdo php-gd php-mbstring
Last metadata expiration check: 1:16:14 ago on Tue 22 Oct 2019 03:03:52 PM EDT.
Dependencies resolved.
================================================================================
Package             Arch   Version                             Repository
                                                                          Size
================================================================================
Installing:
php                 x86_64 7.2.11-1.module_el8.0.0+56+d1ca79aa AppStream 1.5 M
php-gd              x86_64 7.2.11-1.module_el8.0.0+56+d1ca79aa AppStream  83 k

Now restart your web server so that Apache knows that it will be serving PHP requests as well.

systemctl restart httpd 

Test your PHP, by creating a simple info.php file with a phinfo() in it. The file should be placed in the directory root for your web server, which is /var/www/html.

To create the file use:

echo "<?php phpinfo() ?>" > /var/www/html/info.php

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

image

Once confirmed, we recommend removing the info.php as it could be a security issue later on.