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.
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|
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 http://rpms.famillecollet.com/enterprise/remi-release-7.rpm ------ 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 http://rpms.famillecollet.com/enterprise/remi-release-6.rpm ------ For RHEL 6 Only ------ # subscription-manager repos --enable=rhel-6-server-optional-rpms
On Fedora 24-29
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-29.rpm [On Fedora 29] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-28.rpm [On Fedora 28] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-27.rpm [On Fedora 27] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-26.rpm [On Fedora 26] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-25.rpm [On Fedora 25] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-24.rpm [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.
- MariaDB/MySQL (php-mysql) – a dynamic shared object that will add MariaDB support to PHP.
- PostgreSQL (php-pgsql) – PostgreSQL database support for PHP.
- MongoDB (php-pecl-mongo) – An interface for communicating with the MongoDB database in PHP.
- Generic (php-pdo) – A dynamic shared object that will add a database access abstraction layer to PHP.
- Memcache (php-pecl-memcache) – Memcached is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory.
- Memcached (php-pecl-memcached) – An extension that uses the libmemcached library to provide API for communicating with memcached servers.
- GD (php-gd) – A dynamic share object that adds support for using the gd graphics library to PHP.
- XML (php-xml) – A dynamic shared objects that adds support to PHP for manipulating XML documents.
- MBString (php-mbstring) – An extension to handle multi-byte string in PHP applications.
- MCrypt (php-mcrypt) – A Mcrypt library for PHP scripts.
- APC (php-pecl-apcu) – APC module used to optimize and cache PHP code.
- CLI (php-cli) – Command-line interface for PHP.
- 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 https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm [On RHEL/CentOS 7] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm [On RHEL/CentOS 6] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc29-1.noarch.rpm [On Fedora 29] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc28-1.noarch.rpm [On Fedora 29] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc27-1.noarch.rpm [On Fedora 29] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc26-1.noarch.rpm [On Fedora 29] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc25-1.noarch.rpm [On Fedora 29] # rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-fc24-1.noarch.rpm [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:
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
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
------ Enable Apache and MariaDB on Boot ------ # systemctl enable httpd # systemctl enable mariadb ------ Start Apache and MariaDB ------ # systemctl start httpd # systemctl start mariadb
------ 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.
phpinfo() function shows a great deal of information about the current PHP installation:
<?php phpinfo(); ?>
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:
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.