Install Ngnix on OSX 10.8 Mountain Lion

nginxNgnix is (among other things) a web server that uses an asynchronous event-driven approach to handling requests, instead of the Apache HTTP Server model that defaults to a threaded or process-oriented approach. Nginx’s event-driven approach can provide more predictable performance under high loads.

This how-to explains installing Ngnix on OSX Mountain Lion using Homebrew. Ngnix can be built from source, but the Homebrew installation is quick and easy.

(Optional) Prerequisite Step: Configure PHP-FPM

If you will be using Ngnix to server PHP pages, then you should configure PHP-FPM for OSX first before proceeding with the Ngnix installation.

Step 1: Install Homebrew if isn’t already installed

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Step 2: Create installation folders and set permissions

Change USERNAME to your own username

sudo mkdir /usr/local/var
sudo chown USERNAME:admin /usr/local
sudo chown USERNAME:admin /usr/local/include
sudo chown USERNAME:admin /user/local/lib
sudo chown USERNAME:admin /user/local/var

Step 3: Install Ngnix

brew install nginx

Check the output to make sure no errors were encountered. If so, generally brew is pretty good at indicating where the error occurred and possible solutions. Once the installation is complete Nginx will be located at /usr/local/Cellar/nginx/1.2.3/ and the Ngnix config files will be located at /usr/local/etc/nginx/

Step 4: Launch and verify Ngnix

First launch Ngnix manually from the command line to verify everything is working properly. Once verified it can be installed as a launch agent

/usr/local/sbin/ngnix

You won’t see any output after entering the command, however unless you see errors the server should be running in the background. To verify, open your browser to the default location http://localhost:8080/

Once you have verified that the server is running you can stop the server, or just reload the configuration file with these commands:

/usr/local/sbin/ngnix -s stop

Step 5: Customize the configuration file

Open /usr/local/etc/nginx/nginx.conf with your preferred text editor. Some settings that you may want to change are:

listen 80;
root /Users/YOURUSERNAME/Sites;
index index.php index.html index.htm;

Optionally if you will be using PHP-FPM you can alter the php location like so:

location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}

If Ngnix is still running you can tell it to reload the conf file with the following. You can also just stop/start the server. If there are any errors in the conf file you’ll see a message when you reload.

/usr/local/sbin/ngnix -s reload

You should be able to test and run PHP pages. If things are not working, you can monitor the error log:

tail -f /usr/local/Cellar/nginx/1.2.3/logs/error.log

(Optional) Step 6: Install Ngnix as a Launch Agent

If you want Ngnix to launch when your computer starts without having to launch it manually then you can install a launch agent.

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.2.3/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/

Ngnix will now launch when you restart the computer. You can manually start it the first time with:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Ngnix should now be running as a service and you can verify again in your browser.

No comments yet.

Leave a Reply

Please leave these two fields as-is: