Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattbloomfield/48f800275fdb30c83eea35669ca150fe to your computer and use it in GitHub Desktop.
Save mattbloomfield/48f800275fdb30c83eea35669ca150fe to your computer and use it in GitHub Desktop.
Setting up Nginx on Your Local System

Setting up Nginx on Your Local System

by Keith Rosenberg

Step 1 - Homebrew

The first thing to do, if you're on a Mac, is to install homebrew from http://mxcl.github.io/homebrew/

The command to type into terminal to install homebrew is:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Following the installation of homebrew, type

brew cleanup

and then

brew doctor

Running brew doctor will inform you if you have any problems with your install or your machine in general. If you run into troubles, Stack Overflow is your best friend.

But I'm on Windows

I can't help you :'(

Step 2 - nginx

With homebrew installed, you can run (note: without sudo - do not run 'sudo brew' - it's evil)

brew install nginx

directly following install, run

sudo nginx

this will start nginx on port 8080. Open your web browser and go to http://localhost:8080

If this works, run

sudo nginx -s stop

to stop nginx.

Step 3 - Set up nginx.conf

Your nginx.conf file lives at

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

The absolute first thing you should do is make a backup copy of this file in case you royally destroy everything:

mv /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.bak && cp /usr/local/etc/nginx/nginx.conf.bak /usr/local/etc/nginx/nginx.conf

you now have a fresh copy of the nginx.conf file to bastardize, and a backup copy in case you get in too deep.

Step 4 - Modularizing Nginx for Serving Multiple Local Websites

The last thing you want to do is create a directory where you can store individual site nginx.conf files. At the bottom of your nginx.conf file, before your last closing bracket, you should include the following line:

http {
  # ... ...
  # ... ... nginx stuff
  # ... ...
  
  # include all server conf files
  include conf.d/*.conf;
}

Then, make a conf.d directory in your nginx folder:

mkdir /usr/local/etc/nginx/conf.d

and now whenever you create a new server, just place them into your conf.d directory:

vim /usr/local/etc/nginx/conf.d/myWebSite.conf

you can then put a specific server config into that file like so:

server {
    listen       8080;
    server_name  mywebsite.local.com;

    location / {
        root   /Users/myusername/Desktop/Projects/mywebsite/public/;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

Breaking Down a Server Configuration

####listen The port number you want nginx to listen on for this site

server_name

Points to a domain configured in your Mac OSX host file (note: be sure to edit with "sudo", as super user):

> sudo vim /private/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##

127.0.0.1 localhost

# mywebsite
127.0.0.1 mywebsite.local.com

location

Sets up a route and describes where it should point. The root sub-attribute should be a path from / (root) to your project on your local machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment