In this short tutorial I document how I installed and configured Nginx
on my MacBook running Mac OSX Leopard. Of course, I can’t possibly
guarantee that the exact same set of steps will work for you, but it may
help you troubleshoot any problems that you might have as you get
started with this fantastic little piece of software.
If you haven’t yet discovered Nginx you’re in for a treat. The benefits of using a web server like Nginx over its competitors have already been thoroughly covered elsewhere,
so I won’t really address that topic here. However I will say that I’ve
been using the software for about 6 months now and it’s been a real
pleasure to work with. It’s extremely fast, dependable, and easy to
configure. Previous to working with Nginx I’ve only worked with two
other web servers — Apache and LightTPD — and Nginx is by far my
favorite. If you’re curious to know how Apache and Nginx compare
performance-wise, the hard work has already been done for you. Let’s get started.
For the purposes of this tutorial we’ll be using the excellent MacPorts
package manager for Mac OSX. Of course we could just compile our own
Nginx from source, but the MacPorts repositories are kept very
up-to-date so there’s really no need. If you haven’t already done so,
head on over to macports.org and grab yourself a copy of the MacPorts disk image from the downloads page. The installer is very self explanatory and should complete in just a few minutes.
Once you’ve installed MacPorts, crack open a terminal window (found in /Applications/Utilities/Terminal.app) and type in the following command to make sure your MacPorts installation is the latest and greatest.
$ sudo port -d selfupdate
$ port version
Version: 1.710
Now let’s go ahead and install the Nginx port.
$ sudo port install nginx
---> Fetching nginx
...
---> Cleaning nginx
Let us pause and marvel at the beauty that is MacPorts. Whenever I
type a little one-liner like that, I can’t help but think of all the
poor saps who have come before me and made all the hard stuff about
installing software and their dependencies extremely easy. In fact, let
us take a second to remember just how spoiled we are while our software is installing. 🙂
Back? Great! Let’s make sure it installed correctly.
$ which nginx
/opt/local/sbin/nginx
$ nginx -v
nginx version: nginx/0.7.59
During the installation process, there was a small section that looked something like this:
########################################################### # A startup item has been generated that will aid in # starting nginx with launchd. It is disabled # by default. Execute the following command to start it, # and to cause it to launch at startup: # # sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist ###########################################################
What’s all this? It basically says that if we want Nginx to start up
automatically when we start up our machine, we can do so using launchd (see this Developer Connection article from Apple for more information on launchd).
I’m going to leave this step out at this point, simply because I prefer
to not have Nginx constantly running on my machine (I’ll show you at
the end of this article how to start and stop the Nginx server daemon at
will). However, if you prefer to not have to manually start Nginx when
you want to use it, go ahead and issue the following command:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist
Now that we’ve got Nginx installed the way we want, it’s time to configure the thing. If we check out the Nginx Portfile, we can see that all of our configuration files were installed into /opt/local/etc/nginx (hint: look for the nginx_confdir
directive). If you don’t like looking at Portfiles, just ignore that
last bit and trust me. Let’s have a look inside the directory and see
what’s there.
$ cd /opt/local/etc/nginx
$ ls -1
fastcgi_params.example
koi-utf
koi-win
mime.types.example
nginx.conf.example
win-utf
We can ignore the koi-utf, koi-win, and win-utf files for now. These files contain various character maps. The fastcgi_params.example
file is only needed if we’re going to be setting up FastCGI, which is
outside the scope of this particular article. The two files that we’re
interested in at this point are the mime.types.example and nginx.conf.example files.
The mime.types.example file contains a map of Internet media types
(more commonly referred to as MIME types) to various file extensions
that represent files of that type. The defaults are pretty good, so
we’ll just copy this file for now. If you need to register any custom
MIME types later, you’ll know where to look.
$ sudo cp mime.types.example mime.types
Now on to the main Nginx configuration file. First, copy the example.
$ sudo cp nginx.conf.example nginx.conf
Now I’ll do a quick overview of the main configuration file and point
out the relevant configuration directives. The example file only has
the bare minimum enabled, but it’s already enough to start the server.
Open up nginx.conf in your favorite text editor. If we
strip out all the commented lines (which I only do here for conciseness
in writing), we’ll get something close to the following:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root share/nginx/html;
}
}
}
This file reads something like this: Nginx is configured to start up
one worker process that may accept up to 1024 connections at once. In
the http daemon configuration section, we include our mime.types
directives and set a default type in case it cannot be automatically
determined. Then we set up a server configuration. The concept of a
“server” in Nginx is more or less equal to that of a “virtual host”
under Apache. We tell our server to bind to port 80 on localhost and the
root directory of our site. Then we tell this server what to do if it
encounters an error.
Note that the file paths in the default configuration file are relative to /opt/local because that’s the directory we installed Nginx to. So the path share/nginx/html will be expanded to /opt/local/share/nginx/html. If we want to have our web root be somewhere else, like /var/www for example, just use the full path instead of relative paths.
Of course, that was a very high-level overview of the directives in
this file. A more detailed description of all Nginx configuration
directives can be found on the Nginx wiki.
So, let’s start up the server.
$ sudo nginx
$ ps ax | grep nginx
69033 ?? Ss 0:00.00 nginx: master process nginx
69034 ?? S 0:00.00 nginx: worker process
We can see that Nginx started up one master process and one worker. If you navigate to http://localhost/, you should see a welcome message from Nginx.
Awesome! You’re now running Nginx. When the daemon is running, it stores its pid in a file at /opt/local/var/run/nginx/nginx.pid. So to stop the daemon, we can use the following command:
$ sudo kill `cat /opt/local/var/run/nginx/nginx.pid`