Installing Nginx with PHP-FPM on a Linux server
Nginx (pronounced as Engine-x) is a high performance web server,reverse proxy, mail proxy server which was originally written by Igor Sysoev in 2005. Here we discuss about setting up a very basic nginx web server on your Linux server , capable of parsing php pages.
Installing Nginx on your Linux server
Installing nginx on your Linux server is very simple and even on the first try itself, you will not face any difficulties.
The steps that i have followed on my first nginx installation are given bellow (of course with the help of others tutorials and other resources available from net)
#(Change directory to /usr/local/src [ its a good practice to download and compile software's from /usr/local/src ] ) cd /usr/local/src #(download nginx source from "nginx.org") wget http://nginx.org/download/nginx-0.8.33.tar.gz #(Extract the source file ) tar -xzf nginx-0.8.33.tar.gz cd nginx-0.8.33 #Now configure Nginx , here we are using the 2 basic configuration options [ --sbin-path=/usr/local/sbin #the nginx binary path ] [ --with-http_ssl_module #Enables https module ] ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
since you have included the –with-http_ssl_module there is a possibility of getting the following error ( at least i got this error on a fresh centos machine since no pcre libraries were pre-installed on the system)
checking for PCRE library in /usr/include/pcre/ ... not found checking for PCRE library in /usr/pkg/ ... not found checking for PCRE library in /opt/local/ ... not found ./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.
If you get the above error while configuring nginx , please download pcre (Perl Compatible Regular Expressions) from “http://www.pcre.org/” and change the nginx configure command as given bellow
cd /usr/local/src #(Download the pcre source file) wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.bz2 #(extract the file) tar -xjf pcre-7.9.tar.bz2 cd /usr/local/src/nginx-0.8.33
Now configure nginx again with the following configuration options
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-pcre=/usr/local/src/pcre-7.9
The nginx configuration should now be completed with out any errors, now install nginx
make make install
Nginx is now installed on your machine and you can start the nginx process using the
#[ usually nginx will be started automatically at this stage] /usr/local/sbin/nginx
and to stop nginx , you can use the following command
pkill -9 nginx
If you want to use a daemon for starting and stopping nginx like we are doing in apache , please copy the following script and save this as “/etc/init.d/nginx
!/bin/sh
# nginx - this script starts and stops the nginx daemin
#chkconfig: - 85 15
#description: Nginx is an HTTP(S) server, HTTP(S) reverse \
proxy and IMAP/POP3 proxy server
#processname: nginx
#config: /usr/local/nginx/conf/nginx.conf
#pidfile: /usr/local/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="/usr/local/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/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
You can also download the above script from our server using the following method
wget http://sparksupport.com/nginx/startupscript.txt -O /etc/init.d/nginx
Which will download the file from our server and will save it to /etc/init.d/nginx
Change the file permission to 755
chmod 755 /etc/init.d/nginx
Now you can use the downloaded nginx daemon script to start and stop nginx server
/etc/init.d/nginx start ( To start the web server) /etc/init.d/nginx stop ( To stop the web server)
Now if you enter your server ip on the browser , you will see a page, which is the default page for nginx
Now you need the nginx to parse php pages so that you can host your php pages on this nginx server ,for that we need to install a fastcgi process manager on our server
PHP-FPM
The typical nginx configuration involves using spawn-fcgi from the Lighttpd project to get nginx serving up PHP. There are a few issues with spawn-fcg like the spawn-fcgi hangs after a few hours. So here we will use the php-fpm (php fastcgi process manager) which is actually a set of patches to php for an alternative fastcgi-process manager which can be safely used in busy sites with out having the fear of hang issues or crash issues.
More details about php-fpm can be found on “http://php-fpm.org/”
Installing PHP-FPM
Download the php source from “http://php.net”
cd /usr/local/src wget http://museum.php.net/php5/php-5.2.8.tar.bz2 [here we are using php-5.2.8 , if you want to use higher versions of php , then you need to use the corresponding php-fpm version] tar -xjf php-5.2.8.tar.bz2 Download the php-fpm package from "http://php-fpm.org/" wget http://php-fpm.org/downloads/php-5.2.8-fpm-0.5.10.diff.gz gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | sudo patch -d php-5.2.8 -p1 ( patching the php source ) cd php-5.2.8 configuring php ./configure --enable-fastcgi --enable-fpm --with-mcrypt --enable-mbstring --with- mysql=/usr/include /mysql --with-mysql-sock=/tmp/mysql.sock --with-curl --with-sockets --with-gd --with-zlib --with-iconv --with-jpeg-dir=/usr/lib make make install
Now open the php-fpm.conf in your favourite text editior (here we use vi)
vi /usr/local/etc/php-fpm.conf
Search for “Unix group of processes” and ” Unix user of processes” and add the user nobody as given bellow [by default nobody will be the user and group given in the conf, and you will only be required to un comment the following lines on the conf]
Unix user of processes nobody Unix group of processes nobody
please note that the nobody user is the nginx user mentioned in your nginx conf , please refer your nginx conf to verify this now go to your nginx default document root
cd /usr/local/nginx/html
now please create a phpinfo( ) page and save it as index.php
now open the nginx.conf and add these lines to the configuration
location ~ \.php$ {
root /usr/local/nginx/html; # The directory on which the php pages are placed
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Usually the above lines will be present in the nginx conf by default itself, you just need to un comment the above lines from your nginx conf .Also if you get a “NO INPUT” message on the browser , please make sure that the path given in the nginx conf(/usr/local/nginx/html), are correct. That is on the following 2 lines
root /usr/local/nginx/html; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name
now restart nginx and start php-fpm
/etc/init.d/nginx restart php-fpm start
you can verify that your php-fpm is running by the following netstat command
netstat -pant |grep 9000 //the php-fpm runs on the port 9000 and from the nginx we transfer the request to this port internally
Now enter your ipaddres/index.php on your browser
http://your-ip-address/index.php please replace your-ip-address with your ip address
which will show the php info page like the one given bellow.
By following the above steps you will be able to setup a basic nginx web server that is capable of parsing php pages within few minutes
If you face any difficulties in setting up an nginx server using the above methods , please let us know that




Hello
Thank you very much for posting this tutorial. However, I have one problem due to which it is not working for me.
Before executing /etc/init.d/nginx restart
I have to execute service httpd stop then only
/etc/init.d/nginx restart works
That’s OK
but when I browse http://www.mydomain.com, it still shows that “Welcome to nginx” page and not my site pages (i.e. my already uploaded php pages)
Yes, I have made nginx install on my existing server and my files are in
/home/….. directory.
I believe that putting “root /usr/local/nginx/html;” in nginx.conf leads to this index.html
Kindly let me know how can I configure newly installed nginx to resolve to my existing php pages of existing domain.
Looking for your response.
Thank you
Hi Hemang,
First of all thanks for reading this post
And from your querry I believe what the issue is that both your apache and nginx instances are configured to listen on
port “80″ on your server/machine.
That is why you were not able to start the nginx while your httpd is running, so you have stopped the apache process by
“service httpd command” , and once after that you will be able to start nginx .
But Your current web pages are configured only in the apache configuration “httpd.conf” , so that is why you are
redirecteted to the default nginx page since there is no such web pages are configured in the nginx conf.
So to resolve this you will either be required to change the nginx port from the nginx configuration so that it will listen on
any other port other than 80 and you will be able to run both apache and nginx the same time . Another method is to
configure nginx to use this webpages that is to add host entries in the nginx configuration
But i believe the former option will be apt for you because you already have a completely working apache instance .
But if you preffer nginx than apache then you have to configure the nginx with the host configurations and may be some
modules if your web pages/application requires . But I will preffer running both servers simultaneoulsy in your case
The Port change method is given bellow
To change the port on which the nginx daemon listen, please follow these steps
1) Stop the nginx service(if running)
/etc/init.d/nginx stop
2) open your nginx main configuration file in a text editor ( if you have followed the installation directory that i’v
showed in the post then please open “/usr/local/nginx/conf/nginx.conf”)
3) go to the “server” directive in the “http” host entry , this will look like
======================================================
in that file , only the required ones are shown
http {
include mime.types;
default_type application/octet-stream;
* * * * **
**
*
server {
listen 80;
server_name localhost;
* * * * **
**
*
}
[please note i have not included all those junk parts
above ]
4) In that sever directive you can see the Listen attribute , please change it to any other port other than
80 (as i told you earlier you need the 80 port for apache )
suppose if you want to use the port 81 for your nginx then your server directive should be like
server {
listen 81;
server_name localhost;
. . . . . .
. . . . . . .
…
.
}
5) once changed the listen port then save the configuration , and start nginx
/etc/init.d/nginx start
6) Now test the nginx installation from your browser
http://your-up-address:81
example
——–
http://192.168.1.2:81
7) Now you can get both nginx and your native apache instance working together, and all your current webpages
will be working fine as well as the new nginx server
PS: if your apache daemon is not running (may be you have stopped that for the nginx service) , then please start the
apache process to get your current webpages working
If you find the above reply not useful , or if you still facing any issue , please let me know that
Have a great day
Hello Vishnuprasad,
Thanks a lot for replying.
However, my concerned is this.
I have my ready made website running on apache.
Now, if I install nginx (as discussed above), WILL I BE ABLE TO USE NGINX SERVER FOR MY EXISTING WEB SITE PAGES?
because,
Now test the nginx installation from your browser
http://your-up-address:81
will open that default nginx page only
and when I will browse
http://www.mydomain.com
It will still go through port 80 and apache will handle request and open my website, won’t it?
Kindly clarify.
Thanks for your great tutorial and help once again.
Hi Hemang
You are absolutely correct , once you made the port changes in the nginx conf as i mentioned in my last reply , you will be able to use both apache n nginx the same time on your server
so http://yourdomain.com:81 will lead to the nginx page
and http://yourdomain.com will lead to the current website running on apache
Regards
Vishnuprasad
Thanks really very nice post.. very easy and helpful post.