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
Install some packages need for the following steps
# yum install gcc.x86_64 gcc-c++.x86_64 make.x86_64 wget.x86_64 Install NginxDownload latest version of Nginx at
# wget download/nginx-1.0.5.tar.gzExtract nginx-1.0.5.tar.gz package
# tar zxvf nginx-1.0.5.tar.gz # cd nginx-1.0.5To 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 installYou 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_64When 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 esacChmod nginx with executed permission, enter
# chmod +x /etc/init.d/nginxTo 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
# wgetTo Extract its, enter
# tar zxvf php-5.3.8.tar.gz # cd php-5.3.8To 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 installIf you have problems building PHP then read the .
Next step, copy php.ini to /webserver/php/etc/, enter
Rename php-fpm.conf, enter
# cd /webserver/php/etc # cp php-fpm.conf.default php-fpm.confEdit 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 = 500If 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 ;; esacChmod php-fpm with executed permission, enter
# chmod +x /etc/init.d/php-fpmTo start PHP-FPM for the first time, type the following command
# /etc/init.d/php-fpm start Configure PHP-FPM and Nginx working togetherThe 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.confUncomment 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