hualinux2.8 搭建篇 centos8 nginx编译安装模块

 

目录

一、前言

二、nginx编译安装

2.1 查看nginx编译参数

2.1.1 nginx yum安装

2.1.2 查看nginx编译参数

2.2 编译安装

2.2.1 查看echo模块安装说明

2.2.2 下载nignx源码

2.2.3 安装

2.3 修改配置

2.4 复制启动脚本

三、测试

四、结束语


、前言

我在《hualinux2.1 环境搭建:源码、二进制、yum/apt安装区别》说过:

当yum/apt和二进制安装都不满足需求的情况下才用编译安装。主要思想是IT是讲究效率的时代,明明可以yum这么简单、方便就能完成的,偏偏就有人为了炫耀自己能力强用编译安装,结果“简单的事情复杂化”,也不方便维护,不是相当给自己挖个坑吗?

    有能力不用急着去证明,但出问题你解决不了会证明你多么无能。

“大道至简”,用最简单的方式实现自己想要的东西,能用别人的轮子就没有必要自己造。

二、nginx编译安装

当我们需要ninx编译安装的时候,这里有一个小技巧,就是我先用虚拟机yum安装一个nignx,然后nginx -V知道它的基础编译参数,如果需要添加模块如echo模块、lua模块,nginx

yum默认是没有安装的,只需要在yum安装基础上得到的参数再加上相应模块的参数即可。

nginx模块安装可以看它的官方模块页面,里面每一个模块都有自己的安装说明。

最后把nginx

yum安装卸载,下载对应版本的源码包,进行编译安装就大功告成。

2.1 查看nginx编译参数

2.1.1 nginx yum安装

#安装常用的依赖,如果gcc、gcc++需要高版本的看我相关的文章

#rewrite模块需要用到pcre-devel
dnf install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

#添加nginx的repo源,如果不知道可以看nginx官网的centos yum安装说明

#添加nginx yum源
cat>/etc/yum.repos.d/nginx.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
#查看
cat /etc/yum.repos.d/nginx.repo
#安装nginx,我这里使用的是centos8.1
dnf install nginx -y

2.1.2 查看nginx编译参数

[root@vm811 ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) 
built with OpenSSL 1.1.1c FIPS  28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx 
--modules-path=/usr/lib64/ng --conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log --http-log-path=/varaccess.log 
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock 
--http-client-body-temp-ache/nginx/client_temp 
--http-proxy-temp-path=/var/cache/nginx/proxy_temp 
--http-fastcgi-temp-pate/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/vanx/scgi_temp
--user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_dule
--with-http_auth_request_module --with-http_dav_module --with-http_flv_module 
--with-http_gu --with-http_gzip_static_module --with-http_mp4_module 
--with-http_random_index_module --with-httdule --with-http_secure_link_module 
--with-http_slice_module --with-http_ssl_module --with-http_smodule 
--with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module 
--with-strstream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
--with-cc-opt='-O2Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,
-D_GLIBCXX_ASSERTIONS -fexceptions -fstar-strong -grecord-gcc-switches 
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpmhat-annobin-cc1 
-m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-prPIC' 
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie

有些特殊的选项可以不先,如下面的gcc选项不要

--with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,
-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches 
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-
cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-
protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

我把安美化一下效果如下:其中“空格+\”表示还没输入结束,换行继续。

--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 \
--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

得到上面的编译参数,可以把nginx yum安装卸载了,卸载之前把/etc/nginx/nginx.conf和/etc/nginx/conf.d/default.conf备份出来,这样就可以当yum之前的习惯来使用了

#备份/etc/nginx/nginx.conf和/etc/nginx/conf.d/default.conf
#备份启动文件
mkdir -pv /disk1/tools/nginx
cd /disk1/tools/nginx
cp /etc/nginx/nginx.conf .
cp /etc/nginx/conf.d/default.conf .
cp /usr/lib/systemd/system/nginx.service .
#卸载nignx,如果是在自己的虚拟机并不是在要编译安装的机子,不用卸载也行
dnf remove nginx –y

2.2 编译安装

我以安装nginx插件echo为例子,进行编译安装。

2.2.1 查看echo模块安装说明

Nginx官网所支持的模块列表为https://www.nginx.com/resources/wiki/modules/

打找echo模块的安装说明https://github.com/openresty/echo-nginx-module#installation 

其中有安装方法及下载地址,如下图所示:

点上面的文件列表 echo-nginx-module file list,我这里使用目前最新稳定版本下载tar.gz

2.2.2 下载nignx源码

打开nginx下载官网http://nginx.org/en/download.html找到对应的1.18源码包

或直接使用wget命令:

wget http://nginx.org/download/nginx-1.18.0.tar.gz

2.2.3 安装

#安装常用的依赖

#rewrite模块需要用到pcre-devel
dnf install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

把上面下载完的nginx-1.18.0.tar.gz和echo-nginx-module-0.61.tar.gz插件上传到同一目录中,我这里是/disk1/tools

解压上面2个tra.gz包

tar -xf nginx-1.18.0.tar.gz 
tar -xf echo-nginx-module-0.61.tar.gz 
ll

#按照上面的nginx -V得出的安装参数并去掉了gcc参数后,就是nginx安装参数,再安装echo插件说明,添加多一条

--add-module=/disk1/tools/echo-nginx-module-0.61

所以nginx整个安装操作为:

cd nginx-1.18.0/
./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 \
--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 \
--add-module=/disk1/tools/echo-nginx-module-0.61
#上面输入完之后,直接回车即可以编译

#如果编译没有问题那么执行,如下命令进行安装
make -j2
make install

注:

有的编译用的是cmaek,如果cmake错的话,解决错误之后,cmake前删除前一次产生的cmakecache.txt再进行cmake即可

2.3 修改配置

整一下nginx以yum方式安装的配置,个人比较习惯,因为我是在本机yum改为源码安装,所以配置直接复制,如果是非其它机子,上传一下即可。

#添加nginx用户,如果是yum安装过则不用添加
groupadd nginx
useradd -s /sbin/nologin -g
nginx -M nginx

cd /etc/nginx/
mv nginx.conf nginx.conf.orig
cp /disk1/tools/nginx/nginx.conf .

#建立原conf.d目录
mkdir conf.d
cd conf.d/
cp /disk1/tools/nginx/default.conf .

#yum中的default.conf的hmlt默认为/usr/share/nginx/html,源码安装默认为
# /etc/nginx/html,所以要修改一下
sed -i 's#/usr/share/nginx/html#/etc/nginx/html#g' default.conf
cat default.conf

#语法检查一下
nginx -t

2.4 复制启动脚本

cp /disk1/tools/nginx/nginx.service /usr/lib/systemd/system/nginx.service
#启动nginx
systemctl start nginx
systemctl status nginx

#添加开机启动
systemctl enable nginx

#打开浏览器输入nginx所在的IP地址,我这里是192.168.3.11,效果如下:

三、测试

echo插件安装了,现在测试一下echo功能,操作如下:

cd /etc/nginx/conf.d/
#备份
cp default.conf default.conf.$(date +%Y%m%d)

sed -i '/\/ {/i\    location /t1 {' default.conf
sed -i '/\/ {/i\        echo "this is t1";' default.conf
sed -i '/\/ {/i\    }\n' default.conf
grep -A 2 '/t1' default.conf

效果是在配置文件中插入红色框部分内容

#语法检查
nginx -t
#平滑重启
systemctl reload nginx
#如果不生效则直接重启
systemctl restart nginx

输入浏览器,访问/t1,我这里的ip地址为:192.168.3.11,效果图如下:

注:有的浏览会变成下载,如火狐浏览器

四、结束语

我这里是借助nginx yum安装,借用了它的配置及启动脚本,因为是官方的所以一般会比自己写的好,也实现了用最简单的方法达到自己的目的。

无论安装什么插件一般看插件官方的document,要么就是看所在软件的模块列表是否有该插件的安装文档。然后照着安装就行了。我相信你也会用nginx安装其它插件了,如lua这个也是安装频率比较高的。

原创文章 57 获赞 2 访问量 3021

猜你喜欢

转载自blog.csdn.net/hualinux/article/details/105944650