银河麒麟V10sp1服务器操作系统X86版本编译安装nginx1.8.1

戈宏义
2023-12-01

编译安装nginx


前言

目前在企业内部web服务设计架构中,nginx的作为web服务器被广泛应用,编译安装能够更符合企业的实际需求以及应用


一、nginx是什么?

nginx是一款提供静态页面展示的web服务器

二、使用步骤

1.下载nginx安装包

RPM包获取:Index of /packages/
源码包获取:Index of /download/

2. 创建nginx用户和组

提示:如果之前用rpm包安装,先卸载,删除nginx用户
[root@uplooking tools]# groupadd -r -g 108 nginx
[root@uplooking tools]# useradd -r -u 108 -g 108 nginx
[root@uplooking tools]# id nginx
uid=108(nginx) gid=108(nginx) 组=108(nginx)

3. 解压

[root@uplooking tools]# tar xf nginx-1.8.0.tar.gz -C /usr/local/src/

4. 准备编译配置文件

#安装依赖包:pcre-devel、zlib-devel

[root@uplooking ~]# yum install -y pcre-devel openssl-devel

#准备编译配置文件


[root@uplooking ~]# cd /usr/local/src/nginx-1.8.0/
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre

5.参数详解

-prefix= 指向安装目录
--sbin-path 指向(执行)程序文件(nginx)
--conf-path= 指向配置文件(nginx.conf)
--error-log-path= 指向错误日志目录
--http-log-path= 指向访问日志
--pid-path= 指向pid文件(nginx.pid)
--lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--user= 指定程序运行时的非特权用户
--group= 指定程序运行时的非特权用户组
--with-http_ssl_module 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
--with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--http-client-body-temp-path= 设定http客户端请求临时文件路径
--http-proxy-temp-path= 设定http代理临时文件路径
--http-fastcgi-temp-path= 设定http fastcgi临时文件路径
--http-uwsgi-temp-path= 设定http uwsgi临时文件路径
--http-scgi-temp-path= 设定http scgi临时文件路径
--with-pcre 启用pcre库
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx)与应用服务器(如uWSGI服务器)通信的一种规范。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
	WSGI是一种通信协议。
	uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
	而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。

SCGI协议是一个CGI(通用网关接口)协议的替代品· 它是一个应用与HTTP服务器的接口标准,类似于FastCGI,但是它设计得更为容易实现·

CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。

6.添加nginx.service

[root@uplook system]# vim /usr/lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /var/run/nginx/nginx.pid
#ExecStartPre=/usr/sbin/nginx -t
ExecStartPre=/usr/bin/mkdir -p /var/tmp/nginx/
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable nginx

[root@localhost ~]# systemctl start nginx.service 
[root@localhost ~]# systemctl status nginx.service 
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-07-05 14:04:02 CST; 11s ago
  Process: 17549 ExecStartPre=/usr/bin/rm -f /var/run/nginx/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 17551 ExecStartPre=/usr/bin/mkdir -p /var/tmp/nginx/ (code=exited, status=0/SUCCESS)
  Process: 17553 ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 17554 (nginx)
    Tasks: 2
   Memory: 1.4M
   CGroup: /system.slice/nginx.service
           ├─17554 nginx: master process /usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─17555 nginx: worker process

7月 05 14:04:02 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
7月 05 14:04:02 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@uplooking ~]# netstat -lnpt | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5268/nginx          
[root@uplooking ~]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   5268  root    6u  IPv4  24467      0t0  TCP *:http (LISTEN)
nginx   5270 nginx    6u  IPv4  24467      0t0  TCP *:http (LISTEN)
访问测试:
浏览器访问http://主机IP

 三.加载第三方模块


NGINX 3rd Party Modules | NGINX
常用的第三方模块:
1.Development Kit
GitHub - vision5/ngx_devel_kit: Nginx Development Kit - an Nginx module that adds additional generic tools that module developers can use in their own modules
Nginx的开发工具包
 
2.Echo
HTTP Echo Module | NGINX
便捷命令,输出nginx信息
 
3.Extended status module 比较重要
wiki.nginx.org Managed WordPress Site – Just another WordPress site
Nginx status模块的扩展
 
4.Foot filter
wiki.nginx.org Managed WordPress Site – Just another WordPress site
在页面输出底部加入字符串
 
5.GeoIP 重要
wiki.nginx.org Managed WordPress Site – Just another WordPress site
IP地址识别
 
6.HTTP Push
https://pushmodule.slact.net/
将Nginx改装成comet服务
使用 HTTP 长连接、无须浏览器安装插件的两种“服务器推”



7.Limit Upload Rate 重要
GitHub - cfsego/limit_upload_rate: Limit upload rate
限制客户端上传速率
 下载模块后重新编译

8.Lua
Lua | NGINX
将lua嵌入到Nginx 极其好用的module
 Lua 是一个小巧的脚本语言

9.ModSecurity
http://www.modsecurity.org/projects/modsecurity/nginx/index.html
web应用防火墙
 增强web的安全性

10.PageSpeed
http://ngxpagespeed.com/ngx_pagespeed_example/
google开发的 高性能传输、低延时,基于nginx的页面加速
 
11.Secure Download
Secure Download | NGINX
创建安全链接
 
12.SysGuard 当web服务器并发量太大,负载太高,就会停止这个服务,保护系统
GitHub - alibaba/nginx-http-sysguard: A Nginx module to protect servers when system load or memory use goes too high.
当系统负载过高时,保护系统

1.nginx第三方模块安装方法


./configure --prefix=/你的安装目录 --add-module=/第三方模块目录
1) 在未安装nginx的情况下安装nginx第三方模块
[root@uplooking local]# unzip ngx_pagespeed-master.zip
[root@uplooking ~]# cd /usr/local/src/nginx-1.8.0/
# ./configure

--add-module=/usr/local/ngx_pagespeed-master

#make
#make install
2)在已安装nginx情况下安装nginx模块
[root@uplooking local]# unzip ngx_pagespeed-master.zip (mv ngx_pagespeed-master /usr/local )
[root@uplooking tools]# tar xf 1.9.32.10.tar.gz -C /usr/local/ngx_pagespeed-master
#提示:1.9.32.10.tar.gz为ngx_pagespeed-master支持库文件。
[root@uplooking ~]# cd /usr/local/src/nginx-1.8.0/
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--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/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
--add-module=/usr/local/ngx_pagespeed-master/
[root@uplooking nginx-1.8.0]# make make install之后主页会全部初始化
#编译完成后将生成的二进制文件覆盖现有的二进制文件。通常编译出来的二进制文件位于源码目录的objs/nginx
[root@uplooking nginx-1.8.0]# /etc/init.d/nginx stop
[root@uplooking nginx-1.8.0]# cp objs/nginx /usr/sbin/nginx
cp:是否覆盖"/usr/sbin/nginx"? y
[root@uplooking nginx-1.8.0]# /etc/init.d/nginx start
正在启动 nginx: [确定]

总结

安装完毕nginx添加第三方模块总结:
1、 安装nginx安装第三方模块实际上是使用--add-module重新安装一次nginx;
2、 不要make install而是直接把编译目录下objs/nginx文件直接覆盖老的nginx文件/usr/sbin/nginx;
3、 需要安装多个nginx第三方模块,只需要多指定几个相应的--add-module即可;
4、 重新编译的时候,记得一定要把以前编译过的模块一同加到configure参数里面;
5、 某些模块可能需要额外的系统安装库支持

 类似资料: