前言:
== 为什么选择 Nginx ==
Nginx 是一个很牛的高性能 Web 和反向代理服务器, 它具有有很多非常优越的特性:
在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应。
Nginx 作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP 代理服务器对外进行服务。Nginx 采用 C 进行编写,不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好很多。
作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
Nginx 是一个 安装非常的简单,配置文件 非常简洁(还能够支持 perl 语法),Bugs 非常少的服务器:Nginx 启动特别容易,并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动。还能够不间断服务的情况下进行软件版本的升级。
(一)Nginx 安装
当前官方网站发布的最新稳定版本是 0.6.31 :
http://wiki.codemongers.com/NginxNews#latest_stable
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
# ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx --http-log-path=var/log/nginx --user=apache --group=apache --with-pcre=/usr/include/pcre --with-http_stub_status_module --with-http_ssl_module
(注意:在这里,需要说明一下,由于 Nginx 的配置文件中我需要用到正则,所以需要 pcre 模块的支持。而我们在第一部分的安装中已经安装了 pcre 及 pcre-devel 的 相应 rpm 包,但是 Ngxin 并不能正确找到 .h/.so/.a/.la 文件,因此需要变通一下)
# mkdir /usr/include/pcre
# mkdir /usr/include/pcre/.libs
# cp /usr/lib/libpcre.a /usr/include/pcre/libpcre.a
# cp /usr/lib/libpcre.a /usr/include/pcre/libpcre.la
# cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
# cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
然后,修改 objs/Makefile 文件,注释掉以下内容:
./configure --disable-shared
接下来,就可以正常执行 make 及 make install 了。
# make
# make install
# mkdir /var/log/nginx
接下来,修改 Nginx 的配置文件
# cd /usr/local/nginx/conf
# vi nginx.conf (如下图所示)

运行以下命令检测配置文件是否无误:
# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
如出现下面的字符则为配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
启动 nginx:
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
检查 nginx 进程是否启动:
# ps aux|grep nginx|grep -v grep (若看到如下几个进程,就已经启动 OK,若无则要检查配置)
root 2173 0.0 0.1 5308 596 ? Ss 10:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
apache 2174 0.0 0.1 5504 932 ? S 10:41 0:00 nginx: worker process
apache 2175 0.0 0.1 5504 960 ? S 10:41 0:00 nginx: worker process
apache 2177 0.0 0.1 5504 956 ? S 10:41 0:00 nginx: worker process
打开浏览器输入地址,如出现下面的图示则为 nginx 已正常:

(二)配置 Nginx 的 PHP FastCGI
安装 lighthttp 以得到 spawn-fcgi ,并以此来控制 php-cgi 进程运行
# rpm -ivh lua-5.1.2-1.el5.i386.rpm
# rpm -ivh lighttpd-1.4.18-1.el5.i386.rpm
# rpm -ivh lighttpd-fastcgi-1.4.18-1.el5.i386.rpm
编辑 /usr/local/nginx/conf/fastcgi.conf 配置文件: (如下图所示)

修改 /usr/local/nginx/conf/nginx.conf 配置文件: (如下图所示)

启动 php-cgi 进程,监听 127.0.0.1 的 9000 端口,进程数为 5(如果服务器内存小于3GB,可以只开启25个进程),用户为 apache,用户组为 apache:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u apache -g apache -P /var/run/php-cgi.pid -f /usr/bin/php-cgi
参数含义如下:
-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 等
检查 9000 端口是否正常开启:
# netstat -tlunp

(三)测试 Nginx 的 PHP FastCGI
# vi /usr/local/nginx/html/phpinfo.php (内容如下所示:)
phpinfo();
?>
在浏览器打开 phpinfo.php 页面,如出现下面的图示则为 nginx 的 PHP 已正常:

