PHP可以直接读取MongoDB GridFS中的图片并显示到页面中,但对PHP的压力就大了。偶然机会,了解到Nginx可以代理访问,实现过程如下:
1、工具准备
安装一些必要的编译工具及库,这里是直接从“编译安装LNMP”系列教材中摘取的,有点冗余。
yum -y install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch gettext glib2-devel bzip2-devel libuuid-devel docbook-style-xsl libxslt-devel gzip-devel
2、源码准备
(1)Nginx基础软件
Nginx1.4.4
http://nginx.org/download/nginx-1.4.4.tar.gz
pcre(支持Nginx地址重写)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
gzip(页面压缩)
http://zlib.net/zlib-1.2.8.tar.gz
openssl(https安全支持组件)
http://www.openssl.org/source/openssl-1.0.1e.tar.gz
(2)nginx-gridfs插件、MongoDB访问驱动 mongodb-mongo-c-driver
https://github.com/mdirolf/nginx-gridfs/archive/v0.8.tar.gz
https://github.com/mongodb/mongo-c-driver/archive/v0.3.1.tar.gz
3、编译安装Shell脚本
install.sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #需要切换到root用户 if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit 1 fi ROOT_PATH=/usr/local/src/2 mkdir $ROOT_PATH rm -rf $ROOT_PATH/* ################################################## # 必要的工具 ################################################## yum -y install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch gettext glib2-devel bzip2-devel libuuid-devel docbook-style-xsl libxslt-devel gzip-devel ################################################## # 安装Nginx ################################################## #安装pcre库 cd $ROOT_PATH wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz -O pcre-8.33.tar.gz tar -xzvf ./pcre-8.33.tar.gz cd ./pcre-8.33 ./configure make && make install #安装zlib库 cd $ROOT_PATH wget -c http://zlib.net/zlib-1.2.8.tar.gz -O zlib-1.2.8.tar.gz tar -xzvf ./zlib-1.2.8.tar.gz cd ./zlib-1.2.8 ./configure make && make install #解压ssl cd cd $ROOT_PATH wget -c http://www.openssl.org/source/openssl-1.0.1e.tar.gz -O openssl-1.0.1e.tar.gz tar -xzvf ./openssl-1.0.1e.tar.gz #解压nginx-gridfs cd cd $ROOT_PATH wget -c https://github.com/mdirolf/nginx-gridfs/archive/v0.8.tar.gz -O nginx-gridfs-0.8.tar.gz tar -xzvf ./nginx-gridfs-0.8.tar.gz #解压mongo-c-driver cd $ROOT_PATH wget -c https://github.com/mongodb/mongo-c-driver/archive/v0.3.1.tar.gz -O mongo-c-driver-0.3.1.tar.gz tar -xzvf ./mongo-c-driver-0.3.1.tar.gz mv ./mongo-c-driver-0.3.1/* ./nginx-gridfs-0.8/mongo-c-driver #安装nginx cd $ROOT_PATH wget -c http://nginx.org/download/nginx-1.4.4.tar.gz -O nginx-1.4.4.tar.gz tar -xzvf ./nginx-1.4.4.tar.gz cd ./nginx-1.4.4 mkdir /usr/local/nginx ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=$ROOT_PATH/pcre-8.33 --with-zlib=$ROOT_PATH/zlib-1.2.8 --with-openssl=$ROOT_PATH/openssl-1.0.1e --add-module=$ROOT_PATH/nginx-gridfs-0.8 make && make install #添加nginx用户及用户组 groupadd www #添加nginx用户组及用户 useradd -g www -s /usr/sbin/nologin www #不让nginx用户直接登录
4、配置nginx-gridfs
vim /usr/local/nginx/conf/nginx.conf
在 server 节点中添加 location 节点
location /pics/ { gridfs edusns root_collection=sns_attach field=filename type=string; user=eduadmin pass=eduadmin$123; mongo 172.8.8.153:27017; }
注意,如果不指定 field,默认为 MongoDB 的自增ID,且type为int
参数介绍:
gridfs:nginx识别插件的关键字
edusns: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
5、给MongoDB上传图片
/usr/local/mongodb/bin/mongofiles put --host 172.8.8.153 --port 27017 --db edusns --local ~/photo.jpg --type jpg
在浏览器里输入http://localhost/pics/photo.jpg 能下载图片就说明成功了!
6、启动Nginx
/usr/local/nginx/nginx
存在的问题:浏览器端不能缓存,每次请求都要从服务器获取