1. 程式人生 > >ubntu下單機配置fastdfs作為開發環境(2)---nginx生產環境下再編譯+fastdfs擴充套件模組的編譯

ubntu下單機配置fastdfs作為開發環境(2)---nginx生產環境下再編譯+fastdfs擴充套件模組的編譯

前言

參考文章
FastDFS搭建單機圖片伺服器(二)

這篇文章也是參考了網上的文章,為什麼不直接說是轉載的?因為上面文章只說了一半—實際生產環境,譬如,nginx除了fastdfs,實際上可能還要編譯openssl,geoip等等擴充套件模組的。

編譯安裝過程

1、安裝gcc
安裝 nginx 需要先將官網下載的原始碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
檢視是否已經下載:
dpkg -l | grep gcc
如果沒有則下載安裝
sudo apt-get install g+±5

2、 安裝PCRE pcre-devel
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正則表示式庫。nginx 的 http 模組使用 pcre 來解析正則表示式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

sudo apt-get install openssl libssl-dev

3、安裝zlib
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
檢視是否安裝
dpkg -l | grep zlib
如果沒有則下載安裝
sudo apt-get install zlib1g-dev

4、安裝OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程式供測試或其它目的使用。
nginx 不僅支援 http 協議,還支援 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
sudo apt-get install openssl libssl-dev

下載fastdfs-nginx-module

注意
非常強烈建議,下載解壓在 /usr/local/nginx下面,所有nginx的擴充套件模組和nginx版本都放在這裡方便編譯。

mkdir -p /usr/local/nginx

例如:
在這裡插入圖片描述

1、下載

wget  https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip

2、解壓

unzip fastdfs-nginx-module-master.zip

下載Nginx

從這裡就與原文不同了,因為,我們下載的nginx不是最新版本的,而是,ubuntu本身安裝的原始碼包,下來以後再編譯過來。

注意,在ubuntu下面,我們會用apt-get nginx來直接安裝nginx的。
假如輸入
nginx -V命令可以看到:
在這裡插入圖片描述

假如你已經按照教程編譯了nginx,你會發現:
在這裡插入圖片描述

裡面就只有fastdfs一個擴充套件,跟我們apt-get安裝的nginx相差很遠。。。

所以我們產生了這樣的疑問:
請問 apt-get 方式安裝的 nginx 如何新增第三方模組?

好了,答案就是:

在這裡插入圖片描述

首先,我們先下載原始碼:

apt-get source nginx

在這裡插入圖片描述

然後,嗯。。。可以看到的是版本1.140的,離最新版本1.15有距離。。。不過沒關係。
然後,下載fastdfs的擴充套件模組:

wget -c https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip

在這裡插入圖片描述

重新命名,然後解壓:

mv master.zip fastdfs-nginx-module.zip
unzip fastdfs-nginx-module.zip

在這裡插入圖片描述

注意,在source nginx時候已經將nginx 1.14的原始碼下載了,就是這裡:
在這裡插入圖片描述

檢視一下編譯的時候的引數:
在這裡插入圖片描述

即引數如下:

--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/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

所以我們可以得到編譯ubuntu下面的nginx的正式命令:

ls nginx-1.14.0/
./configure --add-module=../fastdfs-nginx-module-master/src/  --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/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

如下:
在這裡插入圖片描述

最後結果:
在這裡插入圖片描述

嗯。。配置是吧,要模組:
參考一下
Linux Nginx安裝以及可能出現錯誤

apt-get install libxml2 libxml2-dev 
apt-get install libxslt-dev

在這裡插入圖片描述

在這裡插入圖片描述

好,繼續配置:
在這裡插入圖片描述

抽絲剝繭,可以查到:
GD Graphics (Draw) Library

在這裡插入圖片描述

所以,現在的問題在於,如何安裝libgd。

在release裡面可以看到:
在這裡插入圖片描述

難道還要手動編譯?
不過,ubuntu裡面有這軟體的:
在這裡插入圖片描述

