不知道你是怎么在你的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. CSS3 定位| Position研究

    视区(视口) 当浏览者查看一份网页文件时,通常使用者代理(User Agents, UA, 浏览器)会提供给浏览者一个视区(视窗或者是画面里的其它可视区域).当我们调整视区大小时,UA 就有可能会改变 ...

  2. CSSOM

    概要 狭义的 DOM API 仅仅包含 DOM 树形结构相关的内容. DOM 中的所有的属性都是用来表现语义的属性,CSSOM 的则都是表现的属性. CSSOM 是 CSS 的对象模型,在 W3C 标 ...

  3. dialog 菜单实例

    dislog 菜单实例 while : do clear menu=`dialog --title system custom` [ $? -eq ] && echo "$m ...

  4. 第6月第10天 svn checkout sqlite3

    1. http://www.cnblogs.com/xuling/p/5602036.html 2. http://blog.csdn.net/qq_26819733/article/details/ ...

  5. AC自动机(病毒侵袭 )

    题目链接:https://cn.vjudge.net/contest/280743#problem/B 题目大意:中文题目 具体思路:AC自动机模板题,编号的时候注意,是按照给定的id进行编号的.然后 ...

  6. mongodb导入json文件

    mongoimport --db test  --collection item --jsonArray  item.json

  7. Wireshark按照域名过滤

    HTTP协议 http.host == "http://baidu.net" DNS协议 dns.qry.name=="www.baidu.com"

  8. linux下查看各硬件型号

    查看主板型号 # dmidecode |grep -A 8 "System Information"System Information 上网查DELL CS24-TY,找到说主板 ...

  9. Centos 6.9 安装Rabbitmq

    一.安装Rabbitmq 首先安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel Er ...

  10. django Rest Framework---缓存通过drf-extensions扩展来实现

    什么情况下使用缓存 1.不经常更新的数据 2.用户经常访问的一些页面,比如商品列表页.商品详情页等 3.用户经常修改的一些操作:购物车.订单中心等 关于DRF缓存扩展可以参考文档:http://chi ...