1、OpenResty介绍:OpenResty 并不是一个全新的 Web 服务器,而是基于 Nginx,它利用了 Nginx 模块化、可扩展的特性,开发了一系列的增强模块,并把它们打包整合,形成了一个"一站式”的 Web 开发平台"。
虽然 OpenResty 的核心是 Nginx,但又不同于Nginx,关键就在于其中的 ngx_lua 模块,把 Lua 语言嵌入到了 Nginx,可以用脚本的方式操作 Nginx 内部的进程、多路复用、阶段式处理等各种构件。
2、OpenResty安装:
1>、安装依赖包:
[root@node1 ~]# yum install readline-devel pcre-devel openssl-devel -y
2>、添加OpenResty仓库:
[root@node1 ~]# yum install yum-utils -y
[root@node1 ~]# yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
3>、安装OpenResty:
[root@node1 ~]# yum install openresty -y
##安装openresty命令工具包
[root@node1 ~]# yum install openresty-resty -y
以上操作默认将OpenResty安装在" /usr/local/openresty "。
3、配置OpenResty:
1>、创建nginx组和用户:
[root@node1 ~]# groupadd nginx
[root@node1 ~]# useradd -g nginx -s /sbin/nologin -M nginx
2>、修改nginx配置文件:
[root@node1 ~]# vim /usr/local/openresty/conf/nginx.conf
#user nobody;
worker_processes 1;
##规定访问失败的日志路径
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
##规定访问成功的日志路径
access_log logs/host.access.log main;
##将访问成功的日志输送到指定主机的指定端口,并打上"access_log"标记
#access_log 127.0.0.1:511,tag=access_log;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
4、启动OpenResty服务:
####将openresty设置临时成环境变量
[root@node1 ~]# export PATH=$PATH:/usr/local/openresty/bin/openresty
##将openresty设置永久成环境变量
[root@node1 ~]# echo "export PATH=$PATH:/usr/local/openresty/bin/openresty" >> /etc/profile
[root@node1 ~]# source /etc/profile ##加载文件,使其立即生效
##检查openresty配置语法是否正确
[root@node1 ~]# openresty -t
##重启openresty服务
[root@node1 ~]# openresty -s reload
##检查openresty nginx配置文件语法是否正确
[root@node1 ~]# /usr/local/openresty/nginx/sbin/nginx -t
##重启openresty nginx服务,单独启动nginx
[root@node1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
##也可以通过systemctl来进行操作
[root@node1 ~]# systemctl start openresty.service
[root@node1 ~]# systemctl status openresty.service
5、确认服务是否启动:
浏览器访问机器80端口:
监控日志文件是否有日志保存: