1、前期准备
sudo yum -y install pcre-devel openssl-devel GeoIP-devel zlib-devel
2、下载nginx-gridfs源码,并将mongodb-mongo-c-driver源码添加到nginx-gridfs/mongo-c-driver中
# 下载地址会因为代码版本号的变更不能使用,博友们自己搜一下吧。。
wget https://download.github.com/mdirolf-nginx-gridfs-v0.8-0-gb5f8113.tar.gz
tar –zxvf mdirolf-nginx-gridfs-v0.8-0-gb5f8113.tar.gz
mv mdirolf-nginx-gridfs-v0.8-0-gb5f8113 mdirolf-nginx-gridfs-v0.8
wget https://download.github.com/mongodb-mongo-c-driver-v0.3-0-g74cc0b8.tar.gz
tar –zxvf mongodb-mongo-c-driver-v0.3-0-g74cc0b8.tar.gz
mv mongodb-mongo-c-driver-v0.3-0-g74cc0b8/* mdirolf-nginx-gridfs-v0.8/mongo-c-driver
rm –rf mongodb-mongo-c-driver-v0.3-0-g74cc0b8
3、安装nginx,指定nginx-gridfs目录与nginx联合编译
wget http://nginx.org/download/nginx-1.0.1.tar.gz
tar –zxvf nginx-1.0.1.tar.gz
cd nginx-1.0.1
./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl --with-http_stub_status_module --add-module=/home/cdh/Downloads/mdirolf-nginx-gridfs
make –j8
sudo make install –j8
4、安装mongodb并启动
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz
tar –xvf mongodb-linux-i686-1.8.1.tgz
sudo mv mongodb-linux-i686-1.8.1 /usr/local/mongodb
sudo mkdir –p /tmp/mongodb/data
/usr/local/mongodb/bin/mongod --dbpath /tmp/mongodb/data --logpath /tmp/mongodb/data/log.log –fork
5、配置nginx-gridfs
sudo vim /usr/local/nginx/conf/nginx.conf
location /pics/ {
gridfs pics
field=filename
type=string;
mongo 127.0.0.1:27017;
}
gridfs:nginx识别插件的关键字
pics:db名
[root_collection]: 选择collection,如root_collection=blog, mongod就会去找blog.files与blog.chunks两个块,默认是fs
[field]:查询字段,保证mongdb里有这个字段名,支持_id, filename, 可省略, 默认是_id
[type]:解释field的数据类型,支持objectid, int, string, 可省略, 默认是int
[user]:用户名, 可省略
[pass]:密码, 可省略
mongo:mongodb url
6、上传图片
sudo /usr/local/mongodb/bin/mongofiles put --host localhost --port 27017 --db pics --local ~/photo.jpg --type jpg
7、启动nginx
sudo /usr/local/nginx/sbin/nginx
在浏览器里输入http://localhost/pics/photo.jpg 能下载图片就说明成功了