在 ubuntu 22.04 中,通过源码编译安装 LOMP 中的 Openresty 服务(Lua 版)
关于 PHP 的安装,请参考: 在 ubuntu 22.04 中,通过源码编译安装 LNMP 中的 PHP 环境)
系统环境
标题 | 版本 | 备注 |
---|---|---|
系统版本 | 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 | 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 |
下载解压 openssl:
点击这里下载 openssl
源码包到 /usr/local/src/
目录下并解压
1 | 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 |
下载解压 zlib:
点击这里下载 zlib
源码包到 /usr/local/src/
目录下并解压
1 | wget -O /usr/local/src/zlib-1.3.1.tar.gz https://www.zlib.net/zlib-1.3.1.tar.gz |
下载 libatomic_ops 源文件
点击这里从 github 克隆 libatomic_ops
源码文件到 /usr/local/src/
目录下并执行 ./autogen.sh
:
1 | # 同步的代码 |
下载 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 | make 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 | ./configure --prefix=/usr/local/openresty \ |
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 | cat << 'EOF' > /etc/profile.d/openresty.sh |
8.执行命令 source /etc/profile.d/openresty.sh
使环境立即生效:
1 | source /etc/profile.d/openresty.sh |
9.在 /lib/systemd/system/
目录下创建 openresty 启动脚本文件 openresty.service
,内容为:
1 | cat << 'EOF' > /lib/systemd/system/openresty.service |
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 | root@vm002-ubuntu:~# systemctl status openresty |
监听的端口:
1 | root@vm002-ubuntu:~# ss -lnpt |egrep 80 |
13.打开浏览器,输入服务器IP,查看页面如下:
14.编辑 /usr/local/openresty/nginx/conf/nginx.conf
文件,在默认的虚拟主机里面添加解析 lua 的测试配置(其它保持不动),如下:
1 | server { |
15.测试下配置及重载下 nginx 服务:
1 | nginx -t && nginx -s reload |
16.访问测试:
报错及解决方法
启动报错
错误一:nginx: [alert] detected a LuaJIT version which is not OpenResty’s
详细信息:
1 | 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) |
报错原因:
由于一开始我使用的不是 openresty 项目作者 提供的 LuaJIT 库(使用的是 https://luajit.org/download.html 官方库),所以启动报了上面的错。
解决方法:
重新克隆下openresty 项目作者 提供的 LuaJIT 库进行编译安装。然后重装 openresty 即可!
错误二:nginx: [emerg] getpwnam(“nginx”) failed
详细信息:
1 | Apr 12 08:36:32 vm002-ubuntu nginx[75963]: nginx: [emerg] getpwnam("nginx") failed |
报错原因:
忘记添加 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 | 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) |
报错原因:
这个问题是因为在 configure openresty 时指定了 --http-client-body-temp-path=xxx
,而在 openresty 安装完成后,并没有生成指定的目录
解决方法:
为系统添加 nginx 用户:
1 | mkdir -p /usr/local/openresty/tmp/{client_body,proxy,fastcgi} |