0基础自学linux运维-2.12-centos 7 nginx编译安装模块

一、前言

我在《0基础自学linux运维-2.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++需要高版本的看我相关的文章

yum -y install gcc gcc++ zlib zlib-devel openssl openssl-devel pcre pcre-devel


#el7,其中el(Red Hat E nterprise L inux(EL)的缩写),7表示系统版本

#EL7是Red Hat 7.x,CentOS 7.x和CloudLinux 7.x的下载

rpm -ih http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install nginx -y

2.1.2 查看nginx编译参数

[root@vm61 ~]# nginx -V

nginx version: nginx/1.16.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

built with OpenSSL1.0.2k-fips  26 Jan 2017

TLS SNI support enabled

configure arguments:--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--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2-fexceptions -fstack-protector-strong --param=ssp-buffer-size=4-grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro-Wl,-z,now -pie'

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

--with-cc-opt='-O2 -g -pipe -Wall-Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong--param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -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,如果是在自己的虚拟机并不是在要编译安装的机子,不用卸载也行

yum 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

按照步骤进行安装:

2.2.2 下载nignx源码

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

或直接使用wget命令:

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

2.2.3 下载echo模块

https://github.com/openresty/echo-nginx-module/tags 目前最新版本为0.61

#不要用wget下载,会变样的,直接点击下载就好,安装lrzsz(yum install rzsz -y)然后#用cd命令进到要存储echo安装模块的目录中,最后把echo模块的tar.gz包拖到xshell

#界面中就行了就会自动上传,并解压,我这里统一放在/disk1/tools中

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


2.2.4 安装

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

yum -y install gcc gcc++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

#解压刚刚下载的nginx-1.16.0.tar.gz包

tar -xf nginx-1.16.0.tar.gz

cd nginx-1.16.0

#如果不懂./configure用法的话可以使用./configure --hlep命令

./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

make install

cd ..

rm -rf nginx-1.16.0 echo-nginx-module-0.61

注:有的编译用的是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


#语法检查一下

[root@vm61 conf.d]# nginx -t

nginx: the configuration file

/etc/nginx/nginx.conf syntax is ok

nginx: [emerg] mkdir()

"/var/cache/nginx/client_temp" failed (2: No such file or directory)

nginx: configuration file

/etc/nginx/nginx.conf test failed

#解决方法,建立相关目录

mkdir -pv /var/cache/nginx/client_temp

chown nginx.nginx -R /var/cache/nginx

nginx -t


2.4 复制启动脚本

cp/disk1/tools/nginx/nginx.service /usr/lib/systemd/system/nginx.service

#启动nginx

systemctl start nginx

systemctl status nginx

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


三、测试

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

cd /etc/nginx/conf.d/

cp default.conf default.conf.orig

vi default.conf

#在“location

/{ ... }上一行添加

    location /t1 {

      echo "this is t1"

    }

#输入“:wq”退出

#平滑重启,生产环境建议用平滑重启,如果不行再重启

nginx -t


四、结束语

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容