Virtualmin with NGINX+PHP-FPM+OPCACHE

By | April 9, 2015

Hi,

After a couple of days figuring how to get this setup working I was able to crack it, so I am sharing it with the world in case someone is in my situation.

Step 1:

I started with a minimum install of CentOS 6.5 64 bits, but it should work with other supported OSes as well. I am using a VPS for my setup.

Step 2:

I also wanted to run the latest PHP version ( 5.6.1 as of today ), so I installed an enable the “Remi” repository. To do that execute the following commands:

a) Get your system ready for virtualmin

yum install wget perl -y

b) Add the “Remi” repository

c) Edit your repository file to enable the “Remi” repository

vi /etc/yum.repos.d/remi.repo
[remi]
name=Les RPM de remi pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
 
......
 
[remi-php56]
name=Les RPM de remi de PHP 5.6 pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/php56/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/php56/mirror
# WARNING: If you enable this repository, you must also enable "remi"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

d) Now install the latest and greatest version of PHP (5.6.1 as of today)

sudo yum install php php-xml php-gd php-imap php-mysql php-odbc php-pear php-pgsql php-snmp php-xmlrpc php-mbstring php-fpm php-opcache -y

e) Verify PHP by running the following command:

[[email protected] ~]# php -v

and that should output:

PHP 5.6.1 (cli) (built: Oct  3 2014 07:28:16) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies

Step 3:

Install Virtualmin, but since PHP is already installed on the system I decide to update the “install.sh” so that only one version of PHP is on the system. I also took this change to remove stuff I don’t use like Postgres.

a) Get Virtualmin

wget http://software.virtualmin.com/gpl/scripts/install.sh

b) Edit the install.sh file

vi install.sh

c) Install Virtualmin and login for the first time to complete the post installation wizard

/bin/sh install.sh

Step 4:

Now it is time to install NGINX and the Webmin modules like it said here:https://www.virtualmin.com/documentation/web/nginx

a) Stop Apache web server

/etc/init.d/httpd stop

b) Download and install NGINX

yum install nginx

c) Start NGINX

/etc/init.d/nginx start

d) Install the Webmin module to manage NGINX

yum install wbm-virtualmin-nginx wbm-virtualmin-nginx-ssl

Step 5:

Configure NGINX to use TCP connections instead of Unix sockets. I know that Unix sockets might bring an extra performance benefit, but until there is a Webmin module to manage NGINX and PHP-FPM I try to keep it as simple as possible. I will explain how to use sockets at the end.

a) Go to Webmin > Server > Ngnix Webserver > Module Config

Change Connect Nginx to PHP processes with from Socket files to TCP connections

Step 6:

Let’s now disable the default way Virtualmin execute PHP (php-cgi) so that when you create a new site it uses PHP-FPM instead of PHP-CGI. I haven’t been able to use PHP-FPM to run under the user account of the site, but since I only have one site hosted it is not a problem for me. You might have to weight your needs against the benefits of this setup.

a) Disable the PHP-CGI call when creating and/or running a site:

vi /usr/libexec/webmin/virtualmin-nginx/virtualmin-nginx-lib.pl

Around line 1705 and 1706 find:

# Launch it, and save the PID
&start_php_fcgi_server_command($d, $cmd, $envs_to_set, $log, $pidfile);

Replace it with:

# Launch it, and save the PID
#&start_php_fcgi_server_command($d, $cmd, $envs_to_set, $log, $pidfile);

Step 7:

Let’s edit the PHP-FPM config file to make change the user from Apache to Nginx.

a) Edit the www.conf file

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

Look for:

; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

Change it with:

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Step 8:

Now at this point PHP-FPM is not running, but your are ready to create your site. This is the only cabeat you have to consider when creating a new site. Since you told NGINX to use a TCP Connection the default connection will look for 127.0.0.1:9000 and that is the port PHP-FPM uses, but if PHP-FPM is running and using the port 9000 when the new site is created it will listed in 127.0.0.1:9001. You can manually adjusted later on, but to keep it simple I stop PHP-PFM when creating a new site.

a) Go ahead and create your new site

b) After this start PHP-PFM like this:

service php-fpm start

c) If you visit your site you will see a 403 error and that is because there is nothing in public_html.

d)Visit /home/SITE-ACCOUNT/public_html and add an index.php file

vi index.php

add and save:

<?php
 
phpinfo();
 
?>

e) Revisit the site and now you should see PHP information page with the latest PHP version and running from PHP-PFM. You are done.

================================================================

Miscellaneous

So, you want to run PHP-FPM from an Unix Socket instead of a TCP connection? No problem.

a) Go to Webmin > Server > Ngnix Webserver > Module Config

Change Connect Nginx to PHP processes with from TCP connections to Socket files

b) Edit the PHP-FPM www.conf file

Add folder for socket

mkdir -p /var/run/php-fpm/

Edit config file

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

Change the following line:

listen = 127.0.0.1:9000

to

listen = /var/run/php-fpm/DOMAINNAME.socket

c) Go to Webmin > Server > Ngnix Webserver > Edit Configuration Files

Update:

fastcgi_pass localhost:9000;

To:

fastcgi_pass unix:/var/run/php-fpm/DOMAINNAME.socket;
Сomments аrchive