不知道你是怎么在你的mac上安装nginx的,但是如果你跟我一样:

brew install nginx

然后你会发现你的nginx.conf中的端口是8080。
于是你可能像我一样试着把端口改为80,然后reload一下。

你发现没有任何错误提示,当你访问localhost时却没有nginx的首页,甚至连403也没有。

于是你尝试...

sudo lsof -n -i:80 | grep -i LISTEN

却发现没有输出和nginx有关的任何东西。
于是才想起OSX不允许system级以外的服务使用1024以下的端口。

起初我并没有太关心这个问题,我并不介意多输入几个数字。
直到我加入了一个新的团队,我和我那些使用其他操作系统的同事们统一了hosts,并使用同一个数据库进行开发时才意识到 —— 我可能因为这个问题给别人造成麻烦。

launchd

关于launchd的描述,这里引用一下manpage,如下:

launchd manages processes, both for the system as a whole and for individual users.
The primary and preferred interface to launchd is via the launchctl(1) tool which (among other options) allows the user or administrator to load and unload jobs.
Where possible, it is preferable for jobs to launch on demand
based on criteria specified in their respective configuration files.
launchd also manages XPC services that are bundled within applications and
frameworks on the system.
During boot launchd is invoked by the kernel to run as the first process on
the system and to further bootstrap the rest of the system.
You cannot invoke launchd directly.

launchd用于为系统和用户管理进程,主要通过launchctl对其进行操作,用户无法直接调用launchd。
启动时由内核调用launchd,运行第一个进程。

plist

另外我们需要了解的就是plist文件。
plist就是property list format的意思,是苹果用来保存应用数据的格式,其实就是个xml。
可以在 /usr/local/opt/nginx 下找到nginx对应的plist文件,比如在我的电脑上是 homebrew.mxcl.nginx.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>homebrew.mxcl.nginx</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/nginx/bin/nginx</string>
<string>-g</string>
<string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>

我们需要把这个文件复制到 /Library/LaunchDaemons 下,如果是 ~/Library/LaunchAgents 也可以,但两者有区别。
前者是系统启动时启动,后者则是在用户登录时启动。
接着执行launchctl load -w,如下:

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

最后,重启你的机器,你会发现nginx在80端口启动了。

OS X - 在80端口启动Nginx的更多相关文章

  1. nginx 普通用户使用80端口启动nginx

    方法一: 依次执行如下命令 cd /usr/local/nginx/sbin/ chown root nginx chmod u+s nginx 优点是,方便简单,缺点是,既然sudo权限都不给了.这 ...

  2. Mac OS X 绑定80端口,不装nginx的小技巧

    Mac OS X 因为要绑定80端口需要ROOT权限, 但是如果用root权限启动eclipse或tomcat又会造成, 启动创建的各类文件是root的,普通用户无法删除. 为此, 我们可以通过pfc ...

  3. Linux非root用户如何使用80端口启动程序

    默认情况下Linux的1024以下端口是只有root用户才有权限占用,我们的tomcat,apache,nginx等等程序如果想要用普通用户来占用80端口的话就会抛出java.net.BindExce ...

  4. linux下普通用户如何使用80端口启动程序

    linux下普通用户如何使用80端口启动程序 http://blog.csdn.net/shootyou/article/details/6750230 大家都知道默认情况下linux的1024以下端 ...

  5. 普通用户从非80端口启动tomcat,通过端口转发监听80端口

    linux下小于1024的端口都需要root去绑定. root权限启动tomcat是不明智的,可以使用非root权限启动tomcat监听8080端口,然后利用端口转发实现对80端口的监听. 端口转发: ...

  6. 非root用户在80端口运行nginx

    一般情况下没有这种需求,但对于强迫症患者来说,还是完整的走了一把. 普通用户是不允许使用1024以下端口的,所以此次操作仍然需要root权限来进行配置.而且由于使用了root安装,因此nginx用户仍 ...

  7. Swoole 绑定域名 80 端口 (Nginx 反向代理)

    启动 Swoole 的 http server,可以使用 IP + 端口 进行访问 创建 Nginx 虚拟域名 vim swotp.liuguofeng.com.conf server { liste ...

  8. SELinux配置不当导致httpd无法在非80端口启动

    检测是否为selinux导致httpd启动失败,若setenforce 0以后就可以启动,就表示selinux配置不当. 首先本机要支持semanage命令,安装方法网上有. semanage  po ...

  9. 非root启动80端口

    Linux非root用户如何使用80端口启动程序   默认情况下Linux的1024以下端口是只有root用户才有权限占用,我们的tomcat,apache,nginx等等程序如果想要用普通用户来占用 ...

随机推荐

  1. [转载]HTML5浏览器测试网站汇总

    http://www.cnblogs.com/javawebsoa/archive/2012/04/19/2458224.html 浏览器支持情况统计 When Can IUse:图表经常更新,展示了 ...

  2. HDU 2093 考试排名 模拟题

    解题报告: 题目描述:写一个程序给一个编程考试C++实时提交系统排名,给你的数据是题目的总数,每次错误提交罚的时间分,每位用户的姓名,然后是输入用户每题的完成情况,有一下几种情况,第一,输入只有一个正 ...

  3. C++ 修饰符类型

    C++ 修饰符类型 C++ 允许在 char.int 和 double 数据类型前放置修饰符.修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求. 下面列出了数据类型修饰符: signed u ...

  4. Contrastive Loss (对比损失)

    参考链接:https://blog.csdn.net/yanqianglifei/article/details/82885477 https://blog.csdn.net/qq_37053885/ ...

  5. poj1221

    dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  6. python基础类型 —— Sets集合

    集合(set)是一个无序不重复元素的序列. 基本功能是进行成员关系测试和删除重复元素. 运行结果如下: sets其他操作: myset.add('x') # 添加一项 myset.update([10 ...

  7. Faster R-CNN在GPU下的安装、测试经历

    在公司的服务器上安装faster rcnn时,遇到了不少问题: 1.cudnn版本不兼容的问题,解决办法参考: http://blog.csdn.net/WoPawn/article/details/ ...

  8. 【mysql】source导入多个文件

    在mysql中,可以将表导出为sql文件,比如1.sql, 2.sql等等. 导入一个文件: source /home/somepath/.sql 那么问题来了,如果我想一次导入100个文件呢?总不能 ...

  9. HttpUrlConnection的setDoOutput与setDoInput的区别

    httpUrlConnection.setDoOutput(true) httpUrlConnection.setDoInput(true) 这两个方法在develope的httpUrlConnect ...

  10. .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录

    结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...