1. 系统要求

1.1 操作系统支持

  • Linux:Ubuntu、CentOS、Debian、RHEL等
  • macOS:10.9及以上版本
  • Windows:通过WSL或Docker支持
  • FreeBSD:9.0及以上版本

1.2 硬件要求

  • CPU:x86_64架构,支持SSE4.2指令集
  • 内存:最小512MB,推荐2GB以上
  • 磁盘:至少1GB可用空间
  • 网络:稳定的网络连接

2. 安装方式

2.1 包管理器安装(推荐)

Ubuntu/Debian

# 添加OpenResty仓库
wget -qO - 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 openresty

CentOS/RHEL

# 添加OpenResty仓库
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# 安装OpenResty
sudo yum install openresty

macOS (Homebrew)

# 添加OpenResty tap
brew tap openresty/brew

# 安装OpenResty
brew install openresty/brew/openresty

2.2 源码编译安装

下载源码

# 下载OpenResty源码
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
tar -xzf openresty-1.21.4.1.tar.gz
cd openresty-1.21.4.1

安装依赖

# Ubuntu/Debian
sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl

# CentOS/RHEL
sudo yum install pcre-devel openssl-devel gcc curl

编译配置

# 配置编译选项
./configure \
    --prefix=/usr/local/openresty \
    --with-luajit \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-pcre-jit \
    --with-file-aio \
    --with-http_secure_link_module

编译安装

# 编译
make -j$(nproc)

# 安装
sudo make install

2.3 Docker安装

官方镜像

# 拉取官方镜像
docker pull openresty/openresty:latest

# 运行容器
docker run -d --name openresty \
    -p 80:80 \
    -v /path/to/conf:/usr/local/openresty/nginx/conf \
    -v /path/to/logs:/usr/local/openresty/nginx/logs \
    openresty/openresty:latest

自定义Dockerfile

FROM openresty/openresty:latest

# 复制配置文件
COPY nginx.conf /usr/local/openresty/nginx/conf/
COPY lua/ /usr/local/openresty/lualib/

# 暴露端口
EXPOSE 80 443

# 启动命令
CMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]

3. 目录结构

3.1 默认安装目录

/usr/local/openresty/
├── bin/                    # 可执行文件
│   ├── openresty          # OpenResty启动脚本
│   ├── resty              # Lua脚本执行器
│   └── restydoc           # 文档查看工具
├── luajit/                # LuaJIT相关文件
├── lualib/                # Lua库文件
│   ├── resty/             # OpenResty Lua库
│   └── ngx/               # Nginx Lua模块
├── nginx/                 # Nginx相关文件
│   ├── conf/              # 配置文件目录
│   ├── html/              # 默认网页目录
│   ├── logs/              # 日志文件目录
│   └── sbin/              # Nginx可执行文件
└── pod/                   # 文档文件

3.2 配置文件结构

nginx/conf/
├── nginx.conf             # 主配置文件
├── mime.types             # MIME类型配置
├── fastcgi_params         # FastCGI参数
├── scgi_params            # SCGI参数
├── uwsgi_params           # uWSGI参数
└── conf.d/                # 额外配置目录
    └── default.conf       # 默认虚拟主机配置

4. 基础配置

4.1 主配置文件(nginx.conf)

# 工作进程数
worker_processes auto;

# 错误日志
error_log logs/error.log;

# 事件模块配置
events {
    worker_connections 1024;
    use epoll;
}

# HTTP模块配置
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    # 日志格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 访问日志
    access_log logs/access.log main;
    
    # 基础配置
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    
    # Lua包路径
    lua_package_path "/usr/local/openresty/lualib/?.lua;;";
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
    
    # 虚拟主机配置
    server {
        listen 80;
        server_name localhost;
        
        location / {
            root html;
            index index.html index.htm;
        }
        
        # Lua测试接口
        location /lua {
            content_by_lua_block {
                ngx.say("Hello, OpenResty!")
            }
        }
        
        # 错误页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

4.2 环境变量配置

# 添加到 ~/.bashrc 或 ~/.zshrc
export PATH=/usr/local/openresty/bin:$PATH
export PATH=/usr/local/openresty/nginx/sbin:$PATH

# 重新加载配置
source ~/.bashrc

5. 服务管理

5.1 Systemd服务配置

# /etc/systemd/system/openresty.service
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/bin/openresty -t
ExecStart=/usr/local/openresty/bin/openresty
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

5.2 服务操作命令

# 启用服务
sudo systemctl enable openresty

# 启动服务
sudo systemctl start openresty

# 停止服务
sudo systemctl stop openresty

# 重启服务
sudo systemctl restart openresty

# 重新加载配置
sudo systemctl reload openresty

# 查看服务状态
sudo systemctl status openresty

5.3 手动管理

# 启动OpenResty
sudo /usr/local/openresty/bin/openresty

# 停止OpenResty
sudo /usr/local/openresty/bin/openresty -s stop

# 重新加载配置
sudo /usr/local/openresty/bin/openresty -s reload

# 测试配置文件
sudo /usr/local/openresty/bin/openresty -t

6. 验证安装

6.1 版本检查

# 查看OpenResty版本
openresty -v

# 查看详细版本信息
openresty -V

# 查看LuaJIT版本
resty -e "print(jit.version)"

6.2 功能测试

# 测试基本HTTP服务
curl http://localhost/

# 测试Lua功能
curl http://localhost/lua

# 测试配置文件语法
openresty -t

6.3 性能测试

# 使用ab进行压力测试
ab -n 1000 -c 10 http://localhost/

# 使用wrk进行性能测试
wrk -t12 -c400 -d30s http://localhost/

7. 常见问题

7.1 权限问题

# 确保nginx用户有正确权限
sudo chown -R nginx:nginx /usr/local/openresty/nginx/logs/
sudo chmod 755 /usr/local/openresty/nginx/logs/

7.2 端口占用

# 检查端口占用
sudo netstat -tlnp | grep :80

# 或使用ss命令
sudo ss -tlnp | grep :80

7.3 防火墙配置

# Ubuntu/Debian (ufw)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

8. 性能优化

8.1 系统级优化

# 调整文件描述符限制
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf

# 调整内核参数
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 65535" >> /etc/sysctl.conf
sysctl -p

8.2 OpenResty配置优化

# 工作进程优化
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;

# 事件模块优化
events {
    worker_connections 65535;
    use epoll;
    multi_accept on;
}

# HTTP模块优化
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 100;
    
    # 缓冲区优化
    client_body_buffer_size 128k;
    client_max_body_size 10m;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
}

9. 总结

通过本章的学习,我们了解了OpenResty的多种安装方式、基础配置和服务管理。正确的安装和配置是后续学习和使用OpenResty的基础,建议在实际环境中多加练习,熟悉各种配置选项和管理命令。

10. 参考资料