基于 CentOS 快速搭建 OpenResty-云服务器玩法在线实验

[复制链接]
查看: 443|回复: 0
发表于 2020-4-15 22:03:07 | 显示全部楼层 |阅读模式
实验内容
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。本实验带您从零开始,学习编译安装、配置 OpenResty

免费在线实验地址:点击进入
实验资源:云服务器,没有云服务器的朋友推荐1折抢购:69元/年的阿里云服务器、或者99元/年的腾讯云服务器

软件环境:
CentOS 7.2 64 位

一、了解 OpenResty®
什么是 OpenResty®
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 等都进行一致的高性能响应。

二、更新系统
在安装前,我们先对 CVM 的系统进行更新,确保系统内的软件源和工具是最新版本。

执行命令从软件源进行更新
  1. sudo yum update -y
复制代码


三、安装 OpenResty®
添加官方仓库
  1. sudo yum install yum-utils
  2. sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
复制代码
安装二进制包
  1. sudo yum install openresty
复制代码
在安装过程中如果看到如下提示,请在确认签名最后8位是 [D5EDEB74] 的情况下,输入 y 并回车确认。
  1. Retrieving key from https://openresty.org/package/pubkey.gpg
  2. Importing GPG key 0xD5EDEB74:
  3. Userid     : "OpenResty Admin <admin@openresty.com>"
  4. Fingerprint: e522 18e7 0878 97dc 6dea 6d6d 97db 7443 d5ed eb74
  5. From       : https://openresty.org/package/pubkey.gpg
  6. Is this ok [y/N]: y
复制代码
不区分大小写

以默认配置文件启动
在安装了官方的二进制包之后,这个包自动帮我们生成了一份配置文件,可以让我们直接启动服务,并确认服务是否启动成功。

执行如下命令启动 openresty 服务

对于 CentOS 6.x 的用户:
  1. sudo service openresty start
复制代码
对于 CentOS 7.x 的用户:
  1. sudo systemctl start openresty
复制代码
确认服务启动成功
接下来就可以直接通过 IP 访问服务器上已经启动的 openresty 服务了。

打开这个链接:http://<您的 CVM IP 地址>/

如果看到 Welcome to OpenResty! 的欢迎页面,则说明服务启动成功!

设置开机自动启动
默认情况下,openresty 可能不会开机启动。我们需要告诉系统开机自动启动 openresty 服务。

执行如下命令设置开机自动启动

对于 CentOS 6.x 的用户:
  1. sudo chkconfig openresty on
复制代码
对于 CentOS 7.x 的用户:
  1. sudo systemctl enable openresty
复制代码

四、配置 OpenResty®
查看现有的配置文件
在安装、启动完成之后,我们就可以尝试对 openresty 的配置文件进行个性化修改了。

默认安装完成后,配置文件存放在 /usr/local/openresty/nginx/conf/,我们可以使用 vim nano 文本编辑器打开配置文件并进行编辑,或直接在实验室的 Web 页面进行编辑。

请选择你熟悉的编辑器
  1. nano /usr/local/openresty/nginx/conf/nginx.conf
  2. vim /usr/local/openresty/nginx/conf/nginx.conf
复制代码
查看 nginx.conf

编辑配置文件修改监听端口
现在我们来尝试修改一下默认配置文件。首先我们来对 Nginx 的监听端口做一个修改。我们将默认的 80 端口监听修改到 8080 端口。

编辑 nginx.conf

示例代码:/usr/local/openresty/nginx/conf/nginx.conf

  1. #user  nobody;
  2. worker_processes  1;

  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;

  6. #pid        logs/nginx.pid;


  7. events {
  8.     worker_connections  1024;
  9. }


  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;

  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';

  16.     #access_log  logs/access.log  main;

  17.     sendfile        on;
  18.     #tcp_nopush     on;

  19.     #keepalive_timeout  0;
  20.     keepalive_timeout  65;

  21.     #gzip  on;

  22.     server {
  23.         listen       8080;
  24.         server_name  localhost;

  25.         #charset koi8-r;

  26.         #access_log  logs/host.access.log  main;

  27.         location / {
  28.             root   html;
  29.             index  index.html index.htm;
  30.         }

  31.         #error_page  404              /404.html;

  32.         # redirect server error pages to the static page /50x.html
  33.         #
  34.         error_page   500 502 503 504  /50x.html;
  35.         location = /50x.html {
  36.             root   html;
  37.         }

  38.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39.         #
  40.         #location ~ \.php$ {
  41.         #    proxy_pass   http://127.0.0.1;
  42.         #}

  43.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44.         #
  45.         #location ~ \.php$ {
  46.         #    root           html;
  47.         #    fastcgi_pass   127.0.0.1:9000;
  48.         #    fastcgi_index  index.php;
  49.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  50.         #    include        fastcgi_params;
  51.         #}

  52.         # deny access to .htaccess files, if Apache's document root
  53.         # concurs with nginx's one
  54.         #
  55.         #location ~ /\.ht {
  56.         #    deny  all;
  57.         #}
  58.     }


  59.     # another virtual host using mix of IP-, name-, and port-based configuration
  60.     #
  61.     #server {
  62.     #    listen       8000;
  63.     #    listen       somename:8080;
  64.     #    server_name  somename  alias  another.alias;

  65.     #    location / {
  66.     #        root   html;
  67.     #        index  index.html index.htm;
  68.     #    }
  69.     #}


  70.     # HTTPS server
  71.     #
  72.     #server {
  73.     #    listen       443 ssl;
  74.     #    server_name  localhost;

  75.     #    ssl_certificate      cert.pem;
  76.     #    ssl_certificate_key  cert.key;

  77.     #    ssl_session_cache    shared:SSL:1m;
  78.     #    ssl_session_timeout  5m;

  79.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  80.     #    ssl_prefer_server_ciphers  on;

  81.     #    location / {
  82.     #        root   html;
  83.     #        index  index.html index.htm;
  84.     #    }
  85.     #}

  86. }
复制代码
检查配置文件有效性
在修改了配置文件之后,我们不要着急重新启动 openresty 服务,而是应该先对配置文件进行校验,确保配置文件不存在语法错误。

以下两个命令都可以对配置文件进行检查:
  1. sudo service openresty configtest
  2. /usr/local/openresty/nginx/sbin/nginx -t
复制代码
对于第一个命令,没有任何输出就是检查通过。对于第二个命令,显示如下内容为检查通过:
  1. nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
  2. nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
复制代码
如果配置文件存在语法问题,如缺少结尾分号,则会有错误信息进行提示:
  1. nginx: [emerg] directive "worker_processes" is not terminated by ";" in /usr/local/openresty/nginx/conf/nginx.conf:12
  2. nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test failed
复制代码
重新加载配置文件
确认配置文件不存在语法问题等错误后,就可以重新加载配置文件了。可以使用以下两个命令实现:
  1. sudo service openresty reload
  2. /usr/local/openresty/nginx/sbin/nginx -s reload
复制代码
重新加载成功时,第一个命令会输出类似信息(根据系统版本可能有所不同):
  1. Reloading openresty configuration: [  OK  ]
复制代码
第二个命令不会有任何输出。

如果加载失败,则会有对应的错误信息显示。

访问修改后的服务
接下来就可以直接通过修改后的端口来访问 openresty 服务了。

打开这个链接:http://<您的 CVM IP 地址>:8080/
腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )