吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 633|回复: 3
收起左侧

[学习记录] 第一个 OpenResty 程序 【OpenResty HelloWorld】

[复制链接]
yuluo829 发表于 2023-8-16 16:00

第一个 OpenResty 接口

本篇文章将说明如何使用 OpenResty 运行 helloWorld

安装

与其他开源软件一样,可以通过多种方式安装OpenResty。例如使用操作系统的包管理器、从源代码编译或docker镜像。不过,建议首先使用包管理器(例如yumapt-get、 和 `来安装 OpenResty。我将以 ubuntu 为例:

# 通过添加 GPG 公钥来安装一些所需的先决条件(稍后可以删除):
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates

# 导入我们的 GPG 密钥:
wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/openresty.gpg

# 添加我们的官方APT存储库。
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list > /dev/null

# 更新索引
sudo apt-get update

# 安装
sudo apt-get -y install openresty

# 卸载
apt --purge remove openresty

首先,将 OpenResty 存储库的 URL 添加到包管理器,然后使用包管理器安装 OpenResty。更详细的步骤可以参考官方文档

在此安装过程中,存在以下两个问题:

  • 为什么不使用源码安装呢?
  • 什么不能直接从操作系统的官方存储库安装而是需要先设置另一个存储库?

首先,讨论第一个问题:

  1. 在使用源码构建的时候,总是因为系统环境的不同,而遇到各种各样的问题。虽然希望通过自己去修改一些 make 的编译参数,来达到最适合系统的,最大限度发挥性能的方式,但折腾到最后,只能去安装。

    如果使用 OpenResty 源码安装的方式去搞,不仅步骤庞杂,还需要解决诸如:openssl(OpenResty 维护了自己的 OpenSSL 版本),pcre 等等外部依赖的问题,手动打补丁等等一起离谱的问题。

  2. 不直接从操作系统的包管理工具安装的原因是因为操作系统的官方存储仓库不愿意接受第三方维护的 openssl,pcre 和软件版,以防止他们给不知道如何选择的用户带来混乱。另一方面 OpenResty 需要指定版本的OpenSSLPCRE才能正常运行,而系统默认的版本都比较旧。

OpenResty CLI

安装 OpenResty 后,OpenResty CLIresty已默认安装。它是一个Perl脚本,正如之前提到的,OpenResty 生态系统工具都是用 编写的Perl,这是由 OpenResty 作者的技术偏好决定的。

$ which resty
/usr/bin/resty

$ head -n 1 /usr/bin/resty
#!/usr/bin/env perl

Hello World

实现一个hello world程序。

我们至少需要三个步骤才能完成它。

  1. 创建一个工作目录。
  2. 修改NGINX配置文件以在其中嵌入Lua代码。
  3. 启动OpenResty服务。

让我们从创建一个工作目录开始。

$ mkdir openresty-sample
$ cd openresty-sample
$ mkdir logs/ conf/

以下是根目录中nginx.confOpenResty 指令的极简,其中嵌入了ngx.say方法。content_by_lua

events {
    worker_connections 1024;
}

http {
    server {
        listen 8080;
        location / {
            content_by_lua '
                ngx.say("hello, world")
            ';
        }
    }
}

请先确保环境中已经添加了openresty PATH;然后,启动 OpenResty 服务:

$ openresty -p `pwd` -c conf/nginx.conf

# 查看服务进程
$ root@yuluo-ubuntu:/app/openresty/openresty-example# ps -ef | grep nginx
root       13989       1  0 07:40 ?        00:00:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -g daemon on; master_process on;
nobody     13990   13989  0 07:40 ?        00:00:00 nginx: worker process
root       14089       1  0 07:48 ?        00:00:00 nginx: master process openresty -p /app/openresty/openresty-example -c conf/nginx.conf
nobody     14090   14089  0 07:48 ?        00:00:00 nginx: worker process
root       14092    1519  0 07:48 pts/1    00:00:00 grep --color=auto nginx

$ root@yuluo-ubuntu:/app/openresty/openresty-example# ps -ef | grep openresty
root       13989       1  0 07:40 ?        00:00:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -g daemon on; master_process on;
root       14089       1  0 07:48 ?        00:00:00 nginx: master process openresty -p /app/openresty/openresty-example -c conf/nginx.conf
root       14094    1519  0 07:49 pts/1    00:00:00 grep --color=auto openresty

# 同时可以在 logs/nginx.pid 中查看 nginx 的 pid
$ cat logs/nginx.pid

如果没有出现错误,则说明OpenResty服务已成功启动。我们可以通过cURL命令访问它来查看返回的结果。

# 访问测试
$ root@yuluo-ubuntu:/app/openresty/openresty-example# curl -i 127.0.0.1:8080
HTTP/1.1 200 OK
Server: openresty/1.21.4.2
Date: Wed, 16 Aug 2023 07:50:13 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

hello, world

恭喜!OpenResty中的Hello World已经完成!

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yuyuan0316 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

HYL6668 发表于 2023-8-16 16:27
借楼问下,我有个PDF文件被加密了,只能使用“银河”系统的电脑打开而且打印不了,请问用什么工具编译一下使之能在其他电脑上打开。
lynxtang 发表于 2023-8-16 17:23
谢谢,逐字逐句看完了,很清晰明了;如果想去掉返回header里面的openresty里面的版本号,是不是还是得用源码修改编译安装呢
 楼主| yuluo829 发表于 2023-8-16 17:45
lynxtang 发表于 2023-8-16 17:23
谢谢,逐字逐句看完了,很清晰明了;如果想去掉返回header里面的openresty里面的版本号,是不是还是得用源 ...

这块没有深入研究,你可以去 github 看看,开源产品。
修改了一些东西的话,肯定是要重新编译的,不过重新编译会复杂很多
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 21:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表