引用:
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。
phpcgi脚本参考http://hi.baidu.com/jackbillow/blog/item/c60ccc02b33caa0d4afb51f6.html
安装参考:http://blog.s135.com/read.php/314.htm呵呵,照做总没有成功。试了n多次。后来改变了一些方法。有时 间另文写出!
安装参考:http://www.howtoforge.com/lemp_nginx_mysql_php_ubuntu_debian 我一直用的是centos,为了使用NGINX。一度想换个LINUX OS发行版本。还好。用了GOOGLE终于搞定了。
针对redhat的rpm包:http://dl.activeby.net/rpms/RPMS/ (NGINX的版本是nginx-0.5.32) 因为机器能连网。我就直接用yum(NGINX的版本是nginx-0.5.33-2.el5)啦。呵呵,你也可以下载后,安装。
安装参考:http://shiningray.cn/linux-shang-pei-zhi-nginx-php5-fastcgi.html
1、安装环境CentOs 5.1
[root@nginx-freetds html]# uname -a
Linux nginx-freetds 2.6.18-53.el5 #1 SMP Mon Nov 12 02:22:48 EST 2007 i686 i686 i386 GNU/Linux
2、系统安装时定制安装mysql了。当然也可以采用yum install的方式安装。同时。安装了一些开发工具。呵呵,如果使用yum install的方式。亦可不在安装系统时安装。
开始安装:
1、启用CENTOS的额加包。如果不安装,可能会出现不能安装NGINX的问题。
[root@nginx-freetds ~]# rpm -Uvh
http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-2.noarch.rpm
Retrieving
http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-2.noarch.rpm
warning: /var/tmp/rpm-xfer.4hD87B: Header V3 DSA signature: NOKEY, key ID 217521f6
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
[root@nginx-freetds ~]#
2、安装NGINX
[root@nginx-freetds ~]#yum install -y nginx
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
epel 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
primary.xml.gz 100% |=========================| 513 kB 00:04
################################################## 1595/1595
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for nginx to pack into transaction set.
nginx-0.5.33-2.el5.i386.r 100% |=========================| 8.3 kB 00:00
---> Package nginx.i386 0:0.5.33-2.el5 set to be updated
--> Running transaction check
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
nginx i386 0.5.33-2.el5 epel 286 k
Transaction Summary
=============================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 286 k
Downloading Packages:
(1/1): nginx-0.5.33-2.el5 100% |=========================| 286 kB 00:02
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 217521f6
Importing GPG key 0x217521F6 "Fedora EPEL " from /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: nginx ######################### [1/1]
Installed: nginx.i386 0:0.5.33-2.el5
Complete!
[root@nginx-freetds ~]#
3、安装后,NGINX的配置文件位于:
[root@nginx-freetds ~]# ll /etc/nginx/
total 28
-rw-r--r-- 1 root root 2837 Nov 12 16:11 koi-utf
-rw-r--r-- 1 root root 2223 Nov 12 16:11 koi-win
-rw-r--r-- 1 root root 2991 Nov 12 16:11 mime.types
-rw-r--r-- 1 root root 2991 Nov 12 16:11 mime.types.default
-rw-r--r-- 1 root root 2753 Nov 12 16:11 nginx.conf
-rw-r--r-- 1 root root 2753 Nov 12 16:11 nginx.conf.default
-rw-r--r-- 1 root root 3610 Nov 12 16:11 win-utf
安装后,NGINX的网站目录位于:
[root@nginx-freetds ~]#
4、配置Nginx的PHP FastCGI
请将以下内容保存为fastcgi.conf文件,保存于/usr/share/nginx下,它为我们的FastCGI模块设置了基本的环境变量:
[root@nginx-freetds ~]# ll /usr/share/nginx/
total 4
drwxr-xr-x 2 root root 4096 Jan 20 19:58 html
[root@nginx-freetds ~]# vi /usr/share/nginx/fastcgi.conf
输入:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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_FILENAME $document_root$fastcgi_script_name;
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 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;
为什么会是在这个下面。这是我经过多次失败之后的总结,当然也可以改在其它的位置。只需在第10步中更改include 后的路径便可:
[root@nginx-freetds ~]# service nginx restart
Stopping nginx: [FAILED]
Starting nginx: 2008/01/20 00:05:32 [emerg] 15038#0: glob() "/usr/share/nginx/./fastcgi.conf" failed (2: No such file or directory) in /etc/nginx/nginx.conf:123
[FAILED]
5、接下来安装PHP,加上php-cli 以便在/usr/bin/ 生成php-cgi。这个很重要。还需要使用到它。
[root@nginx-freetds ~]# yum install -y php php-cli
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
base 100% |=========================| 1.1 kB 00:00
updates 100% |=========================|951 B 00:00
addons 100% |=========================|951 B 00:00
extras 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for php to pack into transaction set.
php-5.1.6-15.el5.i386.rpm 100% |=========================|18 kB 00:00
---> Package php.i386 0:5.1.6-15.el5 set to be updated
---> Downloading header for php-cli to pack into transaction set.
php-cli-5.1.6-15.el5.i386 100% |=========================|17 kB 00:00
---> Package php-cli.i386 0:5.1.6-15.el5 set to be updated
--> Running transaction check
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
php i386 5.1.6-15.el5 base 1.2 M
php-cli i386 5.1.6-15.el5 base 2.3 M
Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 3.5 M
Downloading Packages:
(1/2): php-5.1.6-15.el5.i 100% |=========================| 1.2 MB 00:04
(2/2): php-cli-5.1.6-15.e 100% |=========================| 2.3 MB 00:06
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID e8562897
Importing GPG key 0xE8562897 "CentOS-5 Key (CentOS 5 Official Signing Key) " from
http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: php-cli ######################### [1/2]
Installing: php ######################### [2/2]
Installed: php.i386 0:5.1.6-15.el5 php-cli.i386 0:5.1.6-15.el5
Complete!
[root@nginx-freetds ~]#
查看下:
[root@nginx-freetds ~]# locate php-cgi
/usr/bin/php-cgi
呵呵,已生成php-cgi啦。
show 下安装后的php组件:
[root@nginx-freetds ~]# rpm -aq |grep php
php-5.1.6-15.el5
php-pdo-5.1.6-15.el5
php-common-5.1.6-15.el5
php-mysql-5.1.6-15.el5
php-cli-5.1.6-15.el5
6、接下来安装lighthttp以得到spawn-fcgi以此来控制php-cgi进程运行
[root@nginx-freetds ~]# yum install -y lighttpd-fastcgi
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
epel 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for lighttpd-fastcgi to pack into transaction set.
lighttpd-fastcgi-1.4.18-1 100% |=========================| 9.6 kB 00:00
---> Package lighttpd-fastcgi.i386 0:1.4.18-1.el5 set to be updated
--> Running transaction check
--> Processing Dependency: lighttpd = 1.4.18 for package: lighttpd-fastcgi
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for lighttpd to pack into transaction set.
lighttpd-1.4.18-1.el5.i38 100% |=========================|21 kB 00:00
---> Package lighttpd.i386 0:1.4.18-1.el5 set to be updated
--> Running transaction check
--> Processing Dependency: liblua-5.1.so for package: lighttpd
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for lua to pack into transaction set.
lua-5.1.2-1.el5.i386.rpm100% |=========================| 6.8 kB 00:00
---> Package lua.i386 0:5.1.2-1.el5 set to be updated
--> Running transaction check
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
lighttpd-fastcgi i386 1.4.18-1.el5 epel 45 k
Installing for dependencies:
lighttpd i386 1.4.18-1.el5 epel 373 k
lua i386 5.1.2-1.el5 epel 225 k
Transaction Summary
=============================================================================
Install 3 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 643 k
Downloading Packages:
(1/3): lighttpd-1.4.18-1. 100% |=========================| 373 kB 00:01
(2/3): lighttpd-fastcgi-1 100% |=========================|45 kB 00:00
(3/3): lua-5.1.2-1.el5.i3 100% |=========================| 225 kB 00:01
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: lua ######################### [1/3]
Installing: lighttpd ######################### [2/3]
Installing: lighttpd-fastcgi ######################### [3/3]
Installed: lighttpd-fastcgi.i386 0:1.4.18-1.el5
Dependency Installed: lighttpd.i386 0:1.4.18-1.el5 lua.i386 0:5.1.2-1.el5
Complete!
[root@nginx-freetds ~]# updatedb
[root@nginx-freetds ~]# locate spawn-fcgi
/usr/bin/spawn-fcgi
/usr/share/man/man1/spawn-fcgi.1.gz
[root@nginx-freetds ~]#
OK,spawn-fcgi被安装成命令行式了。成功
以下兵分两路来说明:一是直接利用php-cgi的FastCGI运行方式;二是利用Lighttpd的spawn-fcgi来控制进程的运行方法。
先说说利用php-cgi的FASTCGI运行方式:
7、创建php-cgi启动脚本,
[root@nginx-freetds ~]# vi /etc/init.d/phpcgi
#!/bin/sh
#
# php-cgi - this script starts and stops the php-cgi daemin
#
# chkconfig: - 85 15
# description: Fast CGI php
# processname: php-cgi
# config: /etc/php.ini
# pidfile: /var/run/php-cgi.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
phpcgi="/usr/bin/php-cgi"
prog=$(basename ${phpcgi})
FCGIPORT="8888"
FCGIADDR="127.0.0.1"
FCGIUSER="apache"
FCGIGROUP="apache"
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
[ -e /etc/sysconfig/php-cgi ] && . /etc/sysconfig/php-cgi
lockfile=/var/lock/subsys/php-cgi
start() {
echo -n $"Starting $prog: "
/usr/bin/spawn-fcgi -a $FCGIADDR-p $FCGIPORT -C $PHP_FCGI_CHILDREN -u $FCGIUSER -g $FCGIGROUP -P /var/run/php-cgi.pid -f "${phpcgi}" >> /
dev/null 2>&1
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() {
stop
start
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart)
$1
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
exit 2
esac
然后,开机自动运行:
[root@nginx-freetds ~]#/sbin/chmod +x /etc/init.d/phpcgi
[root@nginx-freetds ~]#/sbin/chkconfig --add phpcgi
[root@nginx-freetds ~]#/sbin/chkconfig --level 35 phpcgi on
[root@nginx-freetds ~]#/sbin/chkconfig --level 35 nginx on
但从网上说会遇到两个问题,这里摘录一位的解决方案。(我没有遇到。也没有机会测试下面的解决方式是否正确)
[root@nginx-freetds ~]# cat /var/log/audit/audit.log| audit2allow -M local
[root@nginx-freetds ~]#/usr/sbin/semodule -i local.pp
下面说说利用Lighttpd的spawn-fcgi来控制进程的运行的方法:
8、开启nginx及利用Lighttpd的spawn-fcgi来控制进程的运行
[root@nginx-freetds ~]# spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f /usr/bin/php-cgi
spawn-fcgi.c.187: child spawned successfully: PID: 2513
参数含义如下
-f指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
-a绑定到地址addr
-p绑定到端口port
-s绑定到unix socket的路径path
-C指定产生的FastCGI的进程数,默认为5(仅用于PHP)
-P指定产生的进程的PID文件路径
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等
因为,安装rpm 安装nginx时。会创建nginx用户和组。
[root@nginx-freetds ~]# service nginx start
Starting nginx: [OK]
9、在IE栏里输入
http://124.207.102.22/index.htm这时NGINX已在正常运行。如下图:
在/usr/share/nginx/html下面新建index.php
在IE栏里输入
http://124.207.102.22/index.php这时NGINX已在正常运行。如下图:
10、那如何实现php的运行呢。在第7或第8步骤中,已开启了PHP的进程:
[root@nginx-freetds ~]# ps -aux |grep php
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
nginx 25130.01.9177204964 ? Ss 20:45 0:00 /usr/bin/php-cgi
nginx 25140.00.6177201656 ? S 20:45 0:00 /usr/bin/php-cgi
nginx 25150.00.6177201656 ? S 20:45 0:00 /usr/bin/php-cgi
nginx 25160.00.6177201656 ? S 20:45 0:00 /usr/bin/php-cgi
nginx 25170.00.6177201656 ? S 20:45 0:00 /usr/bin/php-cgi
nginx 25180.00.6177201656 ? S 20:45 0:00 /usr/bin/php-cgi
root 25420.00.2 3892 676 pts/0 R+ 20:48 0:00 grep php
可以看到,有五个进程正在运行。
默认情况下。NGINX是可以开启静态页面,但如何开启PHP。还是要在/etc/nginx/nginx.php设置的。
各位可以参考我的配置前后的截图(呵呵,研究下,有些参数是可以改变的。要举一返三吧):
修改前:
保存更改。
然后service nginx restart便可了。
11、配置虚拟主机
在APACHE上配置虚拟主机。想来各位都有一定的体验。那如何在NGINX中实现呢?
[root@nginx-freetds html]# vi /etc/nginx/nginx.conf
参考下图(开启https的样例也在内)。最后几行:
server
{
listen 8000; ####监听端口
server_name124.207.102.22aliasanother.alias;####域名
root /usr/share/nginx/html; ####路径
indexindex.php index.html index.htm; ####index
location ~ \.php$
{
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
}
想多加虚机吗。呵呵,多来几个吧(日志选项请自位参考CONF文件自行研究)。
基本上完成了。有些功能还需要参考官方文档深入研究学习下
接下来,研究下rpm安装的情况下实现php连ms sql server.(tar包的已成功且在用啦)