技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> Oracle --> 配置nginx

配置nginx

浏览:2678次  出处信息

这应该算是老掉牙的技术了,但是就是这老掉牙的技术,今天却折腾了我几个小时。首先是到网络找一些文档,因为转来转去的原因(我痛恨一切不自己产生内容,却到处抓取内容还不完整的网站),基本上只有几篇比较完整的配置文档,无一例外,都是采取源代码编译,编译php,编译mysql,编译nginx,编译memcache,编译eaccelerator,编译everthing。似乎只有这样才是王道,才是问题的解决版本。

我讨厌编译,我只需要配置。我如果跳过编译,只看配置,在我的系统上就会出现无法解析php的情况。

最好的文档还是官方文档,仔细看了看官方文档,不用编译还是能解决我的问题的。以下是我的配置过程流水帐。

系统是CentOS 5.4。默认的仓库没有nginx,php的版本还在5.0.x上。需要更新。我就整合在一个文件里(/etc/yum.repos.d/extra.rep)

 
01 [epel]
02 name=Extra Packages for Enterprise Linux 5 - $basearch
05 failovermethod=priority
06 enabled=1
07 gpgcheck=1
08 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
09   
10 [epel-debuginfo]
11 name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
14 failovermethod=priority
15 enabled=0
16 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
17 gpgcheck=1
18   
19 [epel-source]
20 name=Extra Packages for Enterprise Linux 5 - $basearch - Source
22 mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
23 failovermethod=priority
24 enabled=0
25 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
26 gpgcheck=1
27 [root@www yum.repos.d]# ls
28 CentOS-Base.repo   epel.repo          mirrors-rpmforge  rpmforge-testing.repo
29 CentOS-Media.repo  epel-testing.repo  rpmforge.repo     utterramblings.repo
30 [root@www yum.repos.d]# more utterramblings.repo
31 [utterramblings]
32 name=Jason's Utter Ramblings Repo
33 baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
34 enabled=1
35 gpgcheck=1
37 [root@www yum.repos.d]# cat rpmforge.repo utterramblings.repo epel.repo
38 ### Name: RPMforge RPM Repository for Red Hat Enterprise 5 - dag
39 ### URL: <A class=autohyperlink title=http://rpmforge.net/ href="http://rpmforge.net/" target=_blank>rpmforge.net/</A>
40 [rpmforge]
41 name = Red Hat Enterprise $releasever - <A class=autohyperlink title=http://RPMforge.net href="http://RPMforge.net" target=_blank>RPMforge.net</A> - dag
42 baseurl = <A class=autohyperlink title=http://apt.sw.be/redhat/el5/en/$basearch/rpmforge href="http://apt.sw.be/redhat/el5/en/$basearch/rpmforge" target=_blank>apt.sw.be/redhat/el5/en//rpmforge</A>
43 mirrorlist = <A class=autohyperlink title=http://apt.sw.be/redhat/el5/en/mirrors-rpmforge href="http://apt.sw.be/redhat/el5/en/mirrors-rpmforge" target=_blank>apt.sw.be/redhat/el5/en/mirrors-rpmforge</A>
45 enabled = 1
46 protect = 0
47 gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
48 gpgcheck = 1
49 [utterramblings]
50 name=Jason's Utter Ramblings Repo
51 baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
52 enabled=1
53 gpgcheck=1
55 [epel]
56 name=Extra Packages for Enterprise Linux 5 - $basearch
59 failovermethod=priority
60 enabled=1
61 gpgcheck=1
62 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
63   
64 [epel-debuginfo]
65 name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
68 failovermethod=priority
69 enabled=0
70 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
71 gpgcheck=1
72   
73 [epel-source]
74 name=Extra Packages for Enterprise Linux 5 - $basearch - Source
76 mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
77 failovermethod=priority
78 enabled=0
79 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
80 gpgcheck=1

现在使用yum开始升级你的系统,安装你需要的nginx,还有安装spawn-fcgi包吧。

为了便于软件的升级,我们尽可能不改动软件默认的配置,尽可能设置自己的配置文件。
首先在/etc/nginx/目录下创建fcgi.conf文件,内容如下:

 
1 fastcgi_pass   127.0.0.1:9000;
2 fastcgi_index  index.php;
3 include        /etc/nginx/fastcgi_params;

