环境说明:
CentOS 6 64bit Mini最小化安装
MongoDB 1.8.2 【安装配置方法详见:最简单实用的MongoDB安装教程:在CentOS中使用 yum 安装MongoDB及服务器端配置详解】
Nginx 1.0.5
本文将用费覆盖方式安装Nginx的扩展Nginx-Gridfs
Nginx的Nginx-Gridfs扩展地址:https://github.com/mdirolf/nginx-gridfs
可以使用wget下载,但是这个源码中海链接着其它源码,所有用git方法下载最方便,如果没有安装git的话按下面的步骤来
11 | Summary : Fast Version Control System |
14 | Description: Git is a fast, scalable, distributed revision control system with |
15 | : an unusually rich command set that provides both high-level |
16 | : operations and full access to internals. |
18 | : The git rpm installs the core tools with minimal dependencies. To |
19 | : install all git packages, including tools for integrating with |
20 | : other SCMs, install the git-all meta-package. |
Ok,git工具安装完成,下一步下载nginx-gridfs源码包
一、安装nginx-gridfs扩展
05 | //进入我的nginx1.0.5的源码目录编译安装nginx-gridfs扩展 |
07 | //编译前先查看现有的nginx的编译参数配置 |
09 | nginx: TLS SNI support disabled |
10 | nginx: configure arguments: --user=www --group=www --prefix=/usr/ local /nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 |
11 | //编译配置在原有配置基础上增加新的扩展(蓝色部分) |
Nginx的nginx-gridfs扩展模块安装完成,检查一下吧
2 | nginx: nginx version: nginx/1.0.5 |
3 | nginx: built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) |
4 | nginx: TLS SNI support enabled |
5 | nginx: configure arguments: --user=www --group=www --prefix=/usr/ local /nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/root/nginx-gridfs |
二、在Nginx中配置nginx-gridfs
配置语法说明:
gridfs DB_NAME [root_collection=ROOT] [field=QUERY_FIELD] [type=QUERY_TYPE] [user=USERNAME] [pass=PASSWORD]
- gridfs 表示告诉nginx服务器要调用gridfs模块
- root_collection= 指定Gridfs collection的前缀. 默认: fs
- field= 指定用于查询的字段 可以是 _id 和 filename. 默认: _id
- type= 指定查询的类型,这里支持 objectid, string 和int. 默认: objectid
- user= 指定数据库的用户名. 默认: NULL
- pass= 指定数据库的密码. 默认: NULL
Nginx配置文件中的具体写法:
/usr/local/nginx/conf/nginx.conf
1 | 方法4,这种是一个完整参数的配置例子 location /static/ { |
*以上方法中 换行不是必要,仅仅是为了看的清晰
我的一个完整的配置
04 | server_name static.ebook.vm6; |
13 | log_format static.ebook.vm6 '$remote_addr - $remote_user [$time_local] $request ' |
14 | '$status $body_bytes_sent $http_referer ' |
15 | '$http_user_agent $http_x_forwarded_for' ; |
16 | access_log /home/wwwlogs/static.ebook.vm6.log static.ebook.vm6; |
注:在测试配置时要记住不要将nginx的文件过期缓存时间配置开启了,最好是在配置好服务器后再做这个工作,否则很容易造成配置错误的假象。我在配置过程用就遇到了这样的问题
到这里你也许可以正常使用Nginx-Gridfs了,其实不然。
当你重新启动系统后你会发现用浏览器访问服务器上的web站点没有响应,这是因为系统启动过程中Nginx启动比MongoDB早,它初始化的时候不能正确链接MongoDB数据库。
了解关于CentOS的守护进程启动顺序相关的解释可以看看百度百科的这篇文章:http://wenku.baidu.com/view/f13befcfa1c7aa00b52acb40.html
查看启动顺序:
2 | K10saslauthd K86cgred S02lvm2-monitor S11auditd S25netfs S55sshd S90crond |
3 | K50netconsole K87restorecond S08ip6tables S12rsyslog S26udev-post S64mysql S99local |
4 | K74ntpd K89rdisc S08iptables S22messagebus S50php-fpm S80postfix |
5 | K75ntpdate K95cgconfig S10network S24avahi-daemon S55nginx S85mongod |
2 | K10saslauthd K86cgred S02lvm2-monitor S11auditd S25netfs S55sshd S90crond |
3 | K50netconsole K87restorecond S08ip6tables S12rsyslog S26udev-post S64mysql S99local |
4 | K74ntpd K89rdisc S08iptables S22messagebus S50php-fpm S80postfix |
5 | K75ntpdate K95cgconfig S10network S24avahi-daemon S55nginx S85mongod |
这些都是链接到/etc/init.d/目录里的相应守护进程启动脚本
S55nginx 意思是nginx守护进程启动顺序为55
S85mongod 意思是mongod守护进程的启动顺序是85,问题就在这mongod比nginx晚启动
修改启动顺序,将mongod的启动顺序值改为比nginx小的数
[root@vm ~]# mv /etc/rc3.d/S85mongod /etc/rc3.d/S54mongod
[root@vm ~]# mv /etc/rc5.d/S85mongod /etc/rc5.d/S54mongod
ok,现在reboot重启下看看是不是问题已经不存在了!