Nginx on macOS

Installation Guide

April 23, 2019

With little tweaking, it is possible to install the Nginx web server on macOS. This guide first explains how to get Nginx running, and then how to set up the file structure so that it's identical to the Linux-based version.

A little housekeeping

Before we install Nginx, we need to make sure that no other web server is running on macOS. To do this, we’ll stop Apache:


      sudo apachectl stop

NOTE If Apache isn’t installed, this will obviously do nothing.

If you don't have it, install Homebrew

To install Nginx on macOS, we will need Homebrew. If you have not yet installed Homebrew, please follow this guide and do so before continuing.

Install and start Nginx

Once Homebrew is installed, run:


      brew install nginx

Then, once Nginx has been installed, start it by running:


      sudo nginx

To check that Nginx is correctly installed, and has started correctly, go to http://localhost:8080. You should see the default Nginx page.

NOTE If you are installing Nginx on a remote Mac, you’ll need to substitute the localhost in the above URL for the IP address of the remote Mac. For example, if you have installed Nginx on a Mac at 10.0.1.10, you’d type http://10.0.1.10:8080.

Change the listening port

By default, Nginx listens on port 8080 on Macs. In most circumstances, you will want it to listen on port 80 instead. Before we do this, we need to temporarily stop Nginx from running:


      sudo nginx -s stop

Next, open the configuration file at /usr/local/etc/nginx/nginx.conf:


      nano /usr/local/etc/nginx/nginx.conf

The server block in your /usr/local/etc/nginx/nginx.conf file should look as follows:


      server {
listen       80;
server_name  localhost;

Save your changes by pressing Ctrl + X, then Y, then Enter. Then restart Nginx:


      sudo nginx

To test that the change worked, visit http://localhost. You should see the default Nginx page.

NOTE As before, if you have installed Nginx on a remote Mac, you’ll need to substitute the localhost in the above URL for the IP address of the remote Mac. For example, if you have installed Nginx on a Mac at 10.0.1.10, you’d type http://10.0.1.10.

Set up sites-available and sites-enabled

Next, we need to set up the traditional sites-enabled and sites-available folder structure:


      mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/sites-available
cd /usr/local/etc/nginx/sites-available
touch default.conf
cd /usr/local/etc/nginx/sites-enabled
ln -s ../sites-available/default.conf

Having created this structure, we need to tell Nginx where to look for enabled sites. To do this, we’ll open /usr/local/etc/nginx/nginx.conf again:


      nano /usr/local/etc/nginx/nginx.conf

Then we need place the following line within the http block:


      include /usr/local/etc/nginx/sites-enabled/*.conf;

Save your changes by pressing Ctrl + X, then Y, then Enter.

Set macOS to run Nginx when it starts

Finally, we need to tell macOS to run Nginx when it starts. To do this, we’ll create a blank file named homebrew.mxcl.nginx.plist in /Library/LaunchDaemons/ :


      nano /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

And paste the following into it (thanks to jimothyGator for the samples):


      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/sbin/nginx</string>
        <string>-g</string>
        <string>daemon off;</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>

Save your changes by pressing Ctrl + X, then Y, then Enter.

Set up your server blocks

Now you are ready to start creating server blocks in exactly the same way as you would in any other Nginx setup.

Versions

macOS: 10.14.4
Homebrew: 2.1.1
Nginx: 1.15.12
Notice an error?

Have we got something wrong? Please let us know and we’ll fix it right away.

Categories
Mac macOS Nginx
Tags

3 responses to “Nginx on macOS

  1. Hi there,
    I followed the steps you gave all the way till the end, but then in the end, for some reason, I could not save the code in this file: homebrew.mxcl.nginx.plist in /Library/LaunchDaemons/
    I get an error that says this: [ Error writing /Library/LaunchDaemons/homebrew.mxcl.nginx.plist: Permission denied ]
    Can someone please tell me what I might be doing wrong?

    Thanks

    1. Hi Ruqia,

      Can you tell me which version of macOS you are running? And have you tried adding sudo before nano /Library/LaunchDaemons/homebrew.mxcl.nginx.plist?

      Charles

  2. After following the instructions on setting up sites-* and trying to sudo nginx -s reload got an error:

    nginx: [emerg] open() "https://launchpad.thehomeofthefuture.com/usr/local/etc/nginx/sites-enabled/default.conf" failed (2: No such file or directory) in /usr/local/etc/nginx/nginx.conf:116

    Created default.conf file in sites-available folder and after that I managed to start nginx.
    Created file with: touch default.conf.

    Hope I did everything right and it will help somebody.

Join the Discussion

Your email address will not be published. Required fields are marked *