从虚拟主机的角度考虑,我们在/etc/nginx/conf.d/目录下创建virtual.conf文件,内容如下:

 
01 server {
02     listen       80;
03     server_name  <A class=autohyperlink title=http://www.foo.com href="http://www.foo.com" target=_blank>www.foo.com</A> <A class=autohyperlink title=http://foo.com href="http://foo.com" target=_blank>foo.com</A>
04   
05     location / {
06         root   /your/webroot/path/;
07         index  index.shtml index.html index.php;
08     }
09     location ~ \.php$ {
10         include /etc/nginx/fcgi.conf;
11     fastcgi_param   SCRIPT_FILENAME /your/webroot/path/$fastcgi_script_name;
12     }
13 }

这里要主机针对每一个虚拟主机要增加一个SCRIPT_FILENAME的参数,使用绝对路径设置脚本的位置,这里我尝试了网络上传说的$document参数,无论怎么设置,均不凑效。
如果这里有一个通用参数的话,那么就可以把这张包行到fcgi.conf文件里,这样每一个虚拟主机的配置就只要include fcgi.conf就可以了。嗯,应该有这样的参数,只是我暂时还没有找到。

然后,增加一个/etc/init.d/fastcgi-php的起停脚本,如下:

 
01 #!/bin/bash
02 #
03 # fastcgi-php   Start/stop fastcgi php processes
04 #
05 # chkconfig: 2345  99 50
06 # description: spawn php fastcgi module
07 # processname: fastcgi-php
08   
09 PROVIDES=/usr/bin/php-cgi
10 LIGHTTPD_FCGI=/usr/bin/spawn-fcgi
11 SERVER_IP=127.0.0.1
12 SERVER_PORT=9000
13 SERVER_USER=nobody
14 SERVER_GROUP=nobody
15 SERVER_SPAWN=8
16 SERVER_CHILDS=3
17 PHP_CGI=/usr/bin/php-cgi
18 PGREP=/bin/pgrep
19 KILLALL=/usr/bin/killall
20 ### No editing below ####
21 cmd=$1
22   
23 pcgi_start(){
24     echo "Starting $PROVIDES..."
25     $LIGHTTPD_FCGI -a $SERVER_IP -p $SERVER_PORT -u $SERVER_USER -g $SERVER_GROUP -C $SERVER_SPAWN -F $SERVER_CHILDS -f $PHP_CGI
26 }
27   
28 pcgi_stop(){
29     echo "Killing $PROVIDES..."
30     $KILLALL $PROVIDES
31 }
32   
33 pcgi_restart(){
34     pcgi_stop
35     sleep 2
36     pcgi_start
37 }
38   
39 pcgi_status(){
40         $PGREP $PROVIDES > /dev/null
41     [ $? -eq 0  ] && echo "$PROVIDES running" || echo "$PROVIDES NOT running" 
42   
43 }
44   
45 pcgi_help(){
46     echo "Usage: $0 {start|stop|restart|status}"
47 }
48   
49 case ${cmd} in
50 start) pcgi_start;;
51 stop) pcgi_stop;;
52 restart) pcgi_restart;;
53 status) pcgi_status ;;
54 *)      pcgi_help ;;
55 esac

根据你的需要可以调整最上面的参数,其中注意SERVER_SPAWN和SERVER_CHILDS参数,他们分别用来设置spawn进程和fork进程的个数。启动时总的进程个数等于 (SERVER_SPAWN + 1) * SERVER_CHILDS。上面的例子表示启动php-cgi进程9*3=27个。

最后做一些扫尾的工作:

 
1 #chkconfig --add fastcgi-php
2 #chkconfig fastcgi-php on
3 #chkconfig nginx on
4 #chkconfig mysqld on
5 #service mysqld start
6 #service fastcgi-php start
7 #service nginx start

好了,可以写的测试脚本了,看看html是否可以解析,php是否可以解析。

上面的配置方法,不需要修改网络上盛传的php.ini文件,fastcgi_params文件等。而且可以利用yum直接升级这些软件包,而不是每次下载源代码不停的./configure;make;make install 。那是一个痛苦的过程。

建议继续学习:

  1. 配置Nginx+uwsgi更方便地部署python应用    (阅读:105186)
  2. 搜狐闪电邮箱的 Nginx/Postfix 使用模式    (阅读:32334)
  3. 解析nginx负载均衡    (阅读:14025)
  4. Linux 下 PHP 5.2.x 连接 SQL Server 数据库 FreeTDS 配置笔记    (阅读:9971)
  5. Nginx模块开发入门    (阅读:9587)
  6. 检查nginx配置,重载配置以及重启的方法    (阅读:8785)
  7. Cacti 添加 Nginx 监控    (阅读:8708)
  8. Nginx+FastCgi+Php 的工作机制    (阅读:8620)
  9. nginx的配置文件    (阅读:8578)
  10. 奇怪的 Nginx 的 upstream timed out 引起响应 502    (阅读:7543)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1