(四)安装 PHP 扩展模块:xcache
# php -m (使用该命令列出当前系统安装的 PHP Modules)
使用下面命令查看是否在上面第一个步骤中安装了 memcache,如果没有,就手动安装。
# rpm -qa |grep memcache
php-pecl-memcache-2.2.1-1.el5.centos
# tar zxvf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config --enable-xcache
# make
# make install
(五)配置 XCache 加速 PHP
# vi /etc/php.ini (在文件最下面加入以下内容:)
[xcache-common]
zend_extension = /usr/lib/php/modules/xcache.so
[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "8e6867a5d05144cf4761d6481fc674a8"
[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
再查找 display_errors = On 并将其修改为 display_errors = Off
重新启动 nginx ,然后在浏览器中刷新 phpinfo.php 页面,出现如下:

========================= 完结 ============================
最后编辑: 疯狂老头 编辑于2008-6-20 11:39
== 为什么选择 Nginx ==
Nginx 是一个很牛的高性能 Web 和反向代理服务器, 它具有有很多非常优越的特性:
在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应。
Nginx 作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP 代理服务器对外进行服务。Nginx 采用 C 进行编写,不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好很多。
作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
Nginx 是一个 安装非常的简单,配置文件 非常简洁(还能够支持 perl 语法),Bugs 非常少的服务器:Nginx 启动特别容易,并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动。还能够不间断服务的情况下进行软件版本的升级。
(一)Nginx 安装
当前官方网站发布的最新稳定版本是 0.6.31 :
http://wiki.codemongers.com/NginxNews#latest_stable
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
# ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx --http-log-path=var/log/nginx --user=apache --group=apache --with-pcre=/usr/include/pcre --with-http_stub_status_module --with-http_ssl_module
(注意:在这里,需要说明一下,由于 Nginx 的配置文件中我需要用到正则,所以需要 pcre 模块的支持。而我们在第一部分的安装中已经安装了 pcre 及 pcre-devel 的 相应 rpm 包,但是 Ngxin 并不能正确找到 .h/.so/.a/.la 文件,因此需要变通一下)
# mkdir /usr/include/pcre
# mkdir /usr/include/pcre/.libs
# cp /usr/lib/libpcre.a /usr/include/pcre/libpcre.a
# cp /usr/lib/libpcre.a /usr/include/pcre/libpcre.la
# cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
# cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
然后,修改 objs/Makefile 文件,注释掉以下内容:
./configure --disable-shared
接下来,就可以正常执行 make 及 make install 了。
# make
# make install
# mkdir /var/log/nginx
接下来,修改 Nginx 的配置文件
# cd /usr/local/nginx/conf
# vi nginx.conf (如下图所示)

运行以下命令检测配置文件是否无误:
# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
如出现下面的字符则为配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
启动 nginx:
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
检查 nginx 进程是否启动:
# ps aux|grep nginx|grep -v grep (若看到如下几个进程,就已经启动 OK,若无则要检查配置)
root 2173 0.0 0.1 5308 596 ? Ss 10:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
apache 2174 0.0 0.1 5504 932 ? S 10:41 0:00 nginx: worker process
apache 2175 0.0 0.1 5504 960 ? S 10:41 0:00 nginx: worker process
apache 2177 0.0 0.1 5504 956 ? S 10:41 0:00 nginx: worker process
打开浏览器输入地址,如出现下面的图示则为 nginx 已正常:

(二)配置 Nginx 的 PHP FastCGI
安装 lighthttp 以得到 spawn-fcgi ,并以此来控制 php-cgi 进程运行
# rpm -ivh lua-5.1.2-1.el5.i386.rpm
# rpm -ivh lighttpd-1.4.18-1.el5.i386.rpm
# rpm -ivh lighttpd-fastcgi-1.4.18-1.el5.i386.rpm
编辑 /usr/local/nginx/conf/fastcgi.conf 配置文件: (如下图所示)

修改 /usr/local/nginx/conf/nginx.conf 配置文件: (如下图所示)

启动 php-cgi 进程,监听 127.0.0.1 的 9000 端口,进程数为 5(如果服务器内存小于3GB,可以只开启25个进程),用户为 apache,用户组为 apache:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u apache -g apache -P /var/run/php-cgi.pid -f /usr/bin/php-cgi
参数含义如下:
-f
-a
-p
-s
-C
-P
-u 和-g FastCGI 使用什么身份(-u 用户 -g 用户组)运行,Ubuntu 下可以使用www-data,其他的根据情况配置,如nobody、apache 等
检查 9000 端口是否正常开启:
# netstat -tlunp

(三)测试 Nginx 的 PHP FastCGI
# vi /usr/local/nginx/html/phpinfo.php (内容如下所示:)
phpinfo();
?>
在浏览器打开 phpinfo.php 页面,如出现下面的图示则为 nginx 的 PHP 已正常:

(四)安装 PHP 扩展模块:xcache
# php -m (使用该命令列出当前系统安装的 PHP Modules)
使用下面命令查看是否在上面第一个步骤中安装了 memcache,如果没有,就手动安装。
# rpm -qa |grep memcache
php-pecl-memcache-2.2.1-1.el5.centos
# tar zxvf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config --enable-xcache
# make
# make install
(五)配置 XCache 加速 PHP
# vi /etc/php.ini (在文件最下面加入以下内容:)
[xcache-common]
zend_extension = /usr/lib/php/modules/xcache.so
[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "8e6867a5d05144cf4761d6481fc674a8"
[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
再查找 display_errors = On 并将其修改为 display_errors = Off
重新启动 nginx ,然后在浏览器中刷新 phpinfo.php 页面,出现如下:

========================= 完结 ============================
最后编辑: 疯狂老头 编辑于2008-6-20 11:39
| 引用(0)
在 CentOS 5.1 下安装 Nginx 替换 apache (第一部分)
linux sysctl.conf 中相关重要设定的详细说明


2008-6-20
09:21
0
245


