How To Install Nginx And PHP (PHP-FPM) On CentOS 6

by lifeLinux on September 15, 2011

In this article, I’ll go over the steps of how to install Nginx and PHP (PHP-FPM) working together on CentOS 6. To start I used clean version of CentOS 6.

The first step is update CentOS to latest version, type the following command

# yum update -y

Install some packages need for the following steps

# yum install gcc.x86_64 gcc-c++.x86_64 make.x86_64 wget.x86_64

Install Nginx

Download latest version of Nginx at http://nginx.org/

# wget http://nginx.org/download/nginx-1.0.5.tar.gz

Extract nginx-1.0.5.tar.gz package

# tar zxvf nginx-1.0.5.tar.gz
# cd nginx-1.0.5

To build nginx, type the following command

# ./configure --prefix=/webserver/nginx --without-mail_pop3_module --without-mail_imap_module  --without-mail_smtp_module --with-http_ssl_module  --with-http_stub_status_module  --with-http_gzip_static_module
# make
# make install

You may get some errors like the following

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

To fix it, enter

# yum install pcre-devel.x86_64
/configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.

To fix it, enter

# yum install openssl.x86_64 openssl-devel.x86_64

When build process completed successfully, it’ll display as follows

  Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/webserver/nginx"
  nginx binary file: "/webserver/nginx/sbin/nginx"
  nginx configuration prefix: "/webserver/nginx/conf"
  nginx configuration file: "/webserver/nginx/conf/nginx.conf"
  nginx pid file: "/webserver/nginx/logs/nginx.pid"
  nginx error log file: "/webserver/nginx/logs/error.log"
  nginx http access log file: "/webserver/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

If you want to run Nginx by default when the system boots, create file called nginx in /etc/init.d/ with following content

#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/webserver/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/webserver/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
	[ -x $nginx ] || exit 5
	[ -f $NGINX_CONF_FILE ] || exit 6
	echo -n $"Starting $prog: "
	daemon $nginx -c $NGINX_CONF_FILE
	retval=$?
	echo
	[ $retval -eq 0 ] && touch $lockfile
	return $retval
}

stop() {
	echo -n $"Stopping $prog: "
	killproc $prog -QUIT
	retval=$?
	echo
	[ $retval -eq 0 ] && rm -f $lockfile
	return $retval
}

restart() {
	configtest || return $?
	stop
	start
}

reload() {
	configtest || return $?
	echo -n $”Reloading $prog: ”
	killproc $nginx -HUP
	RETVAL=$?
	echo
}

force_reload() {
	restart
}

configtest() {
	$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
	status $prog
}

rh_status_q() {
	rh_status >/dev/null 2>&1
}

case "$1" in
	start)
		rh_status_q && exit 0
		$1
	;;
	stop)
		rh_status_q || exit 0
		$1
	;;
	restart|configtest)
		$1
	;;
	reload)
		rh_status_q || exit 7
		$1
	;;
	force-reload)
		force_reload
	;;
	status)
		rh_status
	;;
	condrestart|try-restart)
		rh_status_q || exit 0
	;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
		exit 2
esac

Chmod nginx with executed permission, enter

# chmod +x /etc/init.d/nginx

To start Nginx for the first time, type the following command

# /etc/init.d/nginx start

Install PHP (PHP-FPM)

Download latest version of PHP at http://download.php.net

# wget http://www.php.net/get/php-5.3.8.tar.gz/from/ca.php.net/mirror

To Extract its, enter

# tar zxvf php-5.3.8.tar.gz
# cd php-5.3.8

To build PHP, I’ll create bash file called build.sh with following content

#!/bin/sh
"./configure" \
"--prefix=/webserver/php" \
"--enable-fpm" \
"--with-libdir=lib64" \
"--with-bz2" \
"--with-config-file-path=/webserver/php/etc" \
"--with-config-file-scan-dir=/webserver/php/etc/php.d" \
"--with-curl=/usr/local/lib" \
"--with-gd" \
"--with-gettext" \
"--with-jpeg-dir=/usr/local/lib" \
"--with-freetype-dir=/usr/local/lib" \
"--with-kerberos" \
"--with-mcrypt" \
"--with-mhash" \
"--with-mysql" \
"--with-mysqli" \
"--with-pcre-regex=/usr" \
"--with-pdo-mysql=shared" \
"--with-pdo-sqlite=shared" \
"--with-pear=/usr/local/lib/php" \
"--with-png-dir=/usr/local/lib" \
"--with-pspell" \
"--with-sqlite=shared" \
"--with-tidy" \
"--with-xmlrpc" \
"--with-xsl" \
"--with-zlib" \
"--with-zlib-dir=/usr/local/lib" \
"--with-openssl" \
"--with-iconv" \
"--enable-bcmath" \
"--enable-calendar" \
"--enable-exif" \
"--enable-ftp" \
"--enable-gd-native-ttf" \
"--enable-libxml" \
"--enable-magic-quotes" \
"--enable-soap" \
"--enable-sockets" \
"--enable-mbstring" \
"--enable-zip" \
"--enable-wddx"

Type the following command to install PHP

# sh build.sh
# make
# make install

If you have problems building PHP then read the article.
Next step, copy php.ini to /webserver/php/etc/, enter

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

Rename php-fpm.conf, enter

# cd /webserver/php/etc
# cp php-fpm.conf.default php-fpm.conf

Edit php-fpm.conf with following content

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nobody
group = nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

If you want to run PHP-FPM by default when the system boots, create file called php-fpm in /etc/init.d/ with following content

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

php_fpm_BIN=/webserver/php/sbin/php-fpm
php_fpm_CONF=/webserver/php/etc/php-fpm.conf
php_fpm_PID=/webserver/php/var/run/php-fpm.pid

php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
	try=0

	while test $try -lt 35 ; do

		case "$1" in
			'created')
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;

			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN $php_opts

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		fi

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-exit"
			exit 1
		else
			echo " done"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	*)
		echo "Usage: $0 {start|stop|force-quit|restart|reload}"
		exit 1
	;;

esac

Chmod php-fpm with executed permission, enter

# chmod +x /etc/init.d/php-fpm

To start PHP-FPM for the first time, type the following command

# /etc/init.d/php-fpm start

Configure PHP-FPM and Nginx working together

The configuration file for Nginx is located at /webserver/nginx/conf/nginx.conf. To edit nginx.conf type the following command

# vi /webserver/nginx/conf/nginx.conf

Uncomment the following lines

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

Type the following command to restart Nginx

# /etc/init.d/nginx restart

Related Posts:

{ 1 trackback }

How To Install Nginx And PHP-FPM On CentOS 6 Via Yum
March 30, 2012 at 12:53 pm

{ 21 comments… read them below or add one }

Kyle September 28, 2011 at 5:15 am

Thanks for the guide! It worked almost flawlessly, but I had some issues with the PDO extension. “=shared” should be removed from everything in the build.sh script. pdo-sqlite is also enabled by default so it can be removed. Here’s the updated version:

#!/bin/sh
“./configure” \
“–prefix=/webserver/php” \
“–enable-fpm” \
“–with-libdir=lib64” \
“–with-bz2” \
“–with-config-file-path=/webserver/php/etc” \
“–with-config-file-scan-dir=/webserver/php/etc/php.d” \
“–with-curl=/usr/local/lib” \
“–with-gd” \
“–with-gettext” \
“–with-jpeg-dir=/usr/local/lib” \
“–with-freetype-dir=/usr/local/lib” \
“–with-kerberos” \
“–with-mcrypt” \
“–with-mhash” \
“–with-mysql” \
“–with-mysqli” \
“–with-pcre-regex=/usr” \
“–with-pdo-mysql” \
“–with-pear=/usr/local/lib/php” \
“–with-png-dir=/usr/local/lib” \
“–with-pspell” \
“–with-sqlite” \
“–with-tidy” \
“–with-xmlrpc” \
“–with-xsl” \
“–with-zlib” \
“–with-zlib-dir=/usr/local/lib” \
“–with-openssl” \
“–with-iconv” \
“–enable-bcmath” \
“–enable-calendar” \
“–enable-exif” \
“–enable-ftp” \
“–enable-gd-native-ttf” \
“–enable-libxml” \
“–enable-magic-quotes” \
“–enable-soap” \
“–enable-sockets” \
“–enable-mbstring” \
“–enable-zip” \
“–enable-wddx”

Reply

AJ October 5, 2011 at 5:45 am

Wow, excellent guide. Exactly what I was looking for. Most guides don’t show how to compile both nginx and php from source. Your guide really saved me a lot of time and effort (and frantic google searches). Thanks!

Reply

AJ October 11, 2011 at 4:31 am

Hi,
I followed your guide and everything worked perfectly except “curl” in php. I used the exact same instructions as you including “–with-curl=/usr/local/lib” but just doesn’t work. Any ideas?

Reply

Jim October 18, 2011 at 9:30 pm

nice tutorial. I found it on google and it worked ok, however I found 2 small errors in your documentation.
First when you install php:
# wget wget http://www.php.net/get/php-5.3.8.tar.gz/from/ca.php.net/mirror
you wrote twice wget

