CentOS 8 安装 OpenResty 详细教程
| 项目 | 要求 |
|---|---|
| 操作系统 | CentOS 8 (x86_64) |
| 内存 | >= 1 GB |
| 权限 | root 或 sudo |
一、添加 OpenResty 官方源
Section titled “一、添加 OpenResty 官方源”# 安装 yum-utils(提供 yum-config-manager)dnf install -y yum-utils
# 添加 OpenResty 官方 CentOS 8 仓库yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo二、安装 OpenResty
Section titled “二、安装 OpenResty”dnf install -y openresty openresty-resty openresty-opm| 包名 | 说明 |
|---|---|
openresty | 核心主程序 |
openresty-resty | resty 命令行工具 |
openresty-opm | OpenResty 包管理器 |
三、目录结构说明
Section titled “三、目录结构说明”/usr/local/openresty/├── nginx/│ ├── conf/│ │ ├── nginx.conf # 主配置文件│ │ └── conf.d/ # 站点配置目录(需手动创建)│ ├── html/ # 默认网站根目录│ └── logs/ # 日志目录├── lualib/ # 内置 Lua 库└── bin/ └── openresty # 可执行文件(软链至 nginx)四、基础配置
Section titled “四、基础配置”4.1 创建站点配置目录
Section titled “4.1 创建站点配置目录”mkdir -p /usr/local/openresty/nginx/conf/conf.d4.2 修改主配置文件
Section titled “4.2 修改主配置文件”vi /usr/local/openresty/nginx/conf/nginx.conf在 http {} 块末尾、} 之前添加一行,引入站点配置:
http { # ... 原有内容保持不变 ...
include conf.d/*.conf;}4.3 创建示例站点配置
Section titled “4.3 创建示例站点配置”cat > /usr/local/openresty/nginx/conf/conf.d/default.conf << 'EOF'server { listen 80; server_name _;
location / { default_type text/plain; content_by_lua_block { ngx.say("OpenResty is running.") } }
# 静态文件服务示例 location /static/ { root /usr/local/openresty/nginx/html; }
# 反向代理示例(按需启用) # location /api/ { # proxy_pass http://127.0.0.1:8080/; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # }}EOF4.4 检查配置语法
Section titled “4.4 检查配置语法”/usr/local/openresty/bin/openresty -t输出 syntax is ok 和 test is successful 即正常。
五、启动与自启
Section titled “五、启动与自启”# 启动systemctl start openresty
# 设置开机自启systemctl enable openresty
# 查看状态systemctl status openresty六、开放防火墙端口
Section titled “六、开放防火墙端口”firewall-cmd --permanent --add-service=httpfirewall-cmd --permanent --add-service=httpsfirewall-cmd --reload七、验证安装
Section titled “七、验证安装”# 验证版本/usr/local/openresty/bin/openresty -v
# 本机访问测试curl http://127.0.0.1期望输出:OpenResty is running.
八、反向代理配置示例
Section titled “八、反向代理配置示例”以下为将 / 路径代理至后端服务的完整示例:
cat > /usr/local/openresty/nginx/conf/conf.d/proxy.conf << 'EOF'upstream backend { server 127.0.0.1:8080; keepalive 32;}
server { listen 80; server_name example.com;
client_max_body_size 100m;
location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 300s; }}EOF
# 重载配置systemctl reload openresty九、常用管理命令
Section titled “九、常用管理命令”# 启动 / 停止 / 重启 / 重载(不中断连接)systemctl start openrestysystemctl stop openrestysystemctl restart openrestysystemctl reload openresty
# 手动方式(等效)/usr/local/openresty/bin/openresty -s reload/usr/local/openresty/bin/openresty -s stop
# 查看实时访问日志tail -f /usr/local/openresty/nginx/logs/access.log
# 查看错误日志tail -f /usr/local/openresty/nginx/logs/error.log十、常见问题排查
Section titled “十、常见问题排查”| 问题 | 原因 | 解决方法 |
|---|---|---|
dnf 找不到 openresty 包 | 官方源未添加成功 | 重新执行第一节,检查 repo 文件是否生成 |
| 80 端口无法访问 | 防火墙未放行 | 执行第六节开放端口 |
nginx.conf 语法错误 | 配置有误 | 运行 openresty -t 查看具体行号 |
| 启动失败报端口占用 | 其他进程占用 80 | ss -tlnp | grep :80 查找并处理冲突进程 |
include conf.d/*.conf 无效 | 目录不存在 | 执行 4.1 节创建目录 |
© 2025-2026 LiuXing. All Rights Reserved.