How To Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 / RHEL 7

3

LAMP Stack stands for Linux, Apache, MariaDB, and PHP. Here is the small tutorial about installing own web server stack with the latest release of CentOS 7 / RHEL 7.

READ: How To Update CentOS 7.x to Latest CentOS Version (7.5)

In CentOS, MySQL is now replaced by MariaDB as a default database.

Install Linux

Here is the article about Step by Step installation of CentOS 7 / RHEL 7. Now you have Linux, and the next is to install Apache, MySQL, and PHP on it.

Let’s install one by one.

Install Apache

Open up a terminal and switch to root user.

$ su -

The package name of the Apache is httpd. Install the httpd package using the YUM command.

yum install -y httpd

Start the Apache service by using the following command.

systemctl start httpd

To make the Apache web server to start automatically during every boot. Type the following on terminal and hit Enter.

systemctl enable httpd

Test Apache

To make sure everything installed correctly we will now test Apache to ensure it is working correctly. Open up any web browser and then enter the following into the web address:

http://localhost

OR

http://your.ip.adr.ess

You will get the web page saying “Testing 123”, this confirms that the Apache is working fine.

Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 - Apache Web Server's Default Page
Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 – Apache Web Server’s Default Page
Apache’s default document root is /var/www/html on CentOS, and the main configuration file is /etc/httpd/conf/httpd.conf. Additional configurations for Apache are stored in the /etc/httpd/conf.d/ directory.

Install MySQL

Next is to install the MariaDB. Type the following command and then press enter.

yum install -y mariadb mariadb-server

Start the MariaDB service using the below command.

systemctl start mariadb

To make the MariaDB start during every boot. Type the following on terminal and hit enter.

systemctl enable mariadb

Nex is to make the MariaDB secure by using the mysql_secure_installation command.

This program enables you to improve the security of your MariaDB installation in the following ways:

  • You can set a password for root accounts.
  • You can remove root accounts that are accessible from outside the localhost.
  • You can remove anonymous-user accounts.
  • You can remove the test database (which by default can be accessed by all users, even anonymous users), and privileges that permit anyone to access databases with names that start with test_.
mysql_secure_installation

Output:

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): <-- Just Press Enter Button
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 <-- Y to Set MariaDB root password
New password: <-- Enter New Password
Re-enter new password: <-- ReEnter 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] y <-- Y to Remove Anonymous Users
 ... 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] y <-- Y to Disallow root login remotely
 ... 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] y <-- Y to remove Test database
 - 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] y <-- Y to reload privilege
 ... 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!

Install PHP

By default Apache server supports the HTML language only, not PHP. Install the PHP package along with support for the MariaDB.

yum install -y php php-mysql

You need to restart the server after the installation of the PHP, to do that type the following on the terminal.

systemctl restart httpd

Firewall

Configure the firewall to allow HTTP requests.

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

Test LAMP Stack

To test PHP, place a .php file on to the default directory of the Apache.

In the terminal copy/paste the following line:

vi /var/www/html/info.php

This will open up a file called info.php.

Copy/paste this line into the phpinfo file and save and then close the file.

<?php phpinfo()?>
This file will display lots of useful information about our PHP installation, such as the installed PHP version, PHP extension details, etc.

Now open your web browser and type the following into the web address:

http://localhost/info.php

OR

http://you.ip.addr.ess/info.php

The page looks like below:

Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 - PHP Information
Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 – PHP Information

Scroll down the browser to the modules section to check the support for the MariaDB. You will get the screen like below.

Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 - MariaDB Support Details
Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 – MariaDB Support Details

You have successfully installed the LAMP Stack on CentOS 7 / RHEL 7.

Interested Topics:

READ: How To Install phpMyAdmin on CentOS 7 / RHEL 7

READ: How To Setup Let’s Encrypt With Apache on CentOS 7 / RHEL 7

That’s All.

Further Reading

You might also like

How To Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 7 / RHEL 7

3