Ubuntu以apt-get 方式安装nginx后,如何增加扩展模块?

作者: xusx 分类: Linux 发布时间: 2021-01-16 15:59 浏览:1,332

一、【nginx -V】查看nginx版本、已安装模块

root@ubuntu:/usr/local/src# /usr/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4)
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --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 --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_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-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

★ubuntu apt-get 目前默认安装完nginx后是1.4.6版的,以下是对该版本的升级。

Nginx Stable PPA是由Ubuntu社区维护的源,本源更新自稳定版分支,是Kaijia目前使用的源,这个源的特点是文件的目录结构和Ubuntu自带的Nginx相同,因此安装这个版本时不需要修改/etc/nginx/下面的配置文件。

不过这个源更新比较慢,一般Nginx新版本发布后要过一至两周才会更新,但质量是保证的。

安装此源只需要在终端中运行:
apt-add-repository ppa:nginx/stable
然后即可安装Nginx
apt-get update
apt-get install nginx

Ubuntu下升级nginx到最新版
1. 执行如下命令
lsb_release –a【安装 sudo apt-get install lsb-core】
获取ubuntu的Codename,我的系统是 trusty,记住这个代号

2. 编辑/etc/apt/source.list,加入如下内容
deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx
然后执行 apt-get update 和 apt-get install nginx 获取当前比较新的稳定版本的nginx。如果出现错误如:W: GPG error: http://nginx.org trusty InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62,则执行sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys ABF5BD827BD9BF62,再执行 apt-get update 和 apt-get install nginx 。

想要知道nginx安装都被放在那里,简单的使用 whereis nginx 即可。此种方式安装的nginx,配置文件在/etc/nginx/目录下。

3. 可创建用户专用与nginx
groupadd nginx
useradd –g nginx nginx
修改配置文件
/etc/nginx/nginx.conf
首行 user nginx; 即可使用当前创建的nginx用户运行。

★另外,彻底卸载Nginx的步骤如下:

1、停止nginx的服务
sudo service nginx stop
2、删除nginx,–purge包括配置文件
sudo apt-get –purge remove nginx
3、自动移除全部不使用的软件包
sudo apt-get autoremove
4、列出与nginx相关的软件 并删除显示的软件
dpkg –get-selections|grep nginx
sudo apt-get –purge remove nginx
sudo apt-get –purge remove nginx-common
sudo apt-get –purge remove nginx-core
5、再次执行
dpkg –get-selections|grep nginx
which nginx # 不在显示nginx

二、下载 nginx 源码

nginx 添加新的模块必须要重新编译,所以先下载 nginx 源码。

cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxf nginx-1.18.0.tar.gz

三、下载 ngx-waf 模块源码

在这里以安装ngx-waf 模块为例,其是一个用于 nginx 的没有复杂配置的 Web 应用防火墙模块。(相关文档请看https://github.com/ADD-SP/ngx_waf/blob/master/README-ZH-CN.md)

cd /usr/local/src
git clone https://github.com/ADD-SP/ngx_waf.git
cd ngx_waf

推荐使用 nginx-1.18.0 的源码,若使用低版本的 nginx 源码则不保证本模块可以正常使用。

四、编译和安装模块(静态)

从 nginx-1.9.11 开始,nginx 开始支持动态模块。

静态模块将所有模块编译进一个二进制文件中,所以增删改模块都需要重新编译 nginx 并替换。

动态模块则动态加载 .so 文件,无需重新编译整个 nginx。只需要将模块编译成 .so 文件然后修改nginx.conf即可加载对应的模块。

root@ubuntu:/usr/local/src# apt-get install gcc make [注:若没安装的话,下同]
root@ubuntu:/usr/local/src# apt-get install uthash-dev libssl-dev
root@ubuntu:/usr/local/src# apt-get install libpcre3 libpcre3-dev
root@ubuntu:/usr/local/src# apt-get install libxml2 libxml2-dev libxslt-dev
root@ubuntu:/usr/local/src# apt-get install libgd2-xpm
root@ubuntu:/usr/local/src# apt-get install libgd2-xpm-dev
root@ubuntu:/usr/local/src# apt-get install geoip-database libgeoip-dev
root@ubuntu:/usr/local/src# cd nginx-1.18.0
root@ubuntu:/usr/local/src/nginx-1.18.0/# ./configure xxx --add-module=/usr/local/src/ngx_waf

./configure xxx –add-module=/usr/local/src/ngx_waf

xxx 为其它的编译参数,一般来说是将 xxx 替换为nginx -V显示的编译参数。

如果运行失败,一般报 xxx not found 的错误, 通过执行apt-get install xxx或 apt-get install xxx-dev来解决。

编译结果部分如下:

......
ngx_http_waf_module was configured
......

nginx path prefix: "/etc/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/lib/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

必须是无任何报错才是编译成功。编译完成接着执行make,注意make完不要make install不然的话就等于是通过源码直接编译安装nginx,用不着上面apt-get install nginx了)

编译完成后回再当前Nginx目录下objs文件下出现nginx可执行文件。

替换Nginx可执行文件,可以先保存一份之前,防止错误无法恢复。mv /usr/sbin/nginx /usr/sbin/nginx.bak,cp objs/nginx /usr/sbin/,此时新模块就加载完了。

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注