/How To setup LAMP stack on CentOS 7

How To setup LAMP stack on CentOS 7

LAMP is short for Linux, Apache, MySQL or PHP which is an open source web development platform. It uses Linux as it’s operating system while Apache and MySQL are used as a web & database servers, the PHP is also used as an object oriented scripting language to work as a complete web stack, which is used to prepare servers for hosting web content.

In this article we are going to show you the step by step instructions to setup your own LAMP stack on CentOS 7 operating system.

Prerequisites

The Minimal system resources to setup LAMP server depends upon the requirements. We are going to setup the LAMP Server under the following base system with minimal installation.

  • Hostname: centos-7.vexxhost.com
  • Static IP: 203.0.113.1
  • Operating system: CentOS Linux 7 (Core)
  • Virtual CPU: 4 cores
  • Memory (RAM): 1GB
  • Disk Space: 40GB

After deploying the CentOS 7 with the mentioned resources, we will now proceed on setting up LAMP by installing Apache (web server), MySQL (database server) then PHP in the following order.

Apache Web Server

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 following steps.

Installing Apache Web Server

Run the following command with root user to install apache http server with yum repositories.

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

Once the installation is complete, we need to start it’s service and 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, so 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 as:

[root@centos-7 ~]# firewall-cmd –permanent –add-server http
[root@centos-7 ~]# systemctl restart firewalld.service

Apache Test

To test the Apache web server installation, we need to open a web browser and give the server IP address. If you got the Apache test page as shown below, then it means that our Apache HTTP server is operational and successfully installed: http://203.0.113.1/

MySQL-MariaDB Server Setup

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

Installing MariaDB

We start by installing 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, enabling it to start at boot and start it’s 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 mysql_secure_installation command, we can secure our database where we will be given the option to change or set its root password or remove anonymous user accounts and disallow remote root login with the test database and user removal.

[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 done with MySQL/MariaDB installation. We will now 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 10
Server version: 5.5.41-MariaDB MariaDB Server

Create a new database and a user with grant privileges as below, where database name is web with user name webuser having password web123.

MariaDB [(none)]> create database web;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> grant all on web.* to 'webuser' identified by 'web123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye

PHP Installation Setup

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

Installing PHP

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

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

PHP Configuration

PHP configuration file is located in /etc/php.ini, we can modify this as our requirements to upgrade performance and other parameters or modifications like creating error logs.

[root@centos-7 ~]# vim /etc/php.ini
error_log = /var/log/php/error.log
max_input_time = 30

Now create the error.log file and give it’s ownership to apahce user.

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

After making modifications, we are 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, lets create a file phpinfo.php in document root directory /var/www/html/ and then add the below code of phpinfo as shown below.

[root@centos-7 html]# vim phpinfo.php
<?php phpinfo(); ?>

Then open the following link in your web browser and we will be able to see all the information about PHP and its other configurations: http://203.0.113.1/phpinfo.php (replacing your IP address with the one above).

Conclusion

In this detailed article we learned to setup LAMP Stack, where we installed and configured Apache, HTTP server, MySQL-MariaDB and PHP client to successfully install the LAMP web stack with it’s tested functionality of working as a web server for hosting websites to install any web applications on it.

2016-02-04T15:36:30+00:00 July 14th, 2015|0 Comments

Leave A Comment