Recently in the deployment of the company server Nginx + MongoDB + GRIDFS environment;
Search n Many documents, basically all the same, during the period encountered a lot of problems;
Here is a list of the building documents:
Abstract Nginx-gridfs is an nginx extension module that supports direct access to files on MongoDB's Gridfs file system and provides HTTP access.
First, Installing MongoDB
MongoDB under linux is no need to install, download from the official website after the installation package decompression, directly execute mongod, you can start MongoDB Server, of course Mongod also has a lot of startup options, run mongod--help can see all the options.
Note: Many people build up, found that access to pictures and files, and MongoDB configuration has a lot of relations; attached my successful configuration of MongoDB, direct decompression available; Click to download MongoDB
For example: (Download my package above, unzip, the following command to start the service directly, pro-Test available)
./mongod-port 10001--dbpath.. /data/--logpath. /log/mongodb.log--fork
Stop MongoDB
Be sure to emphasize that you should never use kill-9 to close mongod! So the database will ignore everything directly kill the process, will make the data file corruption.
The safe approach is to use the kill-2 pid to close mongod, which is when mongod After the process accepts the shutdown instruction, it waits for the current run operation or file allocation to finish, closes all open connections, and flushes the cached data to disk before it is formally closed.
The surest way is to use the shutdown command to end
> Use admin
Switched to DB admin
> Db.shutdownserver ();
Second, installation of Nginx and Nginx-gridfs
dependent libraries, tools
1 2 |
# yum-y Install Pcre-devel openssl-devel zlib-devel # yum-y Install gcc gcc-c++ |
If the automatic installation is unsuccessful, it can be installed manually, as in several dependent libraries;
P CRE Download https://sourceforge.net/projects/pcre/files/pcre/
Download Nginx-gridfs Source code
1 2 3 4 5 |
# git clone https://github.com/mdirolf/nginx-gridfs.git # CD Nginx-gridfs # git checkout v0.8 # git submodule init # git submodule update |
the above method can be used locally git down to the server, or directly on the server git
Download Nginx source code, compile and install.
1 2 3 4 5 |
# wget http://nginx.org/download/nginx-1.4.7.tar.gz # tar ZXVF nginx-1.4.7.tar.gz # CD nginx-1.4.7 #./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl--add-module=. /nginx-gridfs/ # make-j8 && Make Install-j8 |
Modify the/usr/local/nginx/conf/nginx.conf configuration file.
Note: Here are the instructions, Nginx has two nginx.conf configuration files, do not match the wrong;
1 2 3 4 5 6 7 |
Location / girdfstest / { Gridfs Pics field=_id Type=objectid; MONGO 127.0.0.1:10001; } |
For a brief description of the configuration here:
Gridfstest: Access Address
PICS: Database
MONGO 127.0.0.1:10001 #mongo的服务器地址及端口
--------------------------------------
Start Nginx Service
1 |
#/usr/local/nginx/sbin/nginx |
Reload/usr/local/nginx/sbin/nginx-s Reload
3. Testing
Upload a file
[Email protected]:~# ./ mongofiles put 1.jpg-db pics-t jpg
To list files in a library:
[Email protected]:~# mongofiles list-db Pics
Specific mongofiles use can Baidu search under
or directly./mongofiles--help View Help
Browser access:http://192.168.4.156/girdfstest/1.jpg
If you can access the picture, the configuration is successful;
Precautions:
Installation process may encounter a variety of errors, depending on the environment, to deal with;
Main points of attention
1. Nginx start parameter, need to associate nginx-gridfs directory
2. Mongodb try not to use the default port 27017, use a different port (installation process encountered the default port access, do not know what reason)
3. nginx.conf Configuration, parameters above document description
4. If the configuration is not successful, can not access the picture; look at nginx error log, there will be a lot of harvest
During the installation process, if you have other questions, you can look at some of the documents I wrote earlier;
Read MongoDB pictures and files with Nginx-gridfs (why are you always unsuccessful?) )