千家信息网

OpenResty--------企业级入门实践篇

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,前景前篇介绍了OpenResty核心概念,优势与架构等信息,进行本篇之前建议至少观看一遍。OpenResty--------企业级理论实践篇本篇将进行OpenResty的安装与编写第一个应用过程。注:
千家信息网最后更新 2025年02月03日OpenResty--------企业级入门实践篇

前景

前篇介绍了OpenResty核心概念,优势与架构等信息,进行本篇之前建议至少观看一遍。OpenResty--------企业级理论实践篇
本篇将进行OpenResty的安装与编写第一个应用过程。
注:本篇采用CentOS 7(7.4)系统

环境准备

系统信息

[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)[root@localhost ~]# uname -r3.10.0-693.el7.x86_64

网络设备信息

[root@localhost ~]# ifconfigeth0: flags=4163  mtu 1500        inet 192.168.0.254  netmask 255.255.255.0  broadcast 192.168.0.255        inet6 fe80::fb69:bc45:9e19:5b06  prefixlen 64  scopeid 0x20        ether 00:0c:29:be:2d:43  txqueuelen 1000  (Ethernet)        RX packets 3197  bytes 793326 (774.7 KiB)        RX errors 0  dropped 2  overruns 0  frame 0        TX packets 330  bytes 59828 (58.4 KiB)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73  mtu 65536        inet 127.0.0.1  netmask 255.0.0.0        inet6 ::1  prefixlen 128  scopeid 0x10        loop  txqueuelen 1  (Local Loopback)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

关闭selinux

[root@localhost ~]# setenforce 0

清空防火墙规则

[root@localhost ~]# iptables -F

依赖准备

[root@localhost ~]# yum install readline-devel -y[root@localhost ~]# yum install pcre-devel -y[root@localhost ~]# yum install openssl-devel -y[root@localhost ~]# yum install perl -y

安装过程

下载安装包[从github中]

[root@localhost ~]# wget https://github.com/openresty/openresty/releases/download/v1.13.6.2/openresty-1.13.6.2.tar.gz--2019-08-01 02:35:30--  https://github.com/openresty/openresty/releases/download/v1.13.6.2/openresty-1.13.6.2.tar.gzResolving github.com (github.com)... 13.229.188.59Connecting to github.com (github.com)|13.229.188.59|:443... connected.HTTP request sent, awaiting response... 302 FoundLocation: https://github-production-release-asset-2e65be.s3.amazonaws.com/480080/5565c880-1ef7-11e9-9fb6-06daa71b014c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190731T103531Z&X-Amz-Expires=300&X-Amz-Signature=cc703e1769ba14abd394befcd43e7fb26be92ba53e592da53052931ae89f6bf8&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dopenresty-1.13.6.2.tar.gz&response-content-type=application%2Foctet-stream [following]--2019-08-01 02:35:31--  https://github-production-release-asset-2e65be.s3.amazonaws.com/480080/5565c880-1ef7-11e9-9fb6-06daa71b014c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190731T103531Z&X-Amz-Expires=300&X-Amz-Signature=cc703e1769ba14abd394befcd43e7fb26be92ba53e592da53052931ae89f6bf8&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dopenresty-1.13.6.2.tar.gz&response-content-type=application%2Foctet-streamResolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.36.4Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.36.4|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 4635916 (4.4M) [application/octet-stream]Saving to: 'openresty-1.13.6.2.tar.gz'100%[==========================================================================>] 4,635,916   2.28MB/s   in 1.9s   2019-08-01 02:35:34 (2.28 MB/s) - 'openresty-1.13.6.2.tar.gz' saved [4635916/4635916]

解压包

[root@localhost ~]# tar xf openresty-1.13.6.2.tar.gz [root@localhost ~]# ls -l openresty-1.13.6.2total 96drwxrwxr-x 43 1000 1000  4096 May 15  2018 bundle-rwxrwxr-x  1 1000 1000 48140 May 15  2018 configure-rw-rw-r--  1 1000 1000 22924 May 15  2018 COPYRIGHTdrwxrwxr-x  2 1000 1000   156 May 15  2018 patches-rw-rw-r--  1 1000 1000  4689 May 15  2018 README.markdown-rw-rw-r--  1 1000 1000  8972 May 15  2018 README-windows.txtdrwxrwxr-x  2 1000 1000    52 May 15  2018 util

进入安装包目录

[root@localhost ~]# cd openresty-1.13.6.2

生成编译与安装文件

  • --prefix=/usr/local/openresty-1.13.6.2 指定安装目录为/usr/local/openresty-1.13.6.2, 默认为/opt/openresty
  • --with-luajit 开启luajit组件
  • --without-http_redis2_module 禁用 http_redis2_module组件
  • --with-http_iconv_module 开启 http_iconv_module组件
[root@localhost openresty-1.13.6.2]# ./configure \  --prefix=/usr/local/openresty-1.13.6.2 \  --with-luajit \ --without-http_redis2_module \ --with-http_iconv_module

编译

[root@localhost openresty-1.13.6.2]# gmake

安装

[root@localhost openresty-1.13.6.2]# gmake install

创建软链接

[root@localhost openresty-1.13.6.2]# ln -s /usr/local/openresty-1.13.6.2 /usr/local/openresty

服务启动测试

自带默认配置文件,默认服务监听80端口

[root@localhost openresty-1.13.6.2]# cat /usr/local/openresty/nginx/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;        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;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

使用默认的配置文件启动

[root@localhost openresty-1.13.6.2]# /usr/local/openresty/bin/openresty -p /usr/local/openresty/nginx/

通过浏览器访问测试[测试虚拟机IP为:192.168.0.254]

访问成功,OpenResty已经成功返回页面。

编写程序

切换至用户家目录

[root@localhost openresty-1.13.6.2]# cd ~

创建工作目录

[root@localhost ~]# mkdir -vp  openresty-test01/{logs,conf}mkdir: created directory 'openresty-test01'mkdir: created directory 'openresty-test01/logs'mkdir: created directory 'openresty-test01/conf'[root@localhost ~]# tree openresty-test01/openresty-test01/├── conf└── logs2 directories, 0 files

编写配置文件

注意: 如果你安装的是 openresty 1.9.3.1 及以下版本,请使用 content_by_lua 命令代替示例中的 content_by_lua_block。可使用 nginx/openresty -V 命令查看版本号。

[root@localhost ~]# cat openresty-test01/conf/nginx.confworker_processes 1; # 设置worker数量error_log logs/error.log; # 指定错误日志文件路径events {    worker_connections 1024; # 单个worker进程最大允许同时建立外部连接的数量}http {    server {        listen 8888; # 设置监听端口, 注意系统其它服务是否已占用该端口        location / {            # lua 代码块            content_by_lua_block {                ngx.say("this is lisea no.1 openresty app")            }        }    }}

运行

[root@localhost ~]# /usr/local/openresty/bin/openresty -p openresty-test01

无报错就表示运行成功

通过命令测试

[root@localhost ~]# curl 127.0.0.1:8888this is lisea no.1 openresty app

访问成功,OpenResty已经成功返回内容。

通过浏览器测试[测试虚拟机IP为:192.168.0.254, 端口为配置文件中监听设置的:8888]

访问成功,OpenResty已经成功返回内容。

成功 文件 测试 目录 端口 配置 信息 命令 系统 组件 服务 监听 内容 数量 浏览器 版本 过程 准备 浏览 编译 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 银川网络技术服务需要办税务吗 软件开发费算无形资产吗 银联商务 软件开发 建党百年红色资源建立一个数据库 计算机网络安全主要指什么 互联网科技创新app 合肥光学系统设计软件开发公司 传趣网络技术有限公司怎么样 什么是统筹协调网络安全 曹戈展硕网络技术服务有限公司 到哪里学习网络安全知识 你的世界服务器下载 富士软件开发面试 iis服务器频繁断网 把管理系统放在云服务器 魔兽世界永久60服务器人口 澳门公司果蔬配送软件开发 数据库用哪个关键字查询数据 数据库小机 合伙人软件开发 上海东塔网络安全学院白盒测试 创建app软件开发 漳州世纪成达软件开发有限公司 云谷网网络安全证书有问题 幼儿园大班电视网络安全教案 JAVA数据库在线聊天系统 生物合成基因簇 数据库 计算机网络技术专业专升本 杨浦区上门软件开发有哪些 网络安全专业求职简历
0