系统环境

标题 版本 备注
系统版本 ubuntu 22.10 -
内核版本 5.15.0-45-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
Tengine v3.0.0 -

OpenResty 简介

OpenResty 官方网址: https://openresty.org/cn/

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

OpenResty 安装

以下操作都以 root 身份执行

脚本安装

手动安装

安装前的准备

1.添加 nginx 用户,禁止其登陆系统:

1
useradd -s /sbin/nologin nginx

2.安装必要的编译包和模块依赖包:

1
apt install -y autoconf automake libtool libmaxminddb-dev libxslt1-dev libgd-dev libgeoip-dev libgoogle-perftools-dev  libunwind-dev        

依赖及模块的下载

下载解压 pcre:

点击这里下载 pcre 源码包到 /usr/local/src/ 目录下并解压

1
2
wget --no-check-certificate -O /usr/local/src/pcre-8.45.tar.gz https://zenlayer.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
tar -zxf /usr/local/src/pcre-8.45.tar.gz -C /usr/local/src/
下载解压 openssl:

点击这里下载 openssl 源码包到 /usr/local/src/ 目录下并解压

1
2
wget -O /usr/local/src/openssl-1.1.1u.tar.gz https://www.openssl.org/source/old/1.1.1/openssl-1.1.1u.tar.gz
tar -zxf /usr/local/src/openssl-1.1.1u.tar.gz -C /usr/local/src/
下载解压 zlib:

点击这里下载 zlib 源码包到 /usr/local/src/ 目录下并解压

1
2
wget -O /usr/local/src/zlib-1.3.1.tar.gz https://www.zlib.net/zlib-1.3.1.tar.gz
tar -zxf /usr/local/src/zlib-1.3.1.tar.gz -C /usr/local/src/
下载 libatomic_ops 源文件

点击这里从 github 克隆 libatomic_ops 源码文件到 /usr/local/src/ 目录下并执行 ./autogen.sh:

1
2
3
4
5
6
7
8
9
10
11
# 同步的代码
git clone https://github.com/ivmai/libatomic_ops.git /usr/local/src/libatomic_ops

# 进入同步目录
cd /usr/local/src/libatomic_ops

# 生成 configure 文件
./autogen.sh

# 做库文件的软链接
ln -s /usr/local/src/libatomic_ops/src/.libs/libatomic_ops.a /usr/local/src/libatomic_ops/src/
下载 IP 定位模块:ngx_http_geoip2_module

点击这里从 github 克隆 ngx_http_geoip2_module 源码文件到 /usr/local/src/ 目录下:

1
git clone https://github.com/leev/ngx_http_geoip2_module.git /usr/local/src/ngx_http_geoip2_module
下载目录索引模块:ngx-fancyindex

点击这里从 github 克隆 ngx-fancyindex 源码文件到 /usr/local/src/ 目录下:

1
git clone https://github.com/aperezdc/ngx-fancyindex.git /usr/local/src/ngx-fancyindex
下载第三方开源健康检查模块: nginx_upstream_check_module

点击这里从 github 克隆 nginx_upstream_check_module 源码文件到 /usr/local/src/ 目录下:

1
git clone https://github.com/nginx-modules/nginx_upstream_check_module.git /usr/local/src/nginx_upstream_check_module
下载安装 LuaJIT

1.点击这里克隆 luajit/usr/local/src/ 目录下:

1
git clone https://github.com/openresty/luajit2.git /usr/local/luajit

2.进入克隆目录:

1
cd /usr/local/src/luajit/

3.编辑及安装 LuaJIT:

1
2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

下载安装 OpenResty

1.点击这里下载 OpenResty 源码包到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/openresty-1.25.3.1.tar.gz https://openresty.org/download/openresty-1.25.3.1.tar.gz

2.将下载下来的源码包解压到下载目录:

1
tar -zxf /usr/local/src/openresty-1.25.3.1.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/openresty

4.配置编译参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
./configure --prefix=/usr/local/openresty \
--with-luajit=/usr/local/luajit \
--user=nginx \
--group=nginx \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module \
--http-log-path=/usr/local/openresty/logs/access.log \
--http-client-body-temp-path=/usr/local/openresty/tmp/client_body \
--http-proxy-temp-path=/usr/local/openresty/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/openresty/tmp/fastcgi \
--with-mail=dynamic \
--with-mail_ssl_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-google_perftools_module \
--add-module=/usr/local/src/ngx_http_geoip2_module \
--add-module=/usr/local/src/ngx-fancyindex \
--add-module=/usr/local/src/nginx_upstream_check_module \
--with-compat \
--with-pcre=/usr/local/src/pcre-8.45/ \
--with-zlib=/usr/local/src/zlib-1.3.1/ \
--with-libatomic=/usr/local/src/libatomic_ops/ \
--with-openssl=/usr/local/src/openssl-1.1.1u/

5.编译安装:

1
gmake && gmake install

6.安装完成后,别忘记了在 openresty 安装目录里面创建临时目录:

1
mkdir -p /usr/local/openresty/tmp/{client_body,proxy,fastcgi}

7.在 /etc/profile.d/ 目录下创建 openresty 环境变量文件 openresty.sh ,内容为:

1
2
3
cat << 'EOF' > /etc/profile.d/openresty.sh
PATH=$PATH:/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin
EOF

8.执行命令 source /etc/profile.d/openresty.sh 使环境立即生效:

1
source /etc/profile.d/openresty.sh

9.在 /lib/systemd/system/ 目录下创建 openresty 启动脚本文件 openresty.service ,内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat << 'EOF' > /lib/systemd/system/openresty.service
[Unit]
Description=openresty
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s quit
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

10执行命令 systemctl daemon-reload 加载启动脚本文件:

1
systemctl daemon-reload

11.执行命令 systemctl enable --now openresty.service 启动 openresty 并将其设置为开机启动:

1
systemctl enable --now openresty.service

12.查看 openresty 启动状。如果 active 为 running ,则代表 openresty 启动成功,否则请根据系统日志 /var/log/syslog 进行排错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@vm002-ubuntu:~# systemctl status openresty
● openresty.service - openresty
Loaded: loaded (/lib/systemd/system/openresty.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-04-12 08:46:38 CST; 2min 30s ago
Process: 75987 ExecStart=/usr/local/openresty/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 75988 (nginx)
Tasks: 2 (limit: 9389)
Memory: 4.1M
CPU: 23ms
CGroup: /system.slice/openresty.service
├─75988 "nginx: master process /usr/local/openresty/nginx/sbin/nginx"
└─75989 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Apr 12 08:46:38 vm002-ubuntu systemd[1]: Starting openresty...
Apr 12 08:46:38 vm002-ubuntu systemd[1]: Started openresty.

监听的端口:

1
2
root@vm002-ubuntu:~# ss -lnpt |egrep 80
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=75989,fd=8),("nginx",pid=75988,fd=8))

13.打开浏览器,输入服务器IP,查看页面如下:

14.编辑 /usr/local/openresty/nginx/conf/nginx.conf 文件,在默认的虚拟主机里面添加解析 lua 的测试配置(其它保持不动),如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("Hello, Lua")';
}
}

15.测试下配置及重载下 nginx 服务:

1
nginx -t && nginx -s reload

16.访问测试:

报错及解决方法

启动报错

错误一:nginx: [alert] detected a LuaJIT version which is not OpenResty’s

详细信息:

1
2
3
4
5
Apr 12 08:26:08 vm002-ubuntu nginx[52820]: nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
Apr 12 08:26:08 vm002-ubuntu nginx[52820]: nginx: [emerg] getpwnam("nginx") failed
Apr 12 08:26:08 vm002-ubuntu systemd[1]: openresty.service: Control process exited, code=exited, status=1/FAILURE
Apr 12 08:26:08 vm002-ubuntu systemd[1]: openresty.service: Failed with result 'exit-code'.
Apr 12 08:26:08 vm002-ubuntu systemd[1]: Failed to start openresty.

报错原因:

由于一开始我使用的不是 openresty 项目作者 提供的 LuaJIT 库(使用的是 https://luajit.org/download.html 官方库),所以启动报了上面的错。

解决方法:

重新克隆下openresty 项目作者 提供的 LuaJIT 库进行编译安装。然后重装 openresty 即可!

错误二:nginx: [emerg] getpwnam(“nginx”) failed

详细信息:

1
2
3
4
Apr 12 08:36:32 vm002-ubuntu nginx[75963]: nginx: [emerg] getpwnam("nginx") failed
Apr 12 08:36:32 vm002-ubuntu systemd[1]: openresty.service: Control process exited, code=exited, status=1/FAILURE
Apr 12 08:36:32 vm002-ubuntu systemd[1]: openresty.service: Failed with result 'exit-code'.
Apr 12 08:36:32 vm002-ubuntu systemd[1]: Failed to start openresty.

报错原因:

忘记添加 nginx 用户了

解决方法:

为系统添加 nginx 用户:

1
usseradd -s /sbin/nologin nginx

错误三:nginx: [emerg] mkdir() “/usr/local/openresty/tmp/client_body” failed (2: No such file or directory)

详细信息:

1
2
3
4
Apr 12 08:37:07 vm002-ubuntu nginx[75977]: nginx: [emerg] mkdir() "/usr/local/openresty/tmp/client_body" failed (2: No such file or directory)
Apr 12 08:37:07 vm002-ubuntu systemd[1]: openresty.service: Control process exited, code=exited, status=1/FAILURE
Apr 12 08:37:07 vm002-ubuntu systemd[1]: openresty.service: Failed with result 'exit-code'.
Apr 12 08:37:07 vm002-ubuntu systemd[1]: Failed to start openresty.

报错原因:

这个问题是因为在 configure openresty 时指定了 --http-client-body-temp-path=xxx ,而在 openresty 安装完成后,并没有生成指定的目录

解决方法:

为系统添加 nginx 用户:

1
mkdir -p /usr/local/openresty/tmp/{client_body,proxy,fastcgi}