CentOS安装OpenResty(Nginx+Lua)开发环境

小灰灰 2023-02-17 06:20 50阅读 0赞

一.简介

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。来自OpenResty®官网

总结和拓展:

  • OpenResty 是 Nginx 与 Lua 的结合;
  • OpenResty 是多进程模式,会有一个 master 进程和多个 worker 进程。Master 进程管理 worker 进程,向各 worker 进程发送信号,监控 work 进程状态;
  • OpenResty 是异步非阻塞 ;怎样理解阻塞非阻塞与同步异步的区别?知乎
  • 子查询:OpenResty 中有三种方式发起子请求:capture、exec、redirect;
  • OpenResty 缓存机制。

Nginx+Lua架构思维导图:

回到顶部

二.关闭SELinux


先临时关闭,然后永久关闭

  1. setenforce 0 # 临时关闭
  2. sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config # 永久关闭

回到顶部

三.防火墙开启80端口


先临时开启80端口,然后再永久开启

  1. iptables -I INPUT -p tcp --dport 80 -j ACCEPT # 临时生效
  2. /etc/init.d/iptables save # 保存到配置文件,永久生效
  3. /etc/init.d/iptables status # 查看iptables当前状态

回到顶部

四.yum安装


对于一些常见的 Linux 发行版本,OpenResty® 提供 官方预编译包。确保你首先用这种方式来安装。

你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令)。运行下面的命令就可以添加我们的仓库:

  1. yum install yum-utils -y
  2. yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

然后就可以像下面这样安装软件包,比如 openresty:

  1. yum install openresty -y

如果你想安装命令行工具 resty,那么可以像下面这样安装 openresty-resty 包:

  1. yum install openresty-resty -y

命令行工具 opm 在 openresty-opm 包里,而 restydoc 工具在 openresty-doc 包里头。

列出所有 openresty 仓库里头的软件包:

  1. yum --disablerepo="*" --enablerepo="openresty" list available

参考 OpenResty RPM 包页面获取这些包更多的细节。

回到顶部

五.源码包编译安装


下载

从下载页 Download下载最新的 OpenResty® 源码包,并且像下面的示例一样将其解压:

  1. wget https://openresty.org/download/openresty-1.13.6.2.tar.gz

安装前的准备

必须将这些库 perl 5.6.1+, libpcre, libssl安装在您的电脑之中。 对于 Linux来说, 您需要确认使用 ldconfig 命令,让其在您的系统环境路径中能找到它们。

推荐您使用yum安装以下的开发库:

  1. yum install pcre-devel openssl-devel gcc curl -y

安装

  1. tar -zxvf openresty-1.13.6.2.tar.gz
  2. cd openresty-1.13.6.2
  3. ./configure
  4. make
  5. make install

如果您的电脑支持多核 make 工作的特性, 您可以这样编译:

  1. make -j2

默认, —prefix=/usr/local/openresty 程序会被安装到/usr/local/openresty目录。您可以指定各种选项,比如:

  1. ./configure --prefix=/opt/openresty \
  2. --with-luajit \
  3. --without-http_redis2_module \
  4. --with-http_iconv_module \
  5. --with-http_postgres_module

试着使用 ./configure —help 查看更多的选项。

回到顶部

六.配置


第一种常规配置方案

修改nginx.conf配置文件

  1. cd /usr/local/openresty/nginx/conf
  2. mv nginx.conf nginx.conf.$(date +%Y%m%d)
  3. vim nginx.conf
  4. worker_processes 1;
  5. error_log logs/error.log;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. server {
  11. listen 8080;
  12. location / {
  13. default_type text/html;
  14. content_by_lua '
  15. ngx.say("<p>hello, world</p>")
  16. ';
  17. }
  18. }
  19. }

添加环境变量

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

然后启动openresty,启动命令和nginx一致。

  1. nginx -c /usr/local/openresty/nginx/conf/nginx.conf

启动后查看一下服务

  1. ps -ef | grep nginx

访问 Web 服务

  1. curl http://localhost:8080/

如果一切正常,我们应该得到输出

  1. <p>hello, world</p>

重启 Web 服务

  1. nginx -s reload

第二种lua配置方案

添加lua.conf配置文件

  1. server {
  2. listen 8080;
  3. location / {
  4. default_type text/html;
  5. content_by_lua '
  6. ngx.say("<p>hello, world Lua!</p>")
  7. ';
  8. }
  9. }

修改nginx.conf配置文件

  1. cd /usr/local/openresty/nginx/conf
  2. mv nginx.conf nginx.conf.$(date +%Y%m%d)
  3. vim nginx.conf
  4. worker_processes 1;
  5. error_log logs/error.log;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
  11. lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua模块
  12. lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模块
  13. include lua.conf; #lua.conf和nginx.conf在同一目录下
  14. }

添加环境变量

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

然后启动openresty,启动命令和nginx一致。

  1. nginx -c /usr/local/openresty/nginx/conf/nginx.conf

#启动后查看一下服务

  1. ps -ef | grep nginx

访问 Web 服务

  1. curl http://localhost:8080/

如果一切正常,我们应该得到输出

  1. <p>hello, world Lua!</p>

配置lua代码文件

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

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

  1. mkdir /usr/local/openresty/nginx/conf/lua

创建test.lua文件

  1. cd /usr/local/openresty/nginx/conf/lua
  2. vim test.lua
  3. ngx.say("test lua");

修改conf/lua.conf文件

  1. vim /usr/local/openresty/nginx/conf/lua.conf
  2. server {
  3. listen 8080;
  4. location / {
  5. default_type text/html;
  6. lua_code_cache off; #关闭lua代码缓存,调试时关闭,正式环境开启
  7. content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
  8. }
  9. }

关闭缓存后会看到如下报警(忽略不管)

  1. nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:5

重启 Web 服务

  1. nginx -s reload

回到顶部

七.测试性能


安装压力测试工具ab

  1. yum -y install httpd-tools

压力测试

  • -c:每次并发数为10个
  • -n:共发送50000个请求

    ab -c10 -n50000 http://localhost:8080/

测试报详解

  1. Server Software: Apache #服务器软件
  2. Server Hostname: localhost #域名
  3. Server Port: 80 #请求端口号
  4. Document Path: / #文件路径
  5. Document Length: 40888 bytes #页面字节数
  6. Concurrency Level: 10 #请求的并发数
  7. Time taken for tests: 27.300 seconds #总访问时间
  8. Complete requests: 1000 #请求成功数量
  9. Failed requests: 0 #请求失败数量
  10. Write errors: 0
  11. Total transferred: 41054242 bytes #请求总数据大小(包括header头信息)
  12. HTML transferred: 40888000 bytes #html页面实际总字节数
  13. Requests per second: 36.63 [#/sec] (mean) #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
  14. Time per request: 272.998 [ms] (mean) #用户平均请求等待时间
  15. Time per request: 27.300 [ms] (mean, across all concurrent requests)
  16. # 服务器平均处理时间,也就是服务器吞吐量的倒数
  17. Transfer rate: 1468.58 [Kbytes/sec] received #每秒获取的数据长度
  18. Connection Times (ms)
  19. min mean[+/-sd] median max
  20. Connect: 43 47 2.4 47 53
  21. Processing: 189 224 40.7 215 895
  22. Waiting: 102 128 38.6 118 794
  23. Total: 233 270 41.3 263 945
  24. Percentage of the requests served within a certain time (ms)
  25. 50% 263 #50%用户请求在263ms内返回
  26. 66% 271 #66%用户请求在271ms内返回
  27. 75% 279 #75%用户请求在279ms内返回
  28. 80% 285 #80%用户请求在285ms内返回
  29. 90% 303 #90%用户请求在303ms内返回
  30. 95% 320 #95%用户请求在320ms内返回
  31. 98% 341 #98%用户请求在341ms内返回
  32. 99% 373 #99%用户请求在373ms内返回
  33. 100% 945 (longest request)

回到顶部

八.启动脚本


添加启动脚本

  1. vim /etc/init.d/openresty

脚本内容如下

  1. #!/bin/sh
  2. #
  3. # openresty - this script starts and stops the openresty daemin
  4. #
  5. # chkconfig: - 85 15
  6. # description: OpenResty is a full-fledged web platform that integrates \
  7. # the standard Nginx core, LuaJIT, many carefully written Lua libraries, \
  8. # lots of high quality 3rd-party Nginx modules, and most of their external dependencies.
  9. # processname: openresty
  10. # config: /usr/local/openresty/nginx/conf/nginx.conf
  11. # pidfile: /usr/local/openresty/nginx/logs/nginx.pid
  12. # Url http://www.cnblogs.com/wushuaishuai/
  13. # Last Updated 2018.07.15
  14. # Source function library.
  15. . /etc/rc.d/init.d/functions
  16. # Source networking configuration.
  17. . /etc/sysconfig/network
  18. # Check that networking is up.
  19. [ "$NETWORKING" = "no" ] && exit 0
  20. nginx="/usr/local/openresty/nginx/sbin/nginx"
  21. prog=$(basename $nginx)
  22. NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"
  23. NGINX_PID="/usr/local/openresty/nginx/logs/nginx.pid"
  24. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  25. lockfile=/var/lock/subsys/nginx
  26. start() {
  27. [ -x $nginx ] || exit 5
  28. [ -f $NGINX_CONF_FILE ] || exit 6
  29. echo -n $"Starting $prog: "
  30. daemon $nginx -c $NGINX_CONF_FILE
  31. retval=$?
  32. echo
  33. #service php-fpm start
  34. [ $retval -eq 0 ] && touch $lockfile
  35. return $retval
  36. }
  37. stop() {
  38. echo -n $"Stopping $prog: "
  39. $nginx -s stop
  40. echo_success
  41. retval=$?
  42. echo
  43. #service php-fpm stop
  44. [ $retval -eq 0 ] && rm -f $lockfile
  45. return $retval
  46. }
  47. restart() {
  48. stop
  49. start
  50. }
  51. reload() {
  52. configtest || return $?
  53. echo -n $"Reloading $prog: "
  54. $nginx -s reload
  55. RETVAL=$?
  56. echo
  57. }
  58. force_reload() {
  59. restart
  60. }
  61. configtest() {
  62. $nginx -t -c $NGINX_CONF_FILE
  63. }
  64. rh_status() {
  65. status $prog
  66. }
  67. rh_status_q() {
  68. rh_status >/dev/null 2>&1
  69. }
  70. case "$1" in
  71. start)
  72. rh_status_q && exit 0
  73. $1
  74. ;;
  75. stop)
  76. rh_status_q || exit 0
  77. $1
  78. ;;
  79. restart|configtest)
  80. $1
  81. ;;
  82. reload)
  83. rh_status_q || exit 7
  84. $1
  85. ;;
  86. force-reload)
  87. force_reload
  88. ;;
  89. status)
  90. rh_status
  91. ;;
  92. condrestart|try-restart)
  93. rh_status_q || exit 0
  94. ;;
  95. *)
  96. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  97. exit 2
  98. esac

赋予执行权限

  1. chmod u+x /etc/init.d/openresty

回到顶部

九.使用systemctl来管理openresty

  1. #查看openresty状态
  2. systemctl status openresty.service
  3. #启动openresty
  4. systemctl start openresty.service
  5. #设置openresty开机自启动
  6. systemctl enable openresty.service
  7. #重启openresty
  8. systemctl restart openresty.service
  9. #停止openresty
  10. systemctl stop openresty.service
  11. #取消openresty开机自启动
  12. systemctl disable openresty.service

回到顶部

十.参考资料


跟我学OpenResty(Nginx+Lua)开发目录贴

发表评论

表情:
评论列表 (有 0 条评论,50人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Centos 安装桌面环境

    写在前面 Centos 作为企业服务器的操作系统是很常见的,为了保持纯净的系统环境,通常情况下,不需要安装图形桌面,最小化地安装 Centos(minimal 版本) 就可以