OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
官网:https://openresty.org/cn/
官方安装文档:https://openresty.org/cn/installation.html
Linux安装流程如下:
(1)查看操作系统的版本。
cat /etc/os-release
返回结果如下,可以得出当前的CentOS或REAHAT版本是8.
NAME="TencentOS Server"
VERSION="3.2 (Final)"
ID="tencentos"
ID_LIKE="rhel fedora centos"
VERSION_ID="3.2"
PLATFORM_ID="platform:el8.2"
PRETTY_NAME="TencentOS Server 3.2 (Final)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:tencentos:tencentos:3"
HOME_URL="https://tlinux.qq.com/"
BUG_REPORT_URL="https://tlinux.qq.com/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"
根据操作系统版本获取相应的yum安装源。
#CentOS8及以下
wget https://openresty.org/package/centos/openresty.repo
# CentOS9及以上
#wget https://openresty.org/package/centos/openresty2.repo
sudo mv openresty.repo /etc/yum.repos.d/openresty.repo
(2)安装openresty 和相关工具(可选)。
yum check-update
yum install -y openresty
yum install -y openresty-resty
yum install -y openresty-doc
如果出现错误yum源找不到或者gpgcheck错误,则需要修改yum源的配置。
# 替换为对应的OS版本,$basearch会根据arch命令获取对应架构如x86_64
sed -i 's/$releasever/8/g' /etc/yum.repos.d/openresty.repo
# 如果不修改gpgcheck,则需要在每次yum安装命令添加--nogpgcheck参数
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/openresty.repo
添加openresty到系统路径,默认的可执行文件名为nginx,如果已经安装了nginx,可以把新安装的重命名为openresty-nginx。
cat >> /etc/profile<<EOF
export PATH=/usr/local/openresty/nginx/sbin:$PATH
EOF
source /etc/profile
mv /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/openresty-nginx
(3)创建简单项目验证是否安装成功,在启动项目后,访问对应的URL会输出hello, world
即可验证。
mkdir -p work/logs work/conf
touch work/conf/nginx.conf
cat > work/conf/nginx.conf<<EOF
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}
EOF
cd work
openresty-nginx -p `pwd`/ -c conf/nginx.conf
curl http://localhost:8080/
(4)如果需要压测,可以使用Apache ab进行压测。
yum install -y httpd-tools
ab -c10 -n50000 http://localhost:8080/
输出的压测效果如下:
完整的安装脚本 如下:
cat /etc/os-release
#CentOS8及以下
wget https://openresty.org/package/centos/openresty.repo
# CentOS9及以上
#wget https://openresty.org/package/centos/openresty2.repo
sudo mv openresty.repo /etc/yum.repos.d/openresty.repo
# 替换为对应的OS版本,$basearch会根据arch命令获取对应架构如x86_64
sed -i 's/$releasever/8/g' /etc/yum.repos.d/openresty.repo
# 如果不修改gpgcheck,则需要在每次yum安装命令添加--nogpgcheck参数
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/openresty.repo
yum check-update
yum install -y openresty
yum install -y openresty-resty
yum install -y openresty-doc
cat >> /etc/profile<<EOF
export PATH=/usr/local/openresty/nginx/sbin:$PATH
EOF
source /etc/profile
mv /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/openresty-nginx
mkdir -p work/logs work/conf
touch work/conf/nginx.conf
cat > work/conf/nginx.conf<<EOF
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}
EOF
cd work
openresty-nginx -p `pwd`/ -c conf/nginx.conf
curl http://localhost:8080/
yum install -y httpd-tools
ab -c10 -n50000 http://localhost:8080/
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。