How to install and configure Linux, Apache, Mariadb and PHP (LAMP ) on Centos7 or RHEL7

1. Introduction

Short for Linux, Apache, MySQL and PHP, an open-source Web development platform, also called a Web stack, that uses Linux as the operating system, Apache as the Web server, MySQL as the RDBMS and PHP as the object-oriented scripting language. Perl or Python is often substituted for PHP.

2. Requirements

You may need an operating system RHEL 7 or Centos 7 in your dedicated or vps server. Please make sure to set a hostname for your server and its dns is pointing to the IP address of the server.

3. Installation

Please proceed with the following step by step procedures:

3.1 Install Apache

Apache2 is directly available as a CentOS 7.0 package, therefore we can install it like this:

# yum -y install httpd

Now configure your system to start Apache at boot time.

# systemctl start httpd.service
# systemctl enable httpd.service

Now direct your browser to http://your-IP, and you should see the Apache2 place holder page:


3.2 Install MariaDB

To install MySQL, we do install mariadb like this:

# yum -y install mariadb-server mariadb

Then we create the system startup links for MySQL (So that MySQL starts automatically whenever the system boots) and start the MySQL server:

# systemctl start mariadb.service
# systemctl enable mariadb.service

Set passwords for the MySQL root account:

# mysql_secure_installation

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] 
New password: <--yourmariadbpassword
Re-enter new password: <--yourmariadbpassword
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them.  This is intended only for testing, and to make the installation go a bit smoother.  You should remove them before moving into a production environment.

Remove anonymous users? [Y/n] <--ENTER
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <--ENTER
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] <--ENTER
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] <--ENTER
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!

3.3 Install PHP

We can install PHP5 and the Apache PHP5 module as follows:

# yum -y install php php-mysql

We must restart Apache afterwards:

# systemctl restart httpd.service

Testing PHP5 / Getting Details About Your PHP5 Installation

# vi /var/www/html/info.php

Now add the following phpinfo code content in this file.


Now we call that file in a browser (e.g. http://Your IP/info.php):


You may need We have successfully configured Apache2 , mysql and php.

How to install and configure Linux,Apache,Mysql(Mariadb) and PHP (a.k.a LAMP) in Centos 7 / RHEL7


LAMP (Linux, Apache, Mysql & PHP ) is an open source web development platform that uses Linux as the operating system, Apache as the Web server, MySQL as the relational database management system and PHP as the object-oriented scripting language. It is powerful and also comparatively simple and easy to use. This article will help you configure LAMP in Centos7.


Operating System: Centos 7 or RHEL 7

Before you begin LAMP INSTALLATION , you should have a server with root user privilege. By completing steps mentioned below you can configure LAMP in your server.

3. Install Apache

We can install Apache easily using CentOS’s package manager, yum.

# yum install httpd

Afterwards, your web server is installed , you can start Apache on your sever:

# systemctl start httpd.service

Now you can check webserver by using server IP address in your browser:

http://your_server_IP_address/    >>>>> here it is

Now you can see the default CentOS 7 Apache web page. Please see some sample screen shorts below,


4. Install MySQL (MariaDB)

MariaDB is one of the most popular database servers in the world. It’s made by the original developers of MySQL and guaranteed to stay open source.

# yum install mariadb-server mariadb

When the installation is complete, we need to start MariaDB with the following command:

# systemctl start mariadb

Now your mysql database is running.You can access database without any password its not secure so we need to provide password protection.

# mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the instructions:

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 authorization.

New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

After this when you try to access database with out password it may show an error like this:

[root@arunima-centos7 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

You set a mysql root password for your database server so from now on you can’t access it without password.

“root” session do not know password to mysql root user.

If you want to make easier to access your mysql create a .my.cnf in /root/ and add the following content

# touch /root/.my.cnf
# vim /root/.my.cnf

user = root
password = mysqlrootpassword

where of-course “mysqlrootpassword” is your password for mysql’s root user so when you execute mysql command it uses this password.

Install PHP

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

# yum install php php-common php-gd php-mysql

After installing PHP, you should restart apache.

# systemctl restart httpd.service

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root” or “document root”.

In CentOS 7, this directory is located at /var/www/html/. We can create the file at that location as shown below:


Now we can test whether our web server can correctly display content generated by a PHP script.


Please see a sample screen short of phpinfo below,


How to install MariaDB Server in cPanel/WHM servers

MariaDB is a binary drop in replacement of the same MySQL version (for example MySQL 5.1 -> MariaDB 5.1, MariaDB 5.2 & MariaDB 5.3 are compatible. MySQL 5.5 will be compatible with MariaDB 5.5). What this means is that:

  • Data and table definition files (.frm) files are binary compatible.
  • All client APIs, protocols and structs are identical. All filenames, binaries, paths, ports, sockets, and etc… should be the same.
  • All MySQL connectors (PHP, Perl, Python, Java, .NET, MyODBC, Ruby, MySQL C connector etc) work unchanged with MariaDB.
  • The mysql-client package also works with MariaDB server.
  • The shared client library is binary compatible with MySQL’s client library.

This means that for most cases, you can just uninstall MySQL and install MariaDB and you are good to go. (No need to convert any datafiles if you use same main version, like 5.1). You must however still run mysql_upgrade to finish the upgrade. This is needed to ensure that your mysql privilege and event tables are updated with the new fields MariaDB uses. MariaDB do a monthly merges with the MySQL code base to ensure its compatibility and get any and all features and bug fixes Oracle adds.

There are some installation issues with PHP5 that you should be aware of ( REF: )

What are the advantages of using MariaDB Server ?

  • Faster and safer replication: Group commit for the binary log. This makes many setups that uses replication and lot’s of updates more than 2x times faster.
  • Indexes for the MEMORY(HEAP) engine are faster. According to a simple test, 24% faster on INSERT for integer index and 60% faster for index on a CHAR(20) column.
  • CHECKSUM TABLE is faster.
  • MariaDB improved the performance of character set conversions (and removed conversions when they were not really needed). Overall speed improvement is 1-5 % (according to sql-bench) but can be higher for big results sets with all characters between 0x00-0x7f.
  • Pool of Threads in MariaDB 5.1 and even better in MariaDB 5.5. This allows MariaDB to run with 200,000+ connections and with a notable speed improvement when using many connections.
  • There are some improvements to the DBUG code to make its execution faster when debug is compiled in but not used.
  • Our use of the Aria storage engine enables faster complex queries (queries which normally use disk-based temporary tables). The Aria storage engine is used for internal temporary tables, which should give a speed up when doing complex selects. Aria is usually faster for temporary tables when compared to MyISAM because Aria caches row data in memory and normally doesn’t have to write the temporary rows to disk.
  • The test suite has been extended and now runs much faster than before, even though it tests more things.

How can you replace MySQL with MariaDB Server in cPanel installed systems ?

Below you will find some steps on how to affectively stop cPanel/WHM from maintaining MySQL and begin utilizing MariaDB for any and all database activity on your server. MariaDB is “An enhanced, drop-in replacement for MySQL” (Source: .

We recommend only experienced systems administrators perform the above and we are not responsible for any possible data loss.

Step 1: Backup existing MySQL data

Make sure to save all existing data just in case there are any issues.

# mysqldump --all-databases --routines --triggers > /home/db_dump/alldb.sql
# service mysql stop
# cp -r /var/lib/mysql/mysql /home/db_dump/
# service mysql start
Step 2: Disable the targets so cPanel no longer handles MySQL updates #ONLY FOR cPanel 11.36+

The following will mark the versions of MySQL we distribute as uninstalled so they are no longer maintained by cPanel/WHM

# /scripts/update_local_rpm_versions --edit target_settings.MySQL50 uninstalled
# /scripts/update_local_rpm_versions --edit target_settings.MySQL51 uninstalled
# /scripts/update_local_rpm_versions --edit target_settings.MySQL55 uninstalled
Step 3: Remove existing MySQL RPM’s so theres a clean slate for MariaDB

Important: The below command will uninstall the MySQL RPM’s!

# /scripts/check_cpanel_rpms --fix --targets=MySQL50,MySQL51,MySQL55
Step 4: Create a yum repository for MariaDB

Access and select the DISTRO and place the repo content to /etc/yum.repos.d/MariaDB.repo

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

# MariaDB 5.5 CentOS repository list - created 2013-06-23 21:13 UTC
name = MariaDB
baseurl =

Step 5: install MariaDB using the following command
# yum install MariaDB-server MariaDB-client MariaDB-devel

If you are having any dependency problem, please remove PHP from the /etc/yum.conf file and then run yum command again. Please add it back to yum conf once the package is installed.

# /etc/init.d/mysql start
# mysql_upgrade
# /etc/init.d/mysql restart

Please make sure to add PHP back to exclude section of /etc/yum.conf file.

Final Step: Rebuild Easyapache/PHP to ensure modules are intact/working
# /scripts/easyapache --build

Done !