Compiling PHP5.x on CentOS 7

Compiling PHP5.x on CentOS 7

Download PHP packages

Go to site Downloads section and select the PHP-6.X.X version that you want to compile (5.6.12 at time of writing this tutorial), select your nearest mirror and download it then upload to your server using rsync or scp. Another simple solution is typing in your console from your server

# wget -O php-5.6.33.tar.gz

Unpacking PHP packages

In the previous step we download a php-5.6.33.tar.gz file so we can unpackage it using tar tool

# tar -zxvf php-5.6.33.tar.gz

Installing GNU Compiler Collection and essential tools

# yum -y install gcc gcc-c++ make automake autoconf bison flex libtool libstdc++-devel

Installing dependencies

We will compile PHP with XML, SSL, PCRE, SQLite, bzip2, curl, GD, mcrypt … support. mcrypt is available in EPEL repo then you need to type

# yum -y install epel-release


# yum -y install libxml2-devel openssl openssl-devel pcre-devel sqlite-devel bzip2-devel \
libcurl-devel libicu-devel gd-devel readline-devel libmcrypt-devel systemd-devel

After this download the shell script from here to install

chmod a+x

Build PHP with FPM support and systemd integration

PHP-FPM (FastCGI Process Manager) is an alternative FastCGI implementation for PHP, bundled with the official PHP distribution since version 5.3.3.

systemd is replacement for SysV initialization. systemd also is a suite of system management daemons, libraries, and utilities designed as a central management and configuration for GNU/Linux operating system.

First create necessary DIR that will be used during compilation process.

# mkdir -p /etc/php/conf.d /usr/lib/php/modules /usr/share/pear

Create PCRE symbolic link to avoid: “configure: error: Could not find libpcre.(a|so) in /usr”

# ln -s /usr/lib64/ /usr/lib/


# ./ -fs php-5.6.33

where -f = FPM support, s = systemd integration


#  cd php-5.6.33 && make install

Creating configuration file

PHP source code comes with 2 .ini files versions: development and production. As we’re compiling for PROD server we type the following command.

# cp -v php.ini-production /etc/php/php.ini

Adjust the ini directives according your needs.

Creating PHP-FPM configuration file

# cp -pv /etc/php/php-fpm.conf.default /etc/php/php-fpm.conf

Edit /etc/php/php-fpm.conf

To allow php-fpm listen on every interface you must look for the listen directive and set its value. Ex change :


to :

listen 9000

Restrict access to php-fpm service. Only specific IPs can connect to php-fpm service, look for listen.allowed_clients directive and set its value to

listen.allowed_clients =,,,

Creating PHP-FPM init service

# cp -v ./sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service

If the user and group under the php-fpm process will be run does not exists then you must created them before start php-fpm service.

# groupadd --system www-data && \ 
useradd --system -m -d /var/www  -s /usr/sbin/nologin -g www-data www-data

Above command create the system group and system user www-data where:

  • -m = create home DIR if not exsist
  • -d = user home DIR
  • -s = login shell (no login shell for this case because www-data was not created for login purpose)
  • -g = what groups this user belongs to.

For security reasons we will change the owner, group and perms for /var/www

# chown root:root -c /var/www/ && chmod 755  /var/www/ 

Start PHP-FPM service at system boot time

# systemctl enable php-fpm

Start PHP-FPM service

# systemctl start php-fpm

Check PHP-FPM status

# systemctl status php-fpm

Create info.php file

# echo '<?php phpinfo();'>/var/www/info.php


NGINX is a web server with excelent performance and low memory footprint also it can be used as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols as well as load balancer. For this example we will used NGINX as reverse proxy for testing our PHP-FPM service.

Installing NGINX

NGINX is available through EPEL repo

# yum -y install epel-release && yum -y install nginx

Edit /etc/nginx/nginx.conf

Look for server section and add the following section

location ~ \.php$ {
            root         /var/www;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_index index.php;
            include fastcgi.conf;
	} is the IP where php-fpm service is listen on

Start NGINX service at system boot time

# systemctl enable nginx

Start NGINX service

# systemctl start nginx

Check NGINX status service

# systemctl status nginx

Opening ports 80 (NGINX) and 9000 (php-fpm)

FirewallD is the default firewall in CentOS 7.

Adding IP range that will access to php-fpm service in trusted zone. For this example we consider entire LAN has access to php-fpm service.

#  firewall-cmd --permanent --zone=trusted --add-source=

Reload firewall rules

# firewall-cmd --reload


Open your browser with the IP where is listening your NGINX server

$ firefox


Now that you have learned how to compile PHP by yourself you can get all advantages (bug fixes, performance improvements and lastest features) for each version of PHP without waiting for the chosen distribution packager.

comments powered by Disqus