and second on this:
# cd /webserver/php
# cp php-fpm.conf.default php-fpm.conf

you need to add the path etc or else won’t find the file:
something like this:
# cd /webserver/php/etc
# cp php-fpm.conf.default php-fpm.conf

Keep up the good work

Reply

lifeLinux October 20, 2011 at 8:19 am

I updated. Thanks Jim 🙂

Reply

Jim October 25, 2011 at 11:00 am

also you should make a nice tutorial about how to update nginx , php to latest version, in case you have older versions. Might be useful

Reply

Alex Driantsov April 17, 2012 at 10:38 am

Jim, upgrading nginx is as easy and safe comparing to other software. Nginx processes are well controlled using system signals, I dont want to copy and paste other authors, there are hundreds articles on this question, so just give Google a try 😉

Reply

Alex December 6, 2011 at 5:16 am

Not worked!! (

Reply

t8xx January 11, 2012 at 6:56 pm

What about the user ? if i do this with root my nginx will start as root ?

Reply

admin January 12, 2012 at 6:38 am

If the master process is run as root, then nginx will setuid()/setgid() to USER/GROUP. If GROUP is not specified, then nginx uses the same name as USER. By default it’s nobody user and nobody or nogroup group or the –user=USER and –group=GROUP from the ./configure script.

Reply

t8xx January 12, 2012 at 10:27 pm

really nice guide, i am trying to start my nginx server second night right now 🙂 but something is going not well and i dont know what. i could compile nginx and php and installed and mysql . php and nginx from source. yesterday i did only php and naix with no settup of php-cgi today i finished the setting of php 🙂 but anyway i can’t open a simple html file with my nginx 🙁 and i dont know whats wrong. Maybe my permissions on the www folder in /var/www/default is my default webfolder. can u tell me what permissions and on what user to set for /var/www and /var/www/default folders ? and the index.html file inside . thanks a lot. and again congrats for the nice tutorial.

Reply

Long January 17, 2012 at 6:55 am

Hi, the nginx’s home default is /webserver/nginx/html in this tutorial. You need chmod to 755 with following command: chmod -R 755 /webserver/nginx/html

Reply

Felix MC February 13, 2012 at 9:36 pm

on CentOS 32-bit you’ll have to change ““–with-libdir=lib64″ \” to ““–with-libdir=lib″ \” otherwise you’ll get “pcre.so.0 not found”

Reply

Kane Hart March 10, 2012 at 9:05 pm

Followed the instructions to the letter as far as I know. The html portion seems to work fine but the php one is purely blank and when you shutdown php then you get the page errors. But if you fake say fesdfrsdg.php it’s blank also anything with .php is blank extension. Then when php off and you go to a real .php then you still get the error page kind of funny heh.

Reply

Alex Driantsov April 17, 2012 at 10:34 am

After you create rc scripts you should also add them to system using:

$ chkconfig –add nginx
$ chkconfig –add php-fpm

also you may decide to disable httpd and set nginx/php-fpm services to be started on boot, so you will need:

$ chkconfig –levels 2345 httpd off

to disable httpd and

$chkconfig –levels 345 nginx on
$chkconfig –levels 345 php-fpm on

to enable nginx and php-fpm.

Reply

Ahmet KAPIKIRA June 15, 2012 at 8:31 am

Thank u. Very nice article.

Reply

Jim September 19, 2012 at 12:49 am

Hi. Your tutorial is great and worked 100%, however when I did the same steps with php 5.4.X (5.4.7 to be more specific) the connection with mysql is not working and when I start the php-fpm using service php-fpm start , simply blocks my console.

As I said the same thing,same computer same configuration works ok with php 5.3.x

In case you have some time maybe you should look to see what’s the problem and make another post .

Thanks

Reply

John M November 28, 2012 at 3:59 am

Why compile PHP and nginx from source when yum repositories work without any of this hassle (understood if you had custom make comments or patches but doesn’t look like it here)

Reply

NoNeedForAName September 16, 2013 at 11:03 am

Wait until you try to install something that requires a nginx third party module. I’m incredulous that your repos will have it recompiled.

Reply

NoNeedForAName September 16, 2013 at 11:03 am

s/recompiled/precompiled

Reply

Lampk April 6, 2014 at 4:25 pm

Use yum to install the latest version of nginx and php-fpm is better, you can update very easy,do need to compile for each update.
Install the centalt or atomic repos, and you can get lastest version of nginx.
Install ius repos,you can get lastest php-fpm or php.

Reply

Previous post:

Next post: