Install LAMP stack (Apache, MariaDB, PHP) In CentOS 7

We have already published few LAMP stack guides for different Linux platforms. Today, we are going to install LAMP stack in CentOS 7 64 bit server. As you may already know, LAMP stack is the combination of Linux, Apache web server, MySQL/MariaDB, PHP. LAMP stack is used to deploy web-based applications and host dynamic websites. Installing and configuring LAMP stack is trivial. Let us skip the theoretical part and start the practical part straight away.

Though it was tested on CentOS, these steps are same for RHEL, Fedora, and Scientific Linux distributions.

1. Install Apache

Run the following command as root user to install Apache webserver:

yum install httpd

Then, start and enable the Apache service using commands:

systemctl start httpd
systemctl enable httpd

Next, allow http (port 80) and https (port 443) services through your firewall or router.

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

Restart firewall to take effect the changes.

systemctl restart firewalld

Test Apache

Open your web browser and navigate to http://localhost/ or http://IP_Address/.

You will be pleased with following Apache test page. If you see this page, Great! Apache is working!!

Apache test page

Apache test page

2. Install MariaDB

Install MariaDB server using command:

yum install mariadb-server mariadb

Start and enable MariaDB service as shown below.

systemctl start mariadb
systemctl enable mariadb

Set database root password

By default, MySQL root password is empty, It is not recommended for the production usage. We need to set root password to secure database access by unauthorized user.

To set database root password, run:

mysql_secure_installation

Sample output:

Press ENTER when it asks you to set password for root user. Enter a strong password for root user twice, and continue with default values.

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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):  ## Press ENTER
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]  ## Press ENTER
New password:  ## Set new password for database root user
Re-enter new password:  ## Re-enter new password
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]  ## Press 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]  ## Press 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]  ## Press 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]  ## Press 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!

Database root password has been assigned now.

3. Install PHP

Run the following command as root user to install PHP and its modules:

yum install php php-mysql php-gd php-pear

Test PHP:

Let us now check if PHP is working or not.

To do so, create a sample “info.php” file in the web root folder:

vi /var/www/html/info.php

Add the following lines:

<?php 
phpinfo(); 
?>

Restart Apache service to take effect the changes:

systemctl restart httpd

Open up your web browser and navigate to http://IP_Address/info.php.

You will see the following php page that lists all installed modules and other details like php version, build date and commands etc.

phpinfo-chromium_003

To install all php modules (not recommended though), run:

yum install php*

Restart Apache service to take effect the changes. Refresh php test page to see list of newly installed modules or components.

4. Install phpMyAdmin

phpMyAdmin is a free, open source database management tool used to manage MySQL/MariaDB databases from a web browser.

phpMyAdmin is not available in the default repositories.

To install it, you need to enable EPEL repository using command:

yum install epel-release

Then, install phpMyAdmin as shown below:

yum install phpmyadmin

Now, open web browser and access phpMyAdmin with URL – http://localhost/phpmyadmin. Enter the MySQL/MariaDB root username and its password.

In case, you wanted to access phpMyAdmin from a remote system, you need to do some extra steps.

Configure phpMyAdmin

As you might know, phpMyAdmin can only be accessed from the localhost itself, by default.

To access phpMyAdmin from any host on the network, you need to do some additional steps. Please remember that allowing phpMyAdmin to anyone other than localhost should be considered dangerous unless properly secured by SSL.

Still you want to make it accessible from remote systems, edit the phpmyadmin.conf file:

vi /etc/httpd/conf.d/phpMyAdmin.conf

Find and comment out the whole <Directory> section.

#<Directory /usr/share/phpMyAdmin/>
# AddDefaultCharset UTF-8

# <IfModule mod_authz_core.c>
# # Apache 2.4
# <RequireAny>
# Require ip 127.0.0.1
# Require ip ::1
# </RequireAny>
# </IfModule>
# <IfModule !mod_authz_core.c>
# # Apache 2.2
# Order Deny,Allow
# Deny from All
# Allow from 127.0.0.1
# Allow from ::1
# </IfModule>
#</Directory>

Add the following lines:

<Directory /usr/share/phpMyAdmin/>
 Options none
 AllowOverride Limit
 Require all granted
</Directory>

rootserver_004

Save and close the file.

Then, edit “config.inc.php” file:

vi /etc/phpMyAdmin/config.inc.php 

Fine the following line, and change the word ‘cookie’ to ‘http’.

$cfg['Servers'][$i]['auth_type'] = 'http'; // Authentication method (config, http or cookie based)?

rootserver_005

Save and close the file. Restart Apache service using command:

systemctl restart httpd

Now, you can be able to access phpMyAdmin from any remote client in the network.

To access it, type: http://IP_Address/phpmyadmin/ in the address bar of your browser.

Enter MySQL or MariaDB username and password:

192-168-1-104-phpmyadmin-chromium_006

Here it is how phpMyAdmin dashboard looks like:

192-168-1-104-localhost-phpmyadmin-4-4-15-7-chromium_007

From now on, you can create, delete and manage your databases right from the phpMyAdmin dashboard.

There you go. LAMP stack is ready to host your websites and web-based applications. As you can see, installing and configuring LAMP stack is pretty easy.

That’s all for now. If you find this guide useful, please share it on your social networks so that other users can also benefit. I will be here soon with another interesting article.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...