nginx on OS X

Posted on Thursday, June 19, 2014

Recently I had to install nginx on my OSX machine for some testing…. And I ran into some permission issues.
First I am using brew to install nginx on OSX.  The Brew website is located at [1].  The command line install ‘one liner’ is located at the bottom of the page

To install run this command

> ruby -e "$(curl -fsSL"

After brew is installed you can now use it to install nginx.

> brew install nginx

Then to start nginx you can run

> sudo nginx

Or to stop it run

> sudo nginx -s stop

However...  I want to update the nginx.conf file to run using a nginx user, and do a few more settings.
For my test I created a folder on my desktop and I called it www.   In the www file I placed a very simple index.html file to test.   I also placed a log folder within the www folder.
Now edit the nginx.conf.

> sudo vi /usr/local/etc/nginx/nginx.conf

I edited mine to the following (make sure to user your folder structure not mine :)

user  nginx staff;
worker_processes  4;

error_log  /Users/patman/Desktop/www/logs/error.log;
pid       /Users/patman/Desktop/www/logs/;

events {
  worker_connections  1024;
  multi_accept on;

http {
  include       mime.types;
  default_type  application/octet-stream;

  log_format  main_fmt '$remote_addr - $remote_user [$time_local]  $status '
   '"$request" $body_bytes_sent "$http_referer" '
   '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /Users/PBailey-MBPro/Desktop/www/logs/access.log main_fmt;   

  sendfile     on;
  tcp_nopush   on;
  tcp_nodelay on;
  server_names_hash_bucket_size 128;
  keepalive_timeout    70;
  types_hash_max_size 2048;

  gzip on;
  gzip_disable "msie6";

  proxy_buffering    off;
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   X-Scheme $scheme;
  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header   Host $http_host;

  server {
    listen       8080;
    root /Users/patman/Desktop/www;

Create the nginx user 

I found this site that goes over a few quick commands that can set up a new user in OSX [2]
Run the following commands

> sudo dscl . create /Groups/nginx PrimaryGroupID 390
> sudo dscl . create /Users/nginx UniqueID 390
> sudo dscl . create /Users/nginx PrimaryGroupID 390
> sudo dscl . create /Users/nginx UserShell /bin/false
> sudo dscl . create /Users/nginx RealName nginx
> sudo dscl . create /Users/nginx NFSHomeDirectory /usr/local/var/run/nginx
> sudo dscl . create /Groups/nginx GroupMembership nginx
> sudo dseditgroup -o edit -a nginx -t user staff

This will create the nginx user and add them to the "staff" group.

At this point if I start the nginx server

> sudo nginx

I get the following 403 forbidden Error

Fix permissions

Update permissions in the www directory

> cd ~/www
> sudo chown -R nginx:staff *
> sudo chown -R nginx:staff .

This almost gets us there but we have one last issue to fix the.  The Desktop folder that contains the www folder needs to allow the staff group to read it.

> sudo chmod g+x ..

Now nginx is working !

[1]  Brew Homepage
       Visited 6/2014
[2]  Automatically start Nginx as a daemon on Mac OS X Mountain Lion 10.8

       Visited 6/2014

No comments:

Post a Comment