LAMP, When we talk about PHP based Web-Server, we mostly used to think to work on Linux based machine installed Apache, PHP front-end code and Database stored in MySQL.

In this post we will see how we could install and work LAMP on CentOS7.

Setup

For this post we have updated CentOS7 as of today.

# uname  -r
3.10.0-514.16.1.el7.x86_64
[root@srv7 ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr 12 2017 21:03:28

# php -v
PHP 5.4.16 (cli) (built: Nov  6 2016 00:29:02) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

For Installation of various packages in CentOS7, we need to use CentOS repository using yum package manager. Learning yum is good for Linux Administrator for day to day activity.

Install Apache

Apache is one of most famous Web-Server. We could install it in below way.

# yum install httpd

Dependencies Resolved

================================================================================
 Package           Arch         Version                     Repository     Size
================================================================================
Installing:
 httpd             x86_64       2.4.6-45.el7.centos.4       updates       2.7 M
Installing for dependencies:
 apr               x86_64       1.4.8-3.el7                 base          103 k
 apr-util          x86_64       1.5.2-6.el7                 base           92 k
 httpd-tools       x86_64       2.4.6-45.el7.centos.4       updates        84 k
 mailcap           noarch       2.1.41-2.el7                base           31 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Is this ok [y/d/N]: y

Installed:
  httpd.x86_64 0:2.4.6-45.el7.centos.4                                          

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                      apr-util.x86_64 0:1.5.2-6.el7   
  httpd-tools.x86_64 0:2.4.6-45.el7.centos.4    mailcap.noarch 0:2.1.41-2.el7   

Complete!

In my case there are dependencies, it could possible that in your case it could some other, it depend upon earlier system state.

Let’s start Apache service through systemctl. systemctl is method to manage services in RHEL/CentOS 7.

[root@srv7 ~]# ss -ntl| grep 80

[root@srv7 ~]# systemctl start httpd

[root@srv7 ~]# ss -ntl| grep 80
LISTEN     0      128         :::80                      :::*
                  
root@srv7 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

Let check service

# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-08-01 10:23:39 IST; 30s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 13831 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─13831 /usr/sbin/httpd -DFOREGROUND
           ├─13832 /usr/sbin/httpd -DFOREGROUND
           ├─13833 /usr/sbin/httpd -DFOREGROUND
           ├─13834 /usr/sbin/httpd -DFOREGROUND
           ├─13835 /usr/sbin/httpd -DFOREGROUND
           └─13836 /usr/sbin/httpd -DFOREGROUND

Aug 01 10:23:38 srv7 systemd[1]: Starting The Apache HTTP Server...
Aug 01 10:23:39 srv7 httpd[13831]: AH00558: httpd: Could not reliably deter...ge
Aug 01 10:23:39 srv7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

# systemctl is-enabled httpd
enabled

we could browse Apache test page in browser like below, this way it we could confirm that Apache is install and running in correct way.

Installation of MySQL (MariaDB)

As we saw Web Server is running and working fine. Now we need to install MariaDB and configure it for this.

Yum is same tool that we need to use for MariaDB installation, MariaDB is officially used in RHEL/CentOS 7 and present in its repository.

# yum install mariadb mariadb-server


Dependencies Resolved

================================================================================
 Package                      Arch        Version               Repository
                                                                           Size
================================================================================
Installing:
 mariadb                      x86_64      1:5.5.52-1.el7        base      8.7 M
 mariadb-server               x86_64      1:5.5.52-1.el7        base       11 M
Installing for dependencies:
 perl-Compress-Raw-Bzip2      x86_64      2.061-3.el7           base       32 k
 perl-Compress-Raw-Zlib       x86_64      1:2.061-4.el7         base       57 k
 perl-DBD-MySQL               x86_64      4.023-5.el7           base      140 k
 perl-DBI                     x86_64      1.627-4.el7           base      802 k
 perl-Data-Dumper             x86_64      2.145-3.el7           base       47 k
 perl-IO-Compress             noarch      2.061-2.el7           base      260 k
 perl-Net-Daemon              noarch      0.48-5.el7            base       51 k
 perl-PlRPC                   noarch      0.2020-14.el7         base       36 k

Transaction Summary
================================================================================
Install  2 Packages (+8 Dependent packages)

Total download size: 21 M
Installed size: 107 M
Is this ok [y/d/N]: y

Installed:
  mariadb.x86_64 1:5.5.52-1.el7       mariadb-server.x86_64 1:5.5.52-1.el7      

Dependency Installed:
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7                                  
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7                                   
  perl-DBD-MySQL.x86_64 0:4.023-5.el7                                           
  perl-DBI.x86_64 0:1.627-4.el7                                                 
  perl-Data-Dumper.x86_64 0:2.145-3.el7                                         
  perl-IO-Compress.noarch 0:2.061-2.el7                                         
  perl-Net-Daemon.noarch 0:0.48-5.el7                                           
  perl-PlRPC.noarch 0:0.2020-14.el7                                             

Complete!

In my case there are dependencies, it could possible that in your case it could some other, it depend upon earlier system state.

Let’s start MariaDB Service.

[root@srv7 ~]# ss -ntl| grep 3306

[root@srv7 ~]# systemctl start mariadb

[root@srv7 ~]# ss -ntl| grep 3306
LISTEN     0      50           *:3306                     *:*                  

[root@srv7 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

Let’s check status of service.

# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-08-01 11:35:56 IST; 1min 49s ago
 Main PID: 14337 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─14337 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─14495 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysq...

Aug 01 11:35:54 srv7 systemd[1]: Starting MariaDB database server...
Aug 01 11:35:54 srv7 mysqld_safe[14337]: 170801 11:35:54 mysqld_safe Logging....
Aug 01 11:35:54 srv7 mysqld_safe[14337]: 170801 11:35:54 mysqld_safe Startin...l
Aug 01 11:35:56 srv7 systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

# systemctl is-enabled mariadb
enabled

There are some basic security steps for MariaDB, which is good to implement on new Database Server.

# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

Remove anonymous users? [Y/n] y
 ... Success!

Disallow root login remotely? [Y/n] y
 ... Success!

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reload privilege tables now? [Y/n] y
 ... 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!

Now we could connect MariaDB MySQL shell. This way we can confirm that MariaDB installed and working.

# 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.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye

Installation of PHP

Initially we need to PHP packages for LAMP. But PHP has wide range packages which provide new features with PHP codes, these packages install as per application requirement. Let install PHP first.

# yum install php
Dependencies Resolved

================================================================================
 Package             Arch            Version                Repository     Size
================================================================================
Installing:
 php                 x86_64          5.4.16-42.el7          base          1.4 M
Installing for dependencies:
 libzip              x86_64          0.10.1-8.el7           base           48 k
 php-cli             x86_64          5.4.16-42.el7          base          2.7 M
 php-common          x86_64          5.4.16-42.el7          base          564 k

Transaction Summary
================================================================================
Install  1 Package (+3 Dependent packages)

Total size: 4.7 M
Total download size: 4.6 M
Installed size: 17 M
Is this ok [y/d/N]: y

Installed:
  php.x86_64 0:5.4.16-42.el7                                                    

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7             php-cli.x86_64 0:5.4.16-42.el7       
  php-common.x86_64 0:5.4.16-42.el7       

Complete!

Installation of PHP is quit customize and there are various PHP packages for various features, like below.

# yum -q search php-
======================== N/S matched: php- ===================================
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Enchant spelling extension for PHP applications
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-intl.x86_64 : Internationalization extension for PHP applications
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol

we could also find out detail information about these packages. like below.

# yum -q info php-bcmath.x86_64
Available Packages
Name        : php-bcmath
Arch        : x86_64
Version     : 5.4.16
Release     : 42.el7
Size        : 57 k
Repo        : base/7/x86_64
Summary     : A module for PHP applications for using the bcmath library
URL         : http://www.php.net/
License     : PHP and LGPLv2+
Description : The php-bcmath package contains a dynamic shared object that will
            : add support for using the bcmath library to PHP.

So install these packages as per application requirement.

Now we need to setup PHP in apache in such way that PHP application could through it. By default it is a installation task of PHP package.

When PHP installed it placed one module file in Apache configuration in such way that whenever we use Apache to host, it automatically support PHP functions. We can see this thing though one single command.

# egrep "ServerRoot|onf.modules.d" /etc/httpd/conf/httpd.conf| grep  -v ^#
ServerRoot "/etc/httpd"
Include conf.modules.d/*.conf


# rpm -qf /etc/httpd/conf.modules.d/10-php.conf 
php-5.4.16-42.el7.x86_64

# apachectl -t -D DUMP_MODULES| grep -i php
 php5_module (shared)

Above module file is mentioned in Apache configuration file, all module file present in /etc/httpd/conf.modules.d are in effect in Apache.

# apachectl -L| grep -A 5 -i php
php_value (mod_php5.c)
	PHP Value Modifier
	Allowed in *.conf anywhere and in .htaccess
	when AllowOverride includes Options
php_flag (mod_php5.c)
	PHP Flag Modifier
	Allowed in *.conf anywhere and in .htaccess
	when AllowOverride includes Options
php_admin_value (mod_php5.c)
	PHP Value Modifier (Admin)
	Allowed in *.conf anywhere
php_admin_flag (mod_php5.c)
	PHP Flag Modifier (Admin)
	Allowed in *.conf anywhere
PHPINIDir (mod_php5.c)
	Directory containing the php.ini file
	Allowed in *.conf only outside , , , or 

We could also check it in another way. Create a file in Server DocumentRoot.

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

Now, try to access http://IP/info.php. Will show page like below. Provide detail information on PHP running through Apache.