环境:CentOS 7系统
1、安装
方式1:下载预编译安装包
# 下载地址:https://openresty.org/cn/linux-packages.html
# https://openresty.org/cn/linux-packages.html#centos-8-或者更老版本
# 1、添加yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/openresty.repo
# 2、更新yum index:
sudo yum check-update
# 3、安装openresty
sudo yum install -y openresty
# 4、查看帮助
# 默认安装路径:/usr/local/openresty/nginx/
openresty -h
# 5、安装其他命令行工具
# 安装命令行工具 resty
# sudo yum install -y openresty-resty
# 移除命令行工具 resty
# sudo yum remove -y openresty-resty
使用openresty -V查看配置参数:
--prefix=/usr/local/openresty/nginx
--with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre2/include -I/usr/local/openresty/openssl3/include'
--add-module=../ngx_devel_kit-0.3.3
--add-module=../echo-nginx-module-0.63
--add-module=../xss-nginx-module-0.06
--add-module=../ngx_coolkit-0.2
--add-module=../set-misc-nginx-module-0.33
--add-module=../form-input-nginx-module-0.12
--add-module=../encrypted-session-nginx-module-0.09
--add-module=../srcache-nginx-module-0.33
--add-module=../ngx_lua-0.10.27
--add-module=../ngx_lua_upstream-0.07
--add-module=../headers-more-nginx-module-0.37
--add-module=../array-var-nginx-module-0.06
--add-module=../memc-nginx-module-0.20
--add-module=../redis2-nginx-module-0.15
--add-module=../redis-nginx-module-0.3.9
--add-module=../ngx_stream_lua-0.0.15
--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre2/lib -L/usr/local/openresty/openssl3/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre2/lib:/usr/local/openresty/openssl3/lib'
--with-cc='ccache gcc -fdiagnostics-color=always'
--with-pcre-jit
--with-stream
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-http_v2_module
--with-http_v3_module
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
--with-http_stub_status_module
--with-http_realip_module
--with-http_addition_module
--with-http_auth_request_module
--with-http_secure_link_module
--with-http_random_index_module
--with-http_gzip_static_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_slice_module
--with-threads
--with-compat
--with-stream
--with-http_ssl_module
方式2:源码编译安装
1)安装依赖
yum install pcre-devel openssl-devel gcc curl zlib-devel
如果OpenSSL版本太低,不支持QUIC,请重新安装,否则跳过重新安装OpenSSL操作。
重新安装OpenSSL:
# 1、移除openssl
yum remove -y openssl openssl-devel
# 2、安装依赖
sudo yum install gcc make perl
# 3、下载
# 下载地址:https://openssl-library.org/source/index.html
wget https://github.com/openssl/openssl/releases/download/openssl-3.0.15/openssl-3.0.15.tar.gz
# 4、解压
tar -xf openssl-3.0.15.tar.gz
# 5、切换目录
cd openssl-3.0.15/
# 6、 配置
./Configure --prefix=/usr/local/openssl
# 7、编译
make
# 8、安装
make install
# 9、创建软链接
sudo ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/lib64/libcrypto.so.3 /usr/lib64/libcrypto.so.3
ln -s /usr/local/openssl/lib64/libssl.so.3 /usr/lib64/libssl.so.3
# 10、更新动态链接库缓存
sudo ldconfig
# 11、查看版本
openssl version
如果在执行./Configure命令过程中出现报错,报错信息:Can't locate IPC/Cmd.pm in @INC
# 报错信息:Can't locate IPC/Cmd.pm in @INC
# 执行命令
sudo yum install perl-IPC-Cmd
2)安装OpenResty
# 安装说明:https://openresty.org/cn/installation.html
# 1、下载
# 下载地址:https://openresty.org/cn/download.html
wget https://openresty.org/download/openresty-1.27.1.1.tar.gz
# 2、解压
tar -xf openresty-1.27.1.1.tar.gz
# 3、切换目录
cd openresty-1.27.1.1/
# 4、配置,默认--prefix=/usr/local/openresty
./configure
# 配置执行完成后,显示内容:
# ------------------------------------------------
#Type the following commands to build and install:
# gmake
# gmake install
# ------------------------------------------------
# 根据提示,执行gmake、gmake install命令
# 或者也可以执行make、make install命令
# 5、编译
make
# 6、安装
make install
# 7、切换到/usr/local/openresty目录
cd /usr/local/openresty
# 8、查看版本
./bin/openresty -v
# 9、创建软链接
ln -s /usr/local/openresty/bin/openresty /usr/bin/openresty
我们也可以为./configure命令指定参数,为了保持和预编译的参数一致,如下所示:
# 说明:
# 因为使用yum安装OpenSSL版本太低,不支持QUIC。
# 在重新安装OpenSSL后,我们需要指定参数--with-cc-opt、--with-ld-opt。
./configure --prefix=/usr/local/openresty \
--with-cc-opt="-I/usr/local/openssl/include" \
--with-ld-opt="-L/usr/local/openssl/lib64" \
--with-pcre-jit \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_v2_module \
--with-http_v3_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_random_index_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_slice_module \
--with-threads \
--with-compat \
--with-stream \
--with-http_ssl_module
2、启动
# 1、执行命令
openresty
# 2、查看进程
ps aux | grep openresty
3、浏览器访问
4、测试
编辑/usr/local/openresty/nginx/conf/nginx.conf文件:
# 详见:https://openresty.org/cn/getting-started.html#prepare-the-nginxconf-config-file
location /lua {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
location /lua-file {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/conf/lua/test.lua;
}
编辑/usr/local/openresty/nginx/conf/lua/test.lua文件:
ngx.say("<p>hello, world</p>")
检查配置:
openresty -t
重启openresty:
openresty -s reload
使用curl访问:
# 1、访问localhost/lua:
curl localhost/lua
# 执行结果:
<p>hello, world</p>
# 2、访问localhost/lua-file:
curl localhost/lua-file
# 执行结果:
<p>hello, world</p>