Install Nginx on El Capitan OSX 10.11

October 10th, 2015

Let's go the easy way: install Nginx on El Capitan (OSX 10.11) with Homebrew:

brew install nginx

At the end of the installation proces you will see the following message:

To have launchd start nginx at login:
  ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Then to load nginx now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Or, if you don't want/need launchctl, you can just run:
  nginx

I am not sure why, but when running the command nginx I get the following error messages:

nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied)
2015/10/09 22:22:18 [emerg] 1650#0: open() "/usr/local/var/log/nginx/access.log" failed (13: Permission denied)

When I verify the permissions I see that the root user is owner of the directory and its files. This is wrong, it should be me (from the staff group), who is the owner of the directory and its content. Let's change this:

sudo chown -R acorovic:staff /usr/local/var/log/nginx

When I run the nginx command I get the warnings that Nginx could not bind to an address:

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] still could not bind()

Ok, now we are getting somewhere, Nginx is at least trying to bind to port number 8080. But, I have Apache running so I think this obviously interfering with Nginx. Let's stop Apache Server for now:

sudo apachectl stop

But wait, I need to make sure that port 8080 is not listening to some other process, let's check this with the Network Utility tool. When I scan ports 8000 - 9000 I see the following output:

Port Scan has started…

Port Scanning host: 127.0.0.1

     Open TCP Port:     8080        http-alt
     Open TCP Port:     9000        cslistener
Port Scan has completed…

Wait a minute... I still have something listening on port 8080? Oh, it's my Node.js, that little bastard is still running :). The other port, port 9000, is from my local Play Framework server. Let's stop Node.js first:

sudo killall -9 node
// This will work in most cases, but not mine. I have Node.js restarted at every crash or startup, so I need to unload it's plist file:
sudo launchctl unload /Library/LaunchDaemons/com.node.plist

Running the Network Utility tool again:

Port Scan has started…

Port Scanning host: 127.0.0.1

     Open TCP Port:     9000        cslistener
Port Scan has completed…

Sweet! Ready to run the nginx command again? No error messages this time, so what happens when visiting: localhost:8080:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Yeah! It works! But how to stop it?

Well, we need to send a signal to Nginx to make it stop:

nginx -s stop

That's all folks! Next blog about running Nginx in front of a Play app?