直接安裝:
在這裡插入圖片描述

繼續:
在這裡插入圖片描述

the GeoIP module requires the GeoIP library

geoip 模組。。。
嗯,
ubuntu安裝GeoIP問題


sudo apt-get install geoip-bin geoip-database

在這裡插入圖片描述
在這裡插入圖片描述

繼續下一個:
還是提示:


./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

檢視一下ubuntu的倉庫:
在這裡插入圖片描述

好了,估計要安裝:

libnginx-mod-http-geoip

依然無效。。
偶然查到:
./configure: error: the GeoIP module requires the GeoIP library.

在這裡插入圖片描述

apt-get install libgeoip-dev

在這裡插入圖片描述

繼續

在這裡插入圖片描述

終於可以了,內牛滿面。。。
好了,安裝:

make

然後,
在這裡插入圖片描述

老問題了。改改:

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

注意,這個問題的解決過程如下,有興趣可以看看,也可以直接跳過:

注意,很多時候意外都會出現,尤其你用的都是最新版本nginx時候,在1.15.7 nginx版本上面編譯 nginx - fastdfs - module就出問題了:
在這裡插入圖片描述

然後檢查之:
FastDFS安裝Nginx的模組,fastdfs-nginx-module報錯:fdfs_define.h:15:27: 致命錯誤:common_define.h:沒有那個檔案或目錄

這兄dei也遇到了,解決方案:

並且很幸運的找到了解決方法:
編輯 fastdfs-nginx-module-1.20/src/config 檔案


ngx_addon_name=ngx_http_fastdfs_module

if test -n "${ngx_module_link}"; then
ngx_module_type=HTTP
ngx_module_name=$ngx_addon_name
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
ngx_module_libs="-lfastcommon -lfdfsclient"
ngx_module_srcs="$ngx_addon_dir/ngx_http_fastdfs_module.c"
ngx_module_deps=
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='2561024' -DFDFS_MOD_CONF_FILENAME='"/etc/fdfs/mod_fastdfs.conf"'"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -lfastcommon -lfdfsclient"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='2561024' -DFDFS_MOD_CONF_FILENAME='"/etc/fdfs/mod_fastdfs.conf"'"
fi
改成以上內容貌似就可以通過
改變的檔案內容

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

注意!!!千萬不要將config的檔案完全抄下去,只要將

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

這兩句改了即可,例如:
我的fastdfs config檔案是這樣的:

修改之前:

在這裡插入圖片描述

修改之後:

在這裡插入圖片描述

因為下載來的模組config可能已經變了,不能照抄的!

編譯過後,會這樣:

在這裡插入圖片描述

另外提醒:
即使想要降版本,可能也會有麻煩,因為gcc的版本過新,而nginx1.10的這個版本舊,會報錯的。

修改以後再config再make:

在這裡插入圖片描述

make成功,然後;

make install

在這裡插入圖片描述

在這裡插入圖片描述

好了,檢視版本號:

/usr/local/nginx/sbin/nginx -V

在這裡插入圖片描述

?!不對,明明編譯的是1.1.40版本的,而且添加了很多外掛,為什麼還是這個?
然後查一下所有nginx相關目錄:
在這裡插入圖片描述

最後發現在:

/usr/share/nginx 下面才是我們編譯的
/usr/share/nginx/sbin/nginx -V

在這裡插入圖片描述

對比一下目前版本的nginx:
在這裡插入圖片描述

確實是我們編譯的。

好了,下一步我們要替換掉原本apt-get安裝的nginx。

請先關停 nginx:

systemctl stop nginx
rm /usr/sbin/nginx
cp /usr/share/nginx/sbin/nginx /usr/sbin/nginx

在這裡插入圖片描述

替換成功。

總結

這一篇文章其實說的是,怎麼全新編譯一個可以用在生產環境的nginx並且這個nginx已經將fastdfs擴充套件模組編譯進來了。基本上挺曲折的一條路。