openresty安装配置

时间:2021-07-22
本文章向大家介绍openresty安装配置,主要包括openresty安装配置使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

yum安装   OpenResty - OpenResty® Linux 包

1安装

wget https://openresty.org/package/centos/openresty.repo

yum check-update

安装依赖
yum install pcre-devel openssl-devel gcc curl -y
yum install -y openresty openresty-resty

2配置

2 配置
lua配置测试
2.1添加lua.conf配置文件
[root@docker-test conf]# pwd

/usr/local/openresty/nginx/conf

[root@docker-test conf]# cat lua.conf

server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world Lua!</p>")
';
}
}
2.2 修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf

mv nginx.conf nginx.conf.$(date +%Y%m%d)

worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua模块
lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模块
include lua.conf; #lua.conf和nginx.conf在同一目录下
}
说明:

加入lua模块和c模块的路径,在此版本中默认已经加载了lua模块和c模块所以这里可以省略上面的两条配置。

2.3 添加环境变量

echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile

2.4 启动openresty
nginx -c /usr/local/openresty/nginx/conf/nginx.conf

说明:

启动命令和nginx一致

启动后查看一下服务

ps -ef | grep nginx

2.5 重启nginx
nginx -s reload

2.6 访问 Web 服务
curl http://localhost:8080/

2.7配置systemctl的Nginx启动服务

[root@docker-test nginx]# cat /usr/lib/systemd/system/nginx.service

[Unit]
Description=Nginx server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/openresty/nginx/sbin/nginx -s quit
PrivateTmp=true


[Install]
WantedBy=multi-user.target

说明:

配置过systemctl之后可以用此来启动加载nginx,不需要再直接调用nginx命令来控制nginx。

配置lua代码文件

在conf文件夹下创建lua文件夹,专门用来存放lua文件

mkdir /usr/local/openresty/nginx/conf/lua
说明:

我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。

创建test.lua文件

cd /usr/local/openresty/nginx/conf/lua
vim test.lua
ngx.say("test lua");
修改conf/lua.conf文件

vim /usr/local/openresty/nginx/conf/lua.conf
server {
listen 8080;
location / {
default_type text/html;
lua_code_cache off; #关闭lua代码缓存,调试时关闭,正式环境开启
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
}
关闭缓存后会看到如下报警(忽略不管)

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:5
重新加载 Web 服务

systemctl reload nginx

原文地址:https://www.cnblogs.com/yanzi2020/p/15043523.html