nginx切换OpenResty - CentOS 7.x
nginx切换OpenResty - CentOS 7.x
此方法为:停机切换非平滑切换
此方法为:停机切换非平滑切换
此方法为:停机切换非平滑切换
此方法为:停机切换非平滑切换
此方法为:停机切换非平滑切换
目的是:彻底告别旧路径,全量迁移到新路径,使用 OpenResty 原生命令管理,且配置内不留任何旧路径的痕迹。
在 CentOS 7 上,按照这个流程操作是最干净的:
第一步:安装 OpenResty (官方仓库版)
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
sudo yum install -y openresty
安装完成后,执行文件位于 /usr/bin/openresty,你可以直接使用 openresty 命令,它等同于 nginx。
第二步:停止并物理备份旧 Nginx
/usr/local/nginx/sbin/nginx -s stop
mv /usr/local/nginx /usr/local/nginx.20251223.bak
第三步:全量配置迁移
将旧的配置、证书拷贝到 OpenResty 的默认目录下:
# 拷贝配置和证书
cp /usr/local/nginx.20251223.bak/conf/nginx.conf /usr/local/openresty/nginx/conf/
cp -r /usr/local/nginx.20251223.bak/conf/cert /usr/local/openresty/nginx/conf/
cp -r /usr/local/nginx.20251223.bak/conf/conf.d /usr/local/openresty/nginx/conf/
# 拷贝静态网页文件
cp -r /usr/local/nginx.20251223.bak/html/* /usr/local/openresty/nginx/html/
第四步:快速替换内部路径字符串
使用 sed 命令一次性将配置文件中所有指向旧路径 /usr/local/nginx 的地方修改为新路径 /usr/local/openresty/nginx。
# 搜索并替换 conf 目录下所有文件中的路径字符串
# 这里使用 | 作为分隔符,避免路径中的 / 产生冲突
find /usr/local/openresty/nginx/conf/ -type f -name "*.conf" -print0 | xargs -0 sed -i 's|/usr/local/nginx|/usr/local/openresty/nginx|g'
第五步:测试并启动
现在你可以直接使用 openresty 命令了,不需要输入一长串路径,也不需要 -p 参数(因为配置已经完全对齐了 OpenResty 的默认路径)。
# 1. 语法检查
openresty -t
# 2. 启动服务
openresty
# 3. 检查运行版本
openresty -v
进阶优化:Systemd 管理
你说得对,这里是我考虑不周,应该完全独立。
如果你希望彻底摆脱 nginx 这个名字,确实不应该去修改旧的 nginx.service,而是应该创建一个全新的 openresty.service。这样不仅逻辑清晰,而且以后如果你想彻底卸载旧的 Nginx 软件包,也不会影响 OpenResty 的运行。
以下是实现纯粹 OpenResty 管理的步骤:
1. 创建全新的 OpenResty 服务文件
在 CentOS 7 中,直接新建一个专属服务文件:官方仓库安装完 可能已经存在,已存在则不需要更改
vim /usr/lib/systemd/system/openresty.service
将以下内容粘贴进去:
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nfs-client.target
[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/openresty -t
ExecStart=/usr/bin/openresty
ExecReload=/usr/bin/openresty -s reload
ExecStop=/usr/bin/openresty -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. 彻底禁用旧的 Nginx 服务
为了避免系统启动时尝试去拉取已经不存在的旧 Nginx,我们需要彻底关掉它:
# 停止并禁用旧服务
systemctl stop nginx
systemctl disable nginx
# 重新加载系统服务配置
systemctl daemon-reload
3. 启用并启动 OpenResty
现在,你可以用你想要的方式来管理它了:
# 设置开机自启
systemctl enable openresty
# 启动服务
systemctl start openresty
# 检查状态
systemctl status openresty
验证成果
你可以执行以下命令确认路径是否全部替换成功:
grep -r "/usr/local/nginx" /usr/local/openresty/nginx/conf/
如果没有任何输出,说明替换非常彻底。
最后提醒一点:如果你的 nginx.conf 里面有类似 user nginx; 这样的用户指令,请确保你的系统里已经创建了 nginx 用户,或者将其改为 OpenResty 默认的 nobody。
解决报错
即使你已经删除了 /usr/local/nginx 目录或移动了它,已经在内存中运行的进程是不会自动退出的。导致 OpenResty 启动时发现端口被占用(Address already in use),所以报错。
1. 强制清理残余的 Nginx 进程
由于旧的二进制文件已经没了,nginx -s stop 失效了,我们需要直接杀死进程:
# 找到所有名为 nginx 的进程并杀死
pkill -9 nginx
或者手动检查一下,确认没有进程占用 80/443:
netstat -ntlp | grep -E ':80|:443'
如果输出为空,说明端口已释放。
OpenResty独有功能验证
为了验证你现在的环境确实是 OpenResty(而不仅仅是换了壳的 Nginx),我们利用其核心的 LuaJIT 特性。
1. 修改配置文件
请编辑你的 /usr/local/openresty/nginx/conf/nginx.conf,在某个 server { ... } 块内(最好是默认的 80 端口段)添加一个新的 location:
server {
listen 80;
server_name localhost;
# --- 添加以下测试代码 ---
location /lua_test {
default_type text/plain;
content_by_lua_block {
ngx.say("Hello, OpenResty!")
ngx.say("LuaJIT version: ", jit.version)
ngx.say("Current Time: ", ngx.localtime())
}
}
# -----------------------
# ... 其他原有的配置 ...
}
2. 热重载配置
由于 OpenResty 已经启动,我们直接 reload 即可:
openresty -s reload
3. 测试验证
使用 curl 或浏览器访问该接口:
curl http://127.0.0.1/lua_test
curl http://yourdomain/luatest
预期结果
如果一切正常,你会看到类似下面的输出:
Hello, OpenResty!
LuaJIT version: LuaJIT 2.1.0-beta3
Current Time: 2025-12-23 09:45:12
Comments