Centos7.9 源码安装Openresty1.21.4.1,若重启验证不生效&#


1、环境及版本

系统版本: Centos 7.9.2009

Openresty: 1.21.4.1

2、必要组件安装(执行 yum 命令安装)

yum install openssl-devel gcc curl pcre-devel -y

3、获取安装包、解压

访问官网地址: https://openresty.org

获取下载链接: https://openresty.org/download/openresty-1.21.4.1.tar.gz

# 切换到目录
cd /usr/local

# wget 下载安装包
wget  https://openresty.org/download/openresty-1.21.4.1.tar.gz

# 解压命令
tar -zxvf openresty-1.21.4.1.tar.gz

# 进入解压目录
cd openresty-1.21.4.1

4、修改源码,以支持` ip_hash ` 可以在内网同网段生效,以实现负载均衡

[root@nginx openresty-1.21.4.1]# vim bundle/nginx-1.21.4/src/http/modules/ngx_http_upstream_ip_hash_module.c
...
# 修改位置一:  第 80 行,数字3改为4 ngx_http_upstream_ip_hash_pseudo_addr[3] 修改为 ngx_http_upstream_ip_hash_pseudo_addr[4] 

80 static u_char ngx_http_upstream_ip_hash_pseudo_addr[3];
81 
...
# 修改位置二:  第 124 行,数字3改为4; [iphp->addrlen = 3] 修改为 [iphp->addrlen = 4] 

121     case AF_INET:
122         sin = (struct sockaddr_in *) r->connection->sockaddr;
123         iphp->addr = (u_char *) &sin->sin_addr.s_addr;
124         iphp->addrlen = 3;
125         break;
126 
127 #if (NGX_HAVE_INET6)
128     case AF_INET6:
129         sin6 = (struct sockaddr_in6 *) r->connection->sockaddr    ;
130         iphp->addr = (u_char *) &sin6->sin6_addr.s6_addr;
131         iphp->addrlen = 16;
132         break;
133 #endif
134 
# 修改位置三:  第 137 行,数字3改为4; [iphp->addrlen = 3] 修改为 [iphp->addrlen = 4] 

135     default:
136         iphp->addr = ngx_http_upstream_ip_hash_pseudo_addr;
137         iphp->addrlen = 3;
138     }

5、安装

# 执行configure命令 (默认安装目录 `--prefix=/usr/local/openresty`)
# 需要的模块自定义,此处仅示例

./configure --prefix=/usr/local/openresty \
  --with-luajit \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module 

# 执行安装命令
make
make install

6、 开机启动

方法一 (/etc/rc.local):

[root@nginx local]# vim /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local

# 启动 Openresty nginx 服务
/usr/local/openresty/nginx/sbin/nginx
PS: Centos7 默认 rc.local 是不可执行的,仅是为了兼容性添加。

若重启验证不生效,则需要给此文件添加执行权限

注意:此文件执行时, `/etc/profile` 配置的环境变量不会生效

[root@nginx home]# chmod +x /etc/rc.d/rc.local
[root@nginx rc.d]# cd /etc/rc.d/
[root@nginx rc.d]# ll
total 4
drwxr-xr-x. 2 root root  70 Mar 10 15:30 init.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc0.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc1.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc2.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc3.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc4.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc5.d
drwxr-xr-x. 2 root root  45 Mar 10 15:30 rc6.d
-rwxrwxrwx. 1 root root 550 Mar 10 17:36 rc.local
[root@nginx rc.d]#

方法二(使用 service):

在系统服务目录` /usr/lib/systemd/system/`下,创建 ` openresty.service ` 文件

[Unit]
# 服务描述
Description=Openresty
# 在 XX 服务后启动
After=network.target
# 服务运行参数; 注意本节点内命令要用绝对路径
[Service]
# 后台运行方式
Type=forking
# 启动命令
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

# 是否给服务分配独立的临时空间
PrivateTmp=true
# 运行级别下服务安装的相关设置, 可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target

systemctl enable openresty.service    # 设置开机启动
systemctl disable openresty.service   # 停止开机启动
systemctl start openresty.service     # 启动服务
systemctl stop openresty.service      # 停止服务
systemctl status openresty.service    # 查看服务状态
systemctl restart openresty.service   # 重启服务
systemctl list-units --type=service   # 查看所有已启动的服务

相关内容