Installing LAMP stack in CentOS 6/7

This manual explains how to install the latest version of Apache 2.4MariaDB 10.1 MySQL 5.5 and PHP 5.5/5.6

together with necessary PHP modules in CentOS 7/6.

This combination of operating system (Linux) with web server (Apache), database server (MariaDBMySQL) and scripting language (PHP) is known as LAMP stack.

For this purpose, activate Remi repository and use yum.
 

1: Installing Remi Repository

Remi 
is a repository where you can find the latest versions of the PHP stack (full-featured) for installation in Linux  distributions.
Enter the following commands in the terminal:

CentOS 7:

[root@centos ~]# yum update && yum install epel-release
[root@centos ~]# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

CentOS 6:

[root@centos ~]# yum update && yum install epel-release
[root@centos ~]# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

2: Activating Remi Repository

Now, let's make it sure that Remi Repository is activated and everything is ready to install the selected version of PHP.
Open file /etc/yum.repos.d/remi.repo

[root@centos ~]# vi /etc/yum.repos.d/remi.repo

Set value to 1 in line Enabled in sections [Remi-php55] and [Remi-php56] as specified below:

[remi-php55]
name=Remi's PHP 5.5 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php55/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php55/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php56]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Save the changes and close the editor, for which purpose hold down the left "Shift":"  Symbol ":" will appear at the bottom of the editor. Then enter "wq!" and press "Enter".
The editor will close and save the document.

3: Installing MariaDB / MySQL

In CentOS 7  distribution, MariaDB replaces MySQL, but the system will not allow to install MariaDB from the repository by default.
Therefore, we will install it from the official repository of MariaDB.

For installing MariaDB in CentOS 7:

Create a file named /etc/yum.repos.d/mariadb.repo:

[root@centos ~]# touch /etc/yum.repos.d/mariadb.repo

Open the file for editing:

[root@centos ~]# vi /etc/yum.repos.d/mariadb.repo

And make the following entries:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Save the changes and close the editor, for which purpose hold down the left "Shift":", symbol ":" will appear at the bottom of the editor. Then enter "wq!" and press "Enter". Make the following:

[root@centos ~]# yum --enablerepo=remi install httpd MariaDB-client MariaDB-server php php-common

Done.

For installing MySQL in CentOS 6:

[root@centos ~]# yum --enablerepo=remi install php-mysqlnd php-pgsql php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

Done.

4: Installing PHP modules

For installing PHP, enter the following command in the terminal:

CentOS 7/6:

[root@centos ~]# yum --enablerepo=remi install php-mysqlnd php-pgsql php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

Finish the installation by restarting apache:

[root@centos ~]# service apache2 restart

5: Activating/starting Apache

In Systemd -  CentOS 7: Activate Apache and MariaDB at initial startup

[root@centos ~]# systemctl enable httpd
[root@centos ~]# systemctl enable mariadb

Start Apache and MariaDB

[root@centos ~]# systemctl start httpd
[root@centos ~]# systemctl start mariadb

In SysVinit- RHEL / CentOS 6: Activate Apache and MySQL at initial startup

[root@centos ~]# chkconfig --levels 235 httpd on
[root@centos ~]# chkconfig --levels 235 mysqld on

Start Apache and MySQL

[root@centos ~]# /etc/init.d/httpd start
[root@centos ~]# /etc/init.d/mysqld start

6: Testing PHP 5.5 or 5.6

We will use the classic way of testing PHP.
Create a file named test.php in catalogue  /var/www/html/:

[root@centos ~]# touch /var/www/html/test.php

Open it in editor:

[root@centos ~]# vi touch /var/www/html/test.php

And add the following code lines:

<? PHP<? PHP
<? PHP
phpinfo ();
?>

Phpinfo () - function that shows information regarding the current installation of PHP.

Save the changes and close the editor, for which purpose hold down the left "Shift":" , symbol ":" will appear at the bottom of the editor. Then enter "wq!" and press "Enter".

Now open in your web browser http://[server]/test.php
(change [server] to your domain or IP address of your server)

And check the presence of installed modules and additional software by scrolling the page down

Done! Now you have the up-to-date versions of LAMP stack components installed.