How to Install the LAMP Stack on CentOS 7? Print

  • 0

Apache Web Server

The Apache web server is generally recognized as the world’s most popular open source HTTP web server and this allows us to easily install it from its available repositories in CentOS with the following steps.  You must be logged in to your server as root using SSH.

Installing Apache Web Server

Run the following command logged in as root to install the apache http server using the yum repositories provided by CentOS

[root@centos-7 ~]# yum install httpd

Once the installation is complete, we need to start the service and then enable it to startup at boot using following commands.

[root@centos-7 ~]# systemctl start httpd

[root@centos-7 ~]# systemctl enable httpd

Adding Firewall Rule

Apache listens on port 80 and port 443, we need to exclude it from the firewall by simply executing following commands to add a permanent firewall rule and then restart the firewalled service :

[root@centos-7 ~]# firewall-cmd –permanent –add-server http

[root@centos-7 ~]# firewall-cmd –permanent –add-server https

[root@centos-7 ~]# systemctl restart firewalld.service

Apache Test

To test the Apache web server installation, we need to open a web browser and enter the server's IP address.  If you got the Apache test page, then it means that your Apache HTTP server is operational and successfully installed.

MySQL-MariaDB Server Setup

In CentOS 7, MySQL is replaced with MariaDB, it is an enhanced drop-in replacement for MySQL.  Using the following steps we will now install it with yum commands.

Installing MariaDB

We start by installing the MariaDB-server package using the command below.

[root@centos-7 ~]# yum install mariadb mariadb-server

MariaDB server will now be installed with its required dependencies and updates. Once done we need to start it’s service, enable it to start at boot and then start the daemon for the first time as shown below.

[root@centos-7 ~]# systemctl start mariadb.service

[root@centos-7 ~]# systemctl enable mariadb.service

Securing MySQL-MariaDB

Using the mysql_secure_installation command will secure the database server :

  • By changing or setting the server's root password
  • By removing anonymous user accounts
  • By disabling remote root logins with the test database
  • By removing the test database and access to it
[root@centos-7 ~]# mysql_secure_installation

Set root password? [Y/n] Y

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] Y

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y

Testing MySQL-MariaDB

We’re now done with the MySQL/MariaDB installation. We now need to check and test the database by logging in, then we’ll create a new database using the following commands.

[root@centos-7 ~]# mysql -u root -p

Enter password: ******

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is x

Server version: x.x.xx-MariaDB MariaDB Server

Create a new database and a user with grant privileges as below, where the database name is newdatabase, the user name as newuser having password of newuser123.  You will want to change the database name, username and password to your own.

MariaDB [(none)]> create database newdatabase;

Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> grant all on newdatabase.* to 'newuser' identified by 'newuser123';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit


PHP Installation Setup

After successful installations of Apache and MariaDB, we can now proceed with PHP and its other recommended packages as below.

Installing PHP

We can install PHP and other supporting libraries with the following command.

[root@centos-7 ~]# yum install php php-mysql php-pear php-gd php-mbstring

PHP Configuration

The PHP configuration file is located in /etc/php.ini, this file can be modified per your requirements as well as to upgrade performance and other parameters or modifications like creating error logs.   Lets start with installing nano which is an easy to use text editor.  You can also use the vim text editor if you are more familiar with it.

[root@centos-7 ~]# yum install nano

[root@centos-7 ~]# nano /etc/php.ini

error_log = /var/log/php/error.log

max_input_time = 30

Now create the error.log file and give ownership to the apache user.

[root@centos-7 ~]# touch /var/log/php/error.log

[root@centos-7 ~]# chown –R apache /var/log/php

After making the previous modifications, we are now required to restart the Apache services for the changes to take effect.

[root@centos-7 ~]# systemctl reload httpd

Testing PHP

In order to test the PHP installation,  create a file phpinfo.php in the document root directory which is  /var/www/html/ and then add the below code as shown below.

[root@centos-7 html]# nano phpinfo.php

<?php phpinfo(); ?>

Then open the following link in your web browser to see all the information about PHP and its other configurations:   http://(  (replacing the IP address with your servers IP).


In this article we learned how to setup LAMP Stack, we installed and configured the Apache HTTP server, MySQL-MariaDB and PHP. 


Was this answer helpful?

« Back