First we need to install nginx. Using brew we just need to run the following command:
brew install nginx
Initialise nginx
To start nginx run the following commmand:
sudo nginx
If you want nginx server running automatically every time you log in, just run the following command:
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Test nginx server
At this point the nginx server should be running on port 8080. Open Safari and type the following address:
http://localhost:8080
We should see the nginx welcome page. The page loaded by default can be found in the following path: “/usr/local/Cellar/nginx/1.6.0_1/html/index.html“. Note that the folder 1.6.0_1 could be different depending on the nginx version you have installed.
Updating Config file
We going to change port 8080 to port 80 (HTTP) so we can access nginx just using the address http://localhost. Also we will activate the server access and error logs.
Updating port
Open nginx configuration file:
vim /usr/local/etc/nginx/nginx.conf
Look for the following lines:
server { listen 8080; server_name localhost;
Now update the file writing 80 instead 8080.
Activating server logs
To activate logs we need to uncomment the following lines removing the dash “#“:
#error_log logs/error.log;
and
#access_log logs/host.access.log main;
This will create useful logs at “/usr/local/Cellar/nginx/1.6.0_1/logs“. Remember that the folder “1.6.0_1” from the previous path could be different depending on the nginx version you have installed.
Saving new configuration
After doing all the previous changes to the config file we just need to save it and run the following command:
sudo nginx -s reload
And that was it, we have nginx running on http://localhost and generating error and access logs. For more information about the nginx config file please visit nginx documentation.