Web Servers

How To Install PHP with PHP-FPM For NGINX on CentOS

In this article, we have shown how to install PHP with PHP-FPM for NGINX on CentOS.

For this tutorial, we’ve installed NGINX. This tutorial will cover the basic configuration of the NGINX webserver to process PHP application and server them on port 80. It is recommended you study NGINX and PHP-FPM.

Prerequisites

1. Keep the server up-to-date:

# yum update -y

2. Add EPEL(Extra Packages for Enterprise Linux) repository:

# yum install epel-release

3. Install Remi repository:

# rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

4. Enable PHP version:

Here you can choose which PHP version you want to install.

# yum-config-manager --enable remi-php73

5. Now, install PHP 7.3 along with required PHP modules:

Here you can choose or add PHP modules as per your requirements.

# yum -y install php php-fpm php-mysqlnd php-ctype php-dom php-gd php-iconv php-json php-libxml php-mbstring php-posix php-xml php-zip php-openssl php-zlib php-curl php-fileinfo php-bz2 php-intl php-mcrypt php-ftp php-exif php-gmp php-memcached php-imagick

The PHP is installed.

Configure the PHP Processor

Open PHP configuration file:

# vi /etc/php.ini

In this file you can set memory limit, post max size, date timezone and upload filesize.

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 256M


[Date] ; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone =Asia/Kolkata


post_max_size = 256M


upload_max_filesize = 256M

Next, we are looking for the parameter that sets cgi.fix_pathinfo. We will be uncomment by removing semi-colon(;) and set to 0.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

So, we will change both of these conditions by uncommenting the line and setting it to “0” like this:

cgi.fix_pathinfo=0

Once you done, save and exit.

Next, open the php-fpm configution file:

# vi /etc/php-fpm.d/www.conf

Look for user and group, change the parameter to nginx from apache.

user = nginx
group = nginx

Find listen parameter and change it like:

listen = /var/run/php-fpm/php-fpm.sock

Find lines that set the listen.owner and listen.group and uncomment them. Set to nginx:

listen.owner = nginx
listen.group = nginx

Next, find following line and uncomment them to enable php environment variables.

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

After you done configurion, save and exit.

Now, start our PHP processor:

# systemctl start php-fpm

Enable the PHP processor for persistent:

# systemctl enable php-fpm

That’s it.

In this article, we have seen how our support engineers install PHP with PHP-FPM for NGINX on CentOS.

[Need assistance to fix this error or install tools? We’ll help you.]

Related Articles