Linux利用nginx-gridfs搭建部署Nginx和MongoDB文件服务器,支持用户密码验证!

nginx-gridfs是一个nginx的扩展模块,用于支持直接访问MongoDB的GridFS文件系统上的文件并提供 HTTP 访问

1,安装nginx,下载好安装包nginx和nginx-gridfs,此次安装采用nginx1.12.2.

mkdir -p /data/soft/nginx-mongodb/   # 创建文件夹存储安装包和图片,把文件上传到这里
cd /data/soft/nginx-mongodb/


yum update
yum -y wget git
# 安装依赖包

wget http://nginx.org/download/nginx-1.14.2.tar.gz
# 下载nginx1.14.2

# 下载 nginx-gridfs,链接:https://pan.baidu.com/s/1cup-KtIYtaAEqCaZp2dTbA ,提取码:23hf 

2,安装Nginx依赖包并编译安装

yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++
cd /data/soft/nginx-mongodb/
tar xzvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx   --with-openssl=/usr/include/openssl --add-module=/data/soft/nginx-mongodb/nginx-gridfs
vi /data/soft/nginx-mongodb/nginx-1.14.2/objs/Makefile
# Makefile文件内容去掉"-Werror"
make -j8 && make install -j8

注:如遇报错去掉nginx-1.14.2/objs/Makefile里的-Werror,再次编译即可

3.配置nginx.conf,注:配置中的IP改成你自己的

cat /proc/cpuinfo     # 查看CPU核数,根据核数配置Nginx的worker_processes数量
rm -rf /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf
#/usr/local/nginx/conf/nginx.conf添加下面内容,注意!将IP换成实际的访问IP
###############################################################################
user  root;
worker_processes  4;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		}
    server {
        listen       800;
        server_name  192.168.240.113;
        location /file/ {
            gridfs FILEDB
            root_collection=fs
            field=filename
            type=string
            user=foo
            pass=bar;
                mongo 192.168.240.113:20000;
                access_log  logs/gridfs.access.log;
                error_log   logs/gridfs.error.log;
       }
}
##########################################################################

4,    安装mongodb

cat /etc/redhat-release  # 查看系统版本信息,下载相对应的安装包 ,请一定一定耀下载2.6.9版本。。。不支持其他版本!
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-2.6.9.tgz

下载好安装包,此次安装版本mongodb-linux-x86_64-rhel70-2.6.9.tgz,请一定一定耀下载2.6.9版本。。。不支持其他版本!

#解压:
cd /data/soft/nginx-mongodb
tar xzvf mongodb-linux-x86_64-rhel70-2.6.9.tgz -C /usr/local/
#创建所需目录并授权:
cd /usr/local
mv mongodb-linux-x86_64-rhel70-2.6.9 mongodb_file
cd mongodb_file
mkdir data 
mkdir log
mkdir etc
chown -R 777 data log etc
touch /usr/local/mongodb_file/log/mongo.log
chown -R 777 /usr/local/mongodb_file/log/mongo.log
#添加配置文件:
cd etc 
vi mongodb.conf
################################################################################
dbpath=/usr/local/mongodb_file/data
logpath=/usr/local/mongodb_file/log/mongo.log
logappend=true
journal=true
quiet=true
fork=true
port=20000
auth=true
bind_ip = 0.0.0.0
################################################################################


5.    启动mongodb

/usr/local/mongodb_file/bin/mongod -f /usr/local/mongodb_file/etc/mongodb.conf
/usr/local/mongodb_file/bin/mongo --port=20000
use admin
db.createUser({user: "foo",pwd: "bar",roles: [ { role: "userAdminAnyDatabase", db: "admin" }]})
db.auth('foo','bar')
use FILEDB
db.createUser({user: "foo",pwd: "bar",roles: [ { role: "readWrite", db: "FILEDB" }]})
exit

至此,环境搭建完成。
测试nginx请求mongodb,注:配置中的IP改成你自己的

mongodb上传图片:

/usr/local/mongodb_file/bin/mongofiles put --host 192.168.240.113 -u foo -p bar --port 20000 --db FILEDB --local /data/soft/nginx-mongodb/2.jpg --type jpg
# 注意文件名称千万不能有减号!!!

 

检查是否成功存储2.jpg:注:配置中的IP改成你自己的

/usr/local/mongodb_file/bin/mongofiles get --host 192.168.240.113 -u foo -p bar --port 20000 --db FILEDB --local /data/soft/nginx-mongodb/2.jpg --type jpg

 

 

6,启动nginx
检查配置:

/usr/local/nginx/sbin/nginx -t

启动Nginx:

/usr/local/nginx/sbin/nginx

检查进程跟端口,一定要确保worker_processes的子进程要启动成功!!

ps -ef|grep nginx 


nginx访问,注意!将IP换成实际的访问IP:注:配置中的IP改成你自己的
http://192.168.240.113:800/file/1.jpg (file为nginx配置)

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页