NGINX – PHP – FastCGI

Installing PHP.
Installed from Dotdeb to resolve an issue with running PHP on the command line.

First setup APT support, edit the sources.list, add 2 lines then get the key and individual PHP packages to avoid loading dependencies:

nano /etc/apt/sources.list
deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add - 

apt-get install --reinstall php5-cgi php5-cli php5-common php5-curl php5-gd php5-imagick php5-imap php5-mcrypt php5-memcache php5-mysql php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-xsl 

apt-get autoremove libsqlite0 libgif4 libgd2-xpm libming1 

Some shared objects are removed so you may need rename these under /etc/php5/conf.d to avoid warnings:

mv idn.ini idn.org
mv ing.ini ming.org
mv suhosin.ini suhosin.org

NGINX configurations:

First I removed Apache from my Debian VPS to save memory, reduce load, save disk space and then installed NGINX and FastCGI support.

List packages installed and filter apache:

dpkg --get-selections | grep apache
/etc/init.d/apache2 stop
apt-get remove --purge apache2*
apt-get install nginx
apt-get install spawn-fcgi

Setup the web directory if needed:

mkdir /var/www
chown www-data:www-data /var/www

I setup 2 instances of FastCGI support:

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9001 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

After you install the NGINX package from Debain or Dotdeb repositories
back up the original configuration first and then save the updated nginx.conf

cd /etc/nginx
cp nginx.conf orgnginx.conf

The main nginx.conf:

user www-data www-data;
worker_processes  2;
#use debug error log to track down issues at install time otherwise just comment it
#error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include		/etc/nginx/mime.types;
    access_log	/var/log/nginx/http.access.log;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    tcp_nodelay        on;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
	#SSL support
	ssl_certificate /etc/nginx/SSL/ssl-unified.crt;
	ssl_certificate_key /etc/nginx/SSL/ssl.key;
    include /etc/nginx/conf.d/*.conf;
	#this is where the VHOSTS are loaded
    include /etc/nginx/sites-enabled/*;
}

To setup vhosts on Debian:

cd /etc/nginx/sites-available
cp default yourdomain.com
cp default www.yourdomain.com

Edit the yourdomain.com and www.yourdomain.com vhost configurations – see examples below:

This is yourdomain.com config for secure vhost #1:

server {
	#This vhost will listen to both http and https connections
	#This vhost only supports xcache status and webmail access
	listen  80;	#http
	listen 443;	#https
	ssl on;	#turn on SSL support
	server_name yourdomain.com;
	root   /var/www/yourdomain.com;
	access_log  /var/log/nginx/yourdomain.com.access.log;
	error_log  /var/log/nginx/yourdomain.com.error.log;
	autoindex off;
	error_page 404 errors/404.html;
	
	#one of these files must exist or a not found error will occur
	index index.php index.html index.htm;

	# static file 404's aren't logged and expires header is set to maximum age
	location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
		access_log off;
		expires max;
	}

	#xcache support
	location /nginx_status {
		stub_status on;
		access_log   off;
		allow 207.119.0.0;
	}

	#webmail support using FastCGI
	location /hastymail2/ {
		location ~ \.php$ {
			access_log  /var/log/nginx/hasty.access.log;
			error_log  /var/log/nginx/hasty.error.log;
			try_files   $uri =404;
			include fastcgi_params;
			fastcgi_pass   127.0.0.1:9000;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}

	location ~ /\.ht {
		deny  all;
	}
}

This is www.yourdomain.com vhost config supporting a mysql gui, XCACHE and WordPress:

server {
	listen	80;	#http
	server_name www.yourdomain.com;
	root   /var/www/www.yourdomain.com;
	access_log  /var/log/nginx/www.yourdomain.com.access.log;
	error_log  /var/log/nginx/www.yourdomain.com.error.log;
	autoindex off;
	error_page 404 errors/404.html;
	index index.php index.html index.htm;
 
	# static file 404's aren't logged and expires header is set to maximum age
	location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
		access_log off;
		expires max;
	}

	#mysql web gui
	location /sqlbuddy/ {
		location ~ \.php$ {
			access_log  /var/log/nginx/sqlbuddy.access.log;
			error_log  /var/log/nginx/sqlbuddy.error.log;
			try_files   $uri =404;
			include fastcgi_params;
			fastcgi_pass   127.0.0.1:9000;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}

	#note: see the fastcgi_pass directive below, I am using
	#a second instance of fastcgi running on a different port
	#because I can  :) 
	
	location /xcache/ {
		location ~ \.php$ {
			access_log  /var/log/nginx/xcache.access.log;
			error_log  /var/log/nginx/xcache.error.log;
			try_files   $uri =404;
			include fastcgi_params;
			fastcgi_pass   127.0.0.1:9001;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}

	location /blog/ {
		try_files $uri $uri/ /blog/index.php?q=$uri&$args;
		location ~ \.php$ {
			access_log  /var/log/nginx/blog.access.log;
			error_log  /var/log/nginx/blog.error.log;
			try_files   $uri =404;
			include fastcgi_params;
			fastcgi_pass   127.0.0.1:9000;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}

	location ~ /\.ht {
		deny  all;
	}
}

Then create the links to enable the new vhosts:

ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/yourdomain.com
ln -s /etc/nginx/sites-available/www.yourdomain.com /etc/nginx/sites-enabled/www.yourdomain.com

Next:

cd /var/www/
mkdir yourdomain.com
mkdir www.yourdomain.com

Do not forget to set file ownership and permissions as needed.
Finally, put some web content into the new directory then test and restart:

nginx -t
service nginx restart

Nginx configuration for friendly WordPress permalinks.
Use this try_files parameter within the location handling your blog

try_files $uri $uri/ /blog/index.php?q=$uri&$args;

Then set WordPress Custom Structure under Settings\Permalinks to /%postname%/

Finally restart the web server:
/etc/init.d/nginx restart

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

The forecast for 81252 by WP Wunderground