ubntu下单机配置fastdfs作为开发环境(2)---nginx生产环境下再编译+fastdfs扩展模块的编译

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cdnight/article/details/84984547

前言

参考文章
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、解压

扫描二维码关注公众号,回复: 4509300 查看本文章
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扩展模块编译进来了。基本上挺曲折的一条路。

猜你喜欢

转载自blog.csdn.net/cdnight/article/details/84984547