How to Install LAMP on CentOS 7

Share on Social Media

In this tutorial, you will learn, how to install LAMP on CentOS 7 or other Red hat based Linux distros. #centlinux #linux #mysql

LAMP is an archetypal model of web service solution stacks, named as an acronym of the names of its original four open-source components: the Linux operating system, the Apache HTTP server, the MySQL relational database management system (RDBMS), and the PHP programming language.  As a solution stack, LAMP is suitable for building dynamic web sites and web applications.

LAMP stack components are largely interchangeable and not limited to the original selection. This results in evolution of many other stacks like LEMP, LEPP, etc.

In this article, we will install LAMP stack on CentOS 7 server. We will install each of four LAMP stack components separately on our CentOS 7 server.

Recommended Book: How to Build a LAMP Server (PAID LINK)

System Specification:

CPU:3.4 Ghz (2 cores)
Memory:2 GB
Storage:60 GB
Swap:4 GB
Hostname:lampserver.mydomain.com
IP Address:170.172.0.30/16
Gateway:170.172.0.1
DNS Server:170.172.0.3

Install CentOS 7:

As the Linux component of LAMP stack, we will install CentOS 7 on our machine.

Detailed guide of CentOS 7 installation is available here. Please follow it to install and configure CentOS 7.

In this article, we are using yum for installation on the Server, therefore, make sure that either you have accessed to CentOS 7 public yum repository by using CNTLM proxy or you have configured a local yum repository for your Linux server.

Install Apache Server on CentOS 7:

Apache server is the preferred choice for the web server on a LAMP stack.

Connect to lampserver.mydomain.com using ssh as root user and execute following commands to install Apache HTTP Server on CentOS 7.

# yum install -y httpd

Start and enable Apache HTTP service.

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

Allow Apache HTTP service port in Linux firewall.

# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload

Browse URL http://170.172.0.30 to see the Apache HTTP Server’s default webpage.

Apache Default Page
Apache Default Page

Apache HTTP server has been installed on our Linux server.

Install MySQL Database Server on Linux:

For database component of LAMP stack, we have many choices like MySQL, MariaDB or PerconaDB. Here, we will install MySQL database server.

Go to MySQL:: Download MySQL Yum Repository to download and install the MySQL official yum repository.

# cd /soft
# wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
# rpm -Uvh mysql57-community-release-el7-7.noarch.rpm
# yum clean all
# yum makecache

Install the latest version of mysql-community-server. In our case, it installed mysql-community-server 5.7, which is latest at the time of this writeup.

# yum install -y mysql-server

Start and enable the mysqld service.

# systemctl enable mysqld.service
# systemctl start mysqld.service

When mysqld service starts for the first time, it generates a random password for root@localhost user in /var/log/mysqld.log file. Use the following command to get it.

# grep 'temporary password' /var/log/mysqld.log

Configure the mysql security using following command.

# /usr/bin/mysql_secure_installation 

and select the following options.

set a password for root
remove anonymous users: Y
Disallow remote Root Login: Y
Remove Test Database and access to it: Y
Reload Privilege tables: Y

MySQL database server has been installed and configured on our Linux server.

Install PHP on CentOS 7:

PHP is the hypertext preprocessor and it adds the application functionality in our LAMP server.

Use the following command to install PHP from CentOS yum Repository. In our case, it installed PHP 5.4. Although, stable version 7 of PHP is available at this time. If you want to use a latest version than you can download it from http://www.php.net

# yum install -y php

Depends upon your web application, you may required to install specific php modules on the server. To see the compatible modules for your php server use the following command:

# yum search php-

Install required php modules as follows:

# yum install -y php-mysql

Restart the httpd Service to load the php modules.

# systemctl restart httpd.service

Create the test page for php at document root of Apache Server.

# cat >> /var/www/html/info.php << EOF 
><?php 
>phpinfo(); 
>?> 
>EOF

Browse URL http://170.172.0.30/info.php. It will show you a very long page with useful information that may be helpful during troubleshooting of your LAMP server.

PHPinfo Page
PHPinfo Page

Conclusion – Install LAMP on CentOS 7:

We have installed the CentOS, Apache, MySQL & PHP: the complete LAMP Stack with default configurations.

2 thoughts on “How to Install LAMP on CentOS 7”

Leave a Comment