OpenResty 本质上是一个基于 Nginx 的增强版本,集成了 LuaJIT 和大量第三方模块。如果你想用 OpenResty 替换标准的 Nginx,可以按照以下步骤操作:
如果系统已安装 Nginx,建议先卸载以避免冲突:
# 对于 Ubuntu/Debian
sudo apt remove nginx nginx-common nginx-core
# 对于 CentOS/RHEL
sudo yum remove nginx
OpenResty 提供了官方预编译包,推荐通过官方仓库安装:
# 安装依赖
sudo apt-get install -y libpcre3-dev libssl-dev perl make build-essential curl zlib1g-dev
# 添加 OpenResty 仓库
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list
sudo apt-get update
# 安装 OpenResty
sudo apt-get install -y openresty
# 添加 OpenResty 仓库
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
# 安装 OpenResty
sudo yum install -y openresty
OpenResty 完全兼容 Nginx 的配置文件,但需要检查路径差异:
- 配置文件路径:
OpenResty 的默认配置文件路径通常为 /usr/local/openresty/nginx/conf/nginx.conf
(源码安装)或 /etc/openresty/nginx.conf
(包管理安装)。
- 迁移步骤:
1. 备份原有 Nginx 配置(/etc/nginx/nginx.conf
或 /etc/nginx/conf.d/
)。
2. 将配置复制到 OpenResty 的对应路径,例如:
bash
sudo cp /etc/nginx/nginx.conf /etc/openresty/nginx.conf
sudo cp -r /etc/nginx/conf.d/ /etc/openresty/
3. 检查配置语法:
bash
sudo openresty -t
# 启用并启动服务(Systemd)
sudo systemctl enable openresty
sudo systemctl start openresty
# 检查状态
sudo systemctl status openresty
http://服务器IP
,确认服务正常运行。bash
openresty -v
输出应类似:
nginx version: openresty/1.21.4.1
nginx
location /test {
content_by_lua_block {
ngx.say("Hello, OpenResty!")
}
}
opm
:
bash
opm get ledgetech/lua-resty-http
openresty
,但实际调用的仍是 Nginx(路径可能为 /usr/local/openresty/nginx/sbin/nginx
)。通过以上步骤,你可以无缝替换 Nginx 为 OpenResty,并利用其强大的 Lua 扩展能力。如果有特殊需求(如自定义模块),可以考虑从源码编译 OpenResty。