若沒安裝FastDFS,請先實現FastDFS安裝(FastDFS+nginx+fastdfs-nginx-module(1))
環境及版本:
- Ubuntu18.04
- FastDFS V6.06(應該 >=6.03)
- nginx(1.14.0)
- fastdfs-nginx-module V1.22(已通過nginx 1.16.1 測試)
1. 安裝nginx且下載nginx官網源碼
1.1 未安裝nginx
請按照下面步驟安裝nginx
1.1.1 安裝nginx依賴
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get install zlib1g-dev
sudo apt-get install libssl-dev
build-essential
安裝了一組新的包,包括gcc
,g++
,make
.- GNU
libtool
是通用庫支持腳本.它將共享庫的使用隱藏在一個一致的可移植的接口後面. pcre
是一個Perl庫,包括perl兼容的正則表達式庫.nginx的http模塊
使用pcre來解析正則表達式
.zlib
庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip.openssl
是一個強大的安全套接字層密碼庫,囊括主要的密碼算法,常用的密鑰和證書封裝管理功能及SSL
協議,並提供豐富的應用程序供測試或其他目的使用.nginx不僅支持http協議,還支持https
(即在SSL協議上傳輸http)
1.1.2 安裝nginx
sudo apt-get install nginx
1.2 已安裝nginx
如果你之前的系統已經安裝了nginx則下載與之前安裝的版本一致的源碼.查看nginx版本信息:
nginx -V
我的已安裝nginx版本信息如下:
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
1.3 下載nginx官網源碼
1.3.2 下載nginx源碼
從上面版本信息可以看到我安裝的nginx是1.14.0版本的,所以我儘量下載一樣的版本:
sudo apt-get install wget
sudo wget http://nginx.org/download/nginx-1.14.0.tar.gz
1.3.2 解壓nginx源碼包
sudo tar -zxvf nginx-1.14.0.tar.gz
2.fastdfs-nginx-module安裝
我們現在要做的是將fastdfs模塊添加到nginx,然後再重新編譯一個新的nginx,
這樣新的nginx也就包含了fastdfs-nginx-module這個模塊.
github:fastdfs-nginx-module
2.1 克隆fastdfs-nginx-module:
sudo apt-get install git
sudo git clone https://github.com/happyfish100/fastdfs-nginx-module.git
2.2 給nginx添加fastdfs模塊
2.2.2 進入nginx源碼目錄
cd nginx-1.14.0
2.2.3 生成Makefile
複製步驟 1.2 中configure arguments:
後面的參數放到,添加在./configure
後面,
同時額外添加 --add-module=/root/fastdfs-nginx-module/src
注意: 把上面的/root/
替換爲你存放fastdfs-nginx-module
的路徑.
./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=/root/fastdfs-nginx-module/src
生產Makefile如果缺少庫就安裝缺少的庫,比如我就遇到,錯誤1:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
執行:
sudo apt-get install libxslt-dev
遇到錯誤2:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
執行:
sudo apt-get install libgd-dev
遇到錯誤3:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
執行:
sudo apt-get install libgeoip-dev
2.2.4 編譯且替換nginx可執行文件
2.2.4.1 編譯nginx
注意:不要make install
,否則就是覆蓋安裝.
make
make完之後在objs目錄下就多了個nginx,這個就是新版本的程序了.
2.2.4.2 查找nginx可執行程序
find / -name "nginx" -type f -executable -print
找到三個nginx可執行
/etc/init.d/nginx
/root/nginx-1.14.0/objs/nginx
/usr/sbin/nginx
/root/nginx-1.14.0/objs/nginx
是剛新編譯生成的,用它來替換其他兩個舊的nginx.
2.2.4.3 替換調原來的可執行nginx.
nginx -s quit
cp /etc/init.d/nginx /etc/init.d/nginx.bak
cp /usr/sbin/nginx /usr/sbin/nginx.bak
cp /root/nginx-1.14.0/objs/nginx /etc/init.d/nginx
cp /root/nginx-1.14.0/objs/nginx /usr/sbin/nginx
2.2.5 測試新的nginx程序是否正確
nginx -t
nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx:configuration file /usr/local/nginx/conf/nginx.conf test issuccessful
2.2.6 重啓nginx
systemctl restart nginx
2.2.7 查看ngixn版本及其編譯參數
nginx -V
nginx version: nginx/1.14.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=/root/fastdfs-nginx-module/src
configure arguments
與步驟 2.2.3 中的./configure
一致就OK.
2.3 配置 nginx的配置文件,比如 nginx.conf
vi /etc/nginx/nginx.conf
裏面有
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
我在 /etc/nginx/sites-enabled/
目錄裏面有建立一個example.com
,
example.com可以是其他名稱(比如:你買的域名).
所以我在 /etc/nginx/sites-enabled/example.com
添加下面這段:
location /M00 {
root /home/yuqing/fastdfs/data;
ngx_fastdfs_module;
}
2.4 建立一個軟鏈接 ${fastdfs_base_path}/data/M00
到 ${fastdfs_base_path}/data
請根據自己文件路徑進行相應修改:
ln -s /home/yuqing/fastdfs/data /home/yuqing/fastdfs/data/M00
2.5 複製源碼fastdfs中的conf/http.conf
和conf/mime.types
到 /etc/fdfs
把 $YOUR_PATH
替換爲你自己存放 fastdfs
的路徑
假設安裝fastdfs時使用的是默認路徑,如下:
cd $YOUR_PATH/fastdfs
cp conf/http.conf conf/mime.types /etc/fdfs/
2.6 複製 mod_fastdfs.conf
到 /etc/fdfs/
且修改它
把 $YOUR_PATH
替換爲你自己存放 fastdfs-nginx-module
的路徑
cp $YOUR_PATH/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
修改/etc/fdfs/mod_fastdfs.conf
vi /etc/fdfs/mod_fastdfs.conf
tracker_server=tracker:22122
改爲自己的IPstore_path0=/home/yuqing/fastdfs
跟 storaged 配置要一致.
2.7 重啓nginx
nginx -s quit
nginx -t
systemctl restart nginx
2.8 檢測HTTP訪問(瀏覽訪問存儲的數據)
客戶端上傳數據
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/include/stdlib.h
假設返回鏈接如下:
http://192.168.1.100/group1/M00/00/00/aIBdvl5Th7iAM1PwAACLyBo1AoQ37510.h
瀏覽器輸入下面的URL就可以訪問剛上傳的 stdlib.h
數據.(到這步便完成)
http://192.168.1.100/M00/00/00/aIBdvl5Th7iAM1PwAACLyBo1AoQ37510.h
擴展:
我已經配置總是使用https且是配置的自己域名,假設域名爲example.com
,
所以我在瀏覽器輸入的URL鏈接如下:
https://example.com/M00/00/00/aIBdvl5Th7iAM1PwAACLyBo1AoQ37510.h
參考:
happyfish100/fastdfs-nginx-module/INSTALL
歡迎打賞0.095元