跳至主要內容

openrest的安装

知识库集成配置NginxNginx大约 2 分钟

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。

用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

在线安装

# 添加yum源
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
# 安装openresty
yum install -y openresty
# 安装命令行工具 resty
yum install -y openresty-resty
# 查看openresty 仓库里头的软件包
yum --disablerepo="*" --enablerepo="openresty" list available
# 默认安装在  /usr/local/openresty
# 启动
systemctl status|start|stop openresty

源码安装

# 安装gcc和一些工具
yum install -y gcc gcc-c++ curl wget perl bzip2 pcre-devel openssl-devel zlib-devel
# 下载地址:https://openresty.org/cn/download.html
wget https://openresty.org/download/openresty-1.19.3.2.tar.gz
# 解压
tar -xzvf openresty-1.19.3.2.tar.gz
cd openresty-1.19.3.2/
# 查看配置选项
./configure --help
# 配置  --with-Components 激活组件,–-without 则是禁止组件
./configure \
--prefix=/usr/local/openresty \
--with-stream \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--without-http_redis2_module \
--user=root \
--group=root \
--build="LiveOps build at `date +%Y-%m-%d`" \
--with-ld-opt="-Ijemalloc"
# 编译安装
gmake && gmake install
# 查看安装目录
ll /usr/local/openresty
# 设置全局环境变量
echo 'export PATH=/usr/local/openresty/bin:$PATH' >> /etc/profile
source /etc/profile
# 查看版本号
openresty -v
# 和`nginx -t`是一样的,只是为了区别与nginx和openresty
openresty -t

修改配置文件

cat >> /usr/local/openresty/nginx/conf/nginx.conf <<EOP
#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;

    include /usr/local/openresty/nginx/conf/conf.d/*.conf;
}
EOP
mkdir -p /usr/local/openresty/nginx/conf/conf.d/ && cat >> /usr/local/openresty/nginx/conf/conf.d/80.conf << EOF
server {
    listen       80;
    server_name  localhost;
    charset utf-8;

    location / {
        root   html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location /status {
        stub_status on;
        access_log off;
    }
    location /lua {
       default_type 'text/html';
        content_by_lua_block {
            ngx.say("HelloWorld")
        }
   }
}
EOF

配置系统服务

# 注意 PIDFile的文件路径要和nginx.conf里面的pid路径保持一直
cat >> /usr/lib/systemd/system/openresty.service << EOF
[Unit]
Description=A dynamic web platform based on Nginx and LuaJIT.
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/bin/openresty -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;'
ExecReload=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;' -s reload
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable openresty && systemctl start openresty

::: cardList

- name: OpenResty 最佳实践
  desc: GitBook
  avatar: https://app.gitbook.com/public/images/logos/rounded/256x256.png?v=10.3.0-13f908807e9cb0c7824c55db359369ce6f2f2476-1611203601
  link: https://moonbingbing.gitbooks.io/openresty-best-practices/content/
  bgColor: "#CBEAFA"
  textColor: "#6854A1"

:::