There was no single guide that provided this answer, so here’s my own after tinkering around for the entire day on CentOS 5.4.
When I installed CentOS, I did not install a desktop environment, and only added development libraries and tools.
Update your packages
Assuming this is a fresh CentOS install, you’ll want to update your packages:
yum update
Say ‘y’ to every question asked. The update process may take some time depending on the speed of your connection. After this is complete, proceed below:
Download Nginx source
http://wiki.nginx.org/NginxInstall
As of this writing, I downloaded the development 0.8.36 version.
Configre Nginx
Where I have extracted the Nginx source to, I used the compile-time option #3 found at:
http://wiki.nginx.org/NginxInstallOptions
./configure
--prefix=/usr
--conf-path=/etc/nginx/nginx.conf
--http-log-path=/var/log/nginx/access_log
--error-log-path=/var/log/nginx/error_log
--pid-path=/var/run/nginx.pid
--http-client-body-temp-path=/var/tmp/nginx/client
--http-proxy-temp-path=/var/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
--with-md5-asm --with-md5=/usr/include
--with-sha1-asm
--with-sha1=/usr/include
--with-http_realip_module
--with-http_ssl_module
--with-http_perl_module
--with-http_stub_status_module
Compile and Install Nginx
In the same directory, perform
make install
To install Nginx.
Create The Nginx Service File
edit /etc/init.d/nginx in your editor of choice and paste in the following:
#!/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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
Save the file.
Register the nginx service
chmod +x /etc/init.d/nginx
chkconfig nginx on
#This portion exists because nginx will not start without it
mkdir /var/tmp/nginx
http://www.hikaro.com/linux/centos/nginx-init-script.html is the original source with some modifications.
Download PHP and Install
http://www.php.net/downloads.php
Extract the source to a directory.
I have my PHP configured with the following options:
yum install libtool-ltdl-devel
yum install openssl-devel
./configure --with-zlib --with-bz2 --with-curl --with-curlwrappers --enable-exif --enable-ftp --with-gd --with-ldap --enable-mbstring --with-mcrypt --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --enable-soap --enable-sqlite-utf8 --enable-zip --with-openssl
Compile and Install PHP
In the same directory, perform
make install
Copy the php.ini file
Still in the PHP source directory.
cp php.ini-production /etc/php.ini
You should edit the php.ini to match your wanted PHP configuration.
Download Spawn-Fcgi and install
(http://elasticdog.com/2008/02/howto-install-wordpress-on-nginx/ was very useful for this portion)
Spawn-Fcgi comes from the lighttpd project. We are not installing the lighttpd server, but one of the components from the project.
Download spawn-fcgi from:
http://redmine.lighttpd.net/projects/spawn-fcgi/news
As of this writing, I downloaded version 1.6.3. Extract the source to a directory, and change to it.
Configure and install with
./configure
make install
spawn-fcgi should now be installed to /usr/local/bin.
Create the service file for PHP Fastcgi
edit /etc/init.d/fastcgi in the editor of your choice and paste in the following:
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via spawn-fcgi
#
# chkconfig: - 85 15
# description: Run php-cgi as app server
# processname: php-cgi
# config: /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile: /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
spawnfcgi="/usr/local/bin/spawn-fcgi"
php_cgi="/usr/local/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
#####EDIT THIS TO MATCH YOUR USER AND GROUP
server_user=marketdark
server_group=marketdark
############################
server_childs=5
pidfile="/var/run/php_cgi.pid"
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
start() {
[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $"Starting $prog: "
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -- "${php_cgi} -c /etc/php.ini"
retval=$?
echo
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $prog -QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval
}
restart(){
stop
sleep 2
start
}
rh_status(){
status -p ${pidfile} $prog
}
case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
rh_status;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
esac
Save the file.
Register the fastcgi service
chmod +x /etc/init.d/fastcgi
chkconfig fastcgi on
Test:
[root@localhost init.d]# service fastcgi start
Starting php-cgi: spawn-fcgi: child spawned successfully: PID: 9146
Starting fastcgi: [ OK ]
Configure Nginx for FastCGI and PHP
Edit the file /etc/nginx/fastcgi_params
And make sure the file looks like this
(From http://nakedtrout.com/site-performance/nginx-php-fpm-apc-awesome-wordpress-performance/42063
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
Save the file.
Edit the file /etc/nginx/nginx.conf
My file looks like this:
(Source: http://interfacelab.com/nginx-php-fpm-apc-awesome/)
#CHANGE THIS TO THE USER TO RUN THE SERVER UNDER
user marketdark;
#####################
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
#CHANGE THIS TO THE PATH TO YOUR WEB ROOT DIRECTORY
root /var/www/marketdark;
index index.html index.htm index.php;
# if file exists return it right away
if (-f $request_filename) {
break;
}
# otherwise rewrite the fucker
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php$1 last;
break;
}
}
# if the request starts with our frontcontroller, pass it on to fastcgi
location ~ ^/index.php
{
fastcgi_pass 127.0.0.1:9000;
#CHANGE THIS TO THE PATH TO YOUR WEB ROOT DIRECTORY
fastcgi_param SCRIPT_FILENAME /var/www/marketdark$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
}
Start nginx
Test:
[root@localhost init.d]# service nginx start
Starting nginx: [ OK ]
Test your install
Where you have defined your web directory path (where I have it noted #CHANGE THIS TO THE PATH TO YOUR WEB ROOT DIRECTORY) ,
create an index.php file with the following:
Now, figure out the IP of your server and go to it in a browser (for example, since I have my centOS installed on hyper-v under a private NAT, it would be http://http://10.1.10.10/).
At this point you should see the PHP information page!