第一个 OpenResty 程序【OpenResty HelloWolrd】

第一个 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已经完成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值