文章转自 庇护祝福的分享
还是老老实实下源码编译安装,已安装先备份nginx.conf相关文件,再卸载brew uninstall nginx
由于我加的是--with-http_image_filter_module模块需要用到gd库
#Mac
brew install gd
#CentOS
#缺少gd
yum -y install gd-devel
#缺少c++编译器
yum -y install gcc-c++ autoconf automake
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz && tar xzvf pcre-8.42.tar.gz
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
#更新nginx依赖库
sudo yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel
configure
#Mac
./configure --prefix=/usr/local/Cellar/nginx/1.17.0 --sbin-path=/usr/local/Cellar/nginx/1.17.0/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_image_filter_module
#CentOS
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--builddir=nginx-1.15.7 \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-perl_modules_path=/usr/lib64/perl5 \
--with-perl=/usr/bin/perl \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre=../pcre-8.42 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1g \
--with-openssl-opt=no-nextprotoneg \
--with-debug
--prefix 用于指定nginx编译后的安装目录
--sbin-path nginx执行程序所在的目录
--add-module 为添加的第三方模块
--with..._module 表示启用的nginx模块
make
特别注意:在已安装的nginx上进行添加模块的话执行到这里就行了,把objs中的nginx替换掉之前的安装的nginx/sbin/中的nginx文件,然后重启nginx就行了,如果执行下一步的install,会导致之前安装的nginx被覆盖,比如之前配置好的nginx.conf文件)
make install
由于centos 下动态加载模块--with-http_image_filter_module=dynamic
所以需要在nginx.conf的顶部load_module
#先执行软连接,方便加载module
ln -s /usr/lib64/nginx/modules /etc/nginx/modules
#在config里添加
load_module modules/ngx_http_image_filter_module.so;
mac 配置Path,当前用户下生效
vim ~/.bash_profile
#在PATH增加
/usr/local/Cellar/nginx/1.17.0/bin
#令设置生效
source ~/.bash_profile
#重启终端后
nginx -V
#就可以看到你所加的模块
#配置
location ~ /room-thumbnail/(.*)_(\d+)_(\d+)\.(jpeg|jpg|png|gif|webp) {
alias /Users/[who]/application/upload/room-thumbnail/$1.$4;
set $img_w $2;
set $img_h $3;
if ($img_w = 0) {
set $img_w -;
}
if ($img_h = 0) {
set $img_h -;
}
image_filter resize $img_w $img_h;
# error_page 415 /50x.html;
}
http://localhost:58080/room-thumbnail/8d71236b692a4c31a9aa5f00ce7d8e7d_300_250.jpeg
参考:
https://www.vultr.com/docs/how-to-compile-nginx-from-source-on-centos-7
https://www.howtoforge.com/how-to-build-nginx-from-source-on-centos-7/
https://blog.csdn.net/pang1258664723/article/details/88545106
以下是庇护祝福的文章copy过来(未成功,可以忽略以下内容)
nginx -V
查看没有安装模块成功
在MAC OS平台上,我们常常使用Homebrew安装程序,但是如果安装成功后需要添加模块,很多同学就不知道怎么处理了。最后往往是采用源码包编译的方式绕过这个问题。
我在安装nginx的时候就遇到了这个问题,分享一下解决的方案: 首先使用brew uninstall nginx卸载原来的程序。
Homebrew安装程序是根据一个Ruby文件来定义软件包安装配置(英文叫做formula,处方,方案的意思)。 使用brew edit nginx 命令可以打开这个文件,里面有一个名为args的数组,只需要把"--with-your_module"添加到这个数组中就可以了。
最后重新用brew install nginx进行安装,安装后由于原先位于/usr/local/bin目录中的软链接没有删除,会报一个错误,使用rm /usr/local/bin/nginx删除原来的软链接,然后使用brew link nginx重新生成软链接即可。
(我没有遇到这个问题)