OS X-Start nginx on port 80

Source: Internet
Author: User

I don't know how you install Nginx on your Mac, but if you're like me:

brew install nginx

Then you will find that the port in your nginx.conf is 8080.
So you might as well try to change the port to 80 and reload.

You find that there are no error hints when you visit localhost without Nginx's homepage, even 403.

So you try ...

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

But found no output and nginx related to anything.
So I think of OSX. Do not allow services other than system level to use ports below 1024.


At first I didn't care too much about the problem, and I didn't mind typing a few more numbers.
It was not until I joined a new team that I and my colleagues who used other operating systems unified the hosts and used the same database for development that I realized--I might be causing trouble for others because of this problem.

Launchd

For a description of Launchd, here is a reference to Manpage, as follows:

launchd manages processes, both for the system as a whole and for individual users.
The primary and preferred interface to LAUNCHD are 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 demandbased on criteria specified in their respective configuration Files.
LAUNCHD also manages XPC services that is bundled within applications and frameworks on the system.
During boot Launchd is invoked by the kernel-run as the first process on the system and to further bootstrap the rest O f the System.
You cannot invoke launchd directly.

Launchd is used to manage processes for systems and users, primarily through LAUNCHCTL, and users cannot call Launchd directly.
At startup, the kernel calls Launchd and runs the first process.

Plist

Another thing we need to know is the plist file.
Plist is the meaning of the property list format, which is the format that Apple uses to save the application data, which is actually an XML.
You can find nginx corresponding plist file under /usr/local/opt/nginx , such as homebrew.mxcl.nginx.plist on my computer.

The content of it is probably this:

 <?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&        Gt        <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> </arra y> <key>WorkingDirectory</key> <string>/usr/local</string> </dict></p List>  


We need to copy this file to /library/launchdaemons , if it is ~/library/launchagents , but there is a difference.
The former is initiated when the system starts, while the latter is initiated when the user logs on. Then execute the launchctl load-was follows:

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


Finally, restart your machine and you will find nginx booting on port 80.

OS X-Start nginx on port 80

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.