欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

在 OS X 中使用 OpenResty

发布时间:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 在 OS X 中使用 OpenResty 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

1. 移除 lighttpd

在 Mac OS X 上安装lighttpd 的时候,我使用 launchctl 来实现 lighttpd 的自动启动。现在,只需要 unload 即可。

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.lighttpd.plist

这会立即停止 lighttpd 的运行。接下来,将 homebrew.mxcl.lighttpd.plist 从~/Library/LanuchAgents 目录中移除。否则下次启动系统的时候,lighttpd 又会启动。

2. 配置 OpenResty 环境

2.1 编译和安装

OpenResty 的编译和安装流程,在官网上讲得很清楚,我不再赘述。

在这里我假设 OpenResty 已经安装到默认路径 /usr/local/openresty 。

修改 /usr/local/openresty/nginx/conf/nginx.conf ,将其中的 server 段下的listen 80 改为 listen 8080 。

这是因为 80 端口只能被 root 用户启动。而在本文中我们是使用当前用户启动 nginx 的。

如果 8080 端口也被占用,请自行换成可用端口。

2.2 创建 plist 文件

launchctl 依赖一个 plist 配置文件来工作。我们需要手动创建这个文件。

plist 是一种标准的 xml 格式,这种格式的详细介绍,可以看这里:cocos2d-x中的plist文件格式详解 。

launchctl 对这个配置文件的格式有一些具体的要求,可以查看 launchd.plist 。

我们创建的 ~/Library/LaunchAgents/org.openresty.plist 文件,内容如下:


<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict><key>Label</key><string>org.openresty</string><key>ProgramArguments</key><array><string>/usr/local/openresty/nginx/sbin/nginx</string><string>-p</string><string>/usr/local/openresty/nginx</string></array><key>RunAtLoad</key><true/><key>KeepAlive</key><false/><key>HardResourceLimits</key><dict><key>NumberOfFiles</key><integer>512</integer></dict><key>SoftResourceLimits</key><dict><key>NumberOfFiles</key><integer>512</integer></dict> </dict> </plist>



注意其中 Label、Program和ProgramArguments 这三个 Key 是必须存在的。

KeepAlive 这个值建议设置成 false ,除非在 nginx.conf 中设置了 daemon off 。Wayne 在launchctl笔记 中作了解释:

设定nginx登录后自启动后发现,由于缺省情况下是生成子进程后父进程立即退出,导致launchctl在KeepAlive配置的作用下反复启动nginx,产生了很多错误信息,所以在设置守护进程的时候要注意规避这类问题,nginx可以通过设置daemon off;或者去除掉KeepAlive设置来解决。

2.3 载入任务

launchctl load ~/Library/LaunchAgents/org.openresty.plist

这条命令会直接启动 openresty ,下次系统重启的时候,也会自动启动。

2.4 使用 80 端口

既然是自己用,那么使用 8080 总是让人很不爽。要使用 80 端口,也很简单。

首先,将 ~/Library/LaunchAgents/org.openresty.plist 复制到/Library/LaunchDaemons/org.openresty.plist :

cp ~/Library/LaunchAgents/org.openresty.plist /Library/LaunchDaemons/org.openresty.plist

/Library/LaunchDaemons 是给管理员使用的,在用户登录前生效,以 root 身份执行任务。

为什么不复制到 /Library/LaunchAgent 中呢?因为 Wayne 在 launchctl笔记 中提到:

LaunchAgents下的plist都会以当前登录用户的身份load进来……

然后,移除当前的监听:

launchctl unload ~/Library/LaunchAgents/org.openresty.plist rm ~/Library/LaunchAgents/org.openresty.plist

最后,用 sudo 调用 launchctl:

sudo launchctl load /Library/LaunchDaemons/org.openresty.plist

当然,记得要把 nginx 配置文件中的监听端口改成 80 。

3. 快捷方式

在开发过程中,经常需要重启 nginx 进程。在我的电脑上,有两个 nginx 进程,一个负责正常的 HTTP 服务;一个负责测试 OpenResty 功能。

我写了一个脚本 openresty 用于快速操作 nginx 进程。

#!/bin/bash sign=${1:-reload} prefix=${2:-1}if [ "$prefix" = 1 ]; thenprefix='/usr/local/etc/openresty' elseprefix="$hhl/server" fiecho "nginx -s $sign -p $prefix" nginx -s "$sign" -p "$prefix"



在实际使用中,我只需要这样调用就行了:

#!/bin/bash openresty reload 1 openresty reopen 2




转载于:https://my.oschina.net/surjur/blog/486547

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的在 OS X 中使用 OpenResty的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。