LNMP 方案之使用 HevSCGIServer CGI 模块连接 Nginx 和 PHP-CGI

HevSCGIServer CGI 模块可用于实现 SCGI 协议转 CGI 协议,执行 CGI 程序。它将通过创建子进程的方式执行 CGI 程序,在进程间通信方式上是将 socket 映射到 standard i/o,有着比较优秀的性能,同时对内存需求也比较小,适合虚拟主机方案使用。

目录结构配置可参考: LNMP 多用户动态进程管理虚拟主机方案

Nginx 配置文件

# vhost
# Heiher <admin@heiher.info>
 
# HTTP Server
server {
 
	listen   80; ## listen for ipv4
 
	server_name  localhost;
 
	access_log  /var/web/USERNAME/home/web/logs/access.log;
	error_log  /var/web/USERNAME/home/web/logs/error.log;
 
	location / {
		root   /var/web/USERNAME/home/web/www;
		index  index.html index.htm index.php;
		## Rewrite
		if (!-e $request_filename)
		{
			rewrite ^(.+)$ /index.php?q=$1 last;
		}
		omallow USERNAME sftp; # 允许访问隶属于 USERNAME:sftp 的文件
		omdeny all;            # 禁止访问其它所有文件
	}
 
	# pass the PHP scripts to FastCGI server listening on socket file
	#
	location ~ \.php$ {
		if (!-e $request_filename) {
			return 404;
		}
                scgi_pass   unix:/var/web/USERNAME/run/hev-scgi-server.sock;
                scgi_param  REDIRECT_STATUS 200;
                scgi_param  SCRIPT_FILE /usr/bin/php-cgi;
                scgi_param  SCRIPT_FILENAME /var/web/USERNAME/home/web/www/$document_uri;
                include     scgi_params;
	}
 
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny  all;
	}
}
 
# HTTPS Server
server {
 
	listen   443; ## listen for ipv4
 
	server_name  localhost;
 
	ssl  on;
	ssl_certificate  /var/web/USERNAME/cert/cert.pem;
	ssl_certificate_key  /var/web/USERNAME/cert/cert.key;
 
	ssl_session_timeout  5m;
 
	ssl_protocols  SSLv3 TLSv1;
	ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
	ssl_prefer_server_ciphers   on;
 
	access_log  /var/web/USERNAME/home/web/logs/access.log;
	error_log  /var/web/USERNAME/home/web/logs/error.log;
 
	location / {
		root   /var/web/USERNAME/home/web/www;
		index  index.html index.htm index.php;
		## Rewrite
		if (!-e $request_filename)
		{
			rewrite ^(.+)$ /index.php?q=$1 last;
		}
		omallow USERNAME sftp;
		omdeny all;
	}
 
	# pass the PHP scripts to FastCGI server listening on socket file
	#
	location ~ \.php$ {
		if (!-e $request_filename) {
			return 404;
		}
                scgi_pass   unix:/var/web/USERNAME/run/hev-scgi-server.sock;
                scgi_param  REDIRECT_STATUS 200;
                scgi_param  SCRIPT_FILE /usr/bin/php-cgi;
                scgi_param  SCRIPT_FILENAME /var/web/USERNAME/home/web/www/$document_uri;
		scgi_param  HTTPS on;
                include     scgi_params;
	}
 
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny  all;
	}
}

HevSCGIServer 编译安装
1. 下载源代码

mkdir hev-scgi-server-framework
cd hev-scgi-server-framework
git clone git://gitcafe.com/heiher/hev-scgi-server-library.git
git clone git://gitcafe.com/heiher/hev-scgi-server.git
git clone git://gitcafe.com/heiher/hev-scgi-server-cgi.git

2. 编译

cd hev-scgi-server-library
make
cd ..
 
cd hev-scgi-server
make
cd ..
 
cd hev-scgi-server-cgi
make
cd ..

3. 安装

mkdir -p /opt/hev-scgi-server/{bin,conf,modules}
cp hev-scgi-server/bin/* hev-scgi-server-library/bin/* /opt/hev-scgi-server/bin/
cp hev-scgi-server-cgi/bin/* /opt/hev-scgi-server/modules/
cp hev-scgi-server/conf/main.conf /opt/hev-scgi-server/conf/

HevSCGIServer 配置
conf/main.conf

[Server]
Address=unix:/var/web/USERNAME/run/hev-scgi-server.sock
ModuleDirPath=./modules

conf/modules.conf

[Module0]
Alias=HevSCGIHandlerPHP
Pattern=.*
FileName=libhev-scgi-server-cgi.so
CGIBinPath=/usr/bin/php-cgi
WorkDir=/var/web/USERNAME/home/web/www

/etc/rc.local

# HevSCGIServer
cd /opt/hev-scgi-server/
rm -f /var/web/USERNAME/run/hev-scgi-server.sock
LD_LIBRARY_PATH=/opt/hev-scgi-server/bin /opt/hev-scgi-server/bin/hev-scgi-server -u USERNAME -g GROUP
chmod 0666 /var/web/USERNAME/run/hev-scgi-server.sock

Over!

4 thoughts on “LNMP 方案之使用 HevSCGIServer CGI 模块连接 Nginx 和 PHP-CGI”

Leave a Reply

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