Installing Nginx in Mac OS X Maverick With Homebrew
Install with brew
Use brew to install the nginx with command:
brew install nginx
After install run:
sudo nginx
Testing
Open Navigator it by going to URL:
http://localhost:8080
Configuration
The default place of nginx.conf on Mac after installing with brew is:
/usr/local/etc/nginx/nginx.conf
Changing the default port (8080)
We shall change it to 80. First stop the nginx server if it is running by:
sudo nginx -s stop
Update thanks @zue666, @pixel67
if apache is running must also stop
##sudo apachectl stop
Then open nginx.conf with (example vim):
vim /usr/local/etc/nginx/nginx.conf
and change the:
server {
listen 8080;
server_name localhost;
##access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
to:
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
Save configuration and relaunch nginx
sudo nginx
Testing
Open Navigator it by going to URL:
http://localhost
Update: thanks @pablohenrique
* ERROR 403 Forbidden - *
dont worry, some other application is already using port 80. probably be skype.
1 Solution 1: Change Skype port in Skype > Preferences > advanced
2 Solution 2: No change server { listen to 80.
That means that nginx will use port 8080 instead of 80. To access it I would use this url
http://localhost:8080.
Changing the path of defualt web location
he nginx html folder (brew install only) is by the defult in:
/usr/local/Cellar/nginx/1.2.3/html
Note: change ** 1.2.3 ** to your nginx version.
The defualt path configuration:
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
To let say Users/to/www:
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root /Users/to/www;
index index.html index.htm;
}
After change relaunch nginix server and nginx is now serving pages from your custom folder!
enjoy!
Written by Reinaldo Aguilera
Related protips
23 Responses
Thank you, this helped greatly.
got this message after changing port to 80.
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
Any advice?
Thanks! super awesome
@weizhang185 , try to stop apache ( if it's already loaded )
sudo apachectl stop
I had to brew install pcre --universal to make it work
yep kill apache or httpd running
This should run without using sudo nginx
anyone know why I have to keep using sudo?
@pixel67 anything that needs to listen on port 80 would need sudo in my opinion.
I followed the tutorial to the letter, only it is showing 403 Forbidden
Me too, I'm getting 403 forbidden.
@juniorand
@sijpkes
I had the same problem. The problem is 403 Forbidden, which means that the file (probably the index.html) has no permission to be read. So execute this command:
$ sudo chmod 777 /var/www/index.html
Your problem will be solved. Of course, make sure that you have that file inside that specific path.
OBS.: I left the port 8080 in my nginx.conf, because apache is running on port 80 and I need both (for now).
@weizhang185 your problem is due to something quite simple: some other application is already using port 80. You could figure out what is the app using that port and delete it or change the port the service is using. I had the same problem and the way I fixed it was modifying my nginx.conf file to this:
server {
listen 8080;
server_name localhost;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
That means that nginx will use port 8080 instead of 80. To access it I would use this url: http://localhost:8080.
If you fix that problem and now you're getting the 403 Forbidden error, read my last comment right here on this page.
update post, thanks @pablohenrique @zeu666 @pixel67 and more :D
Excellent post! Do you know how can I launch nginx at boot?
@eagostini just after brewing, you can run the following statement:
$ ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
this will make launchd start nginx at login.
=)
Cool man, your awesome! I got it yesterday actually, anyway it's good to know I did right.
Cheers!
Can you also show how to setup additional vhosts on nginx
Can you add additional nginx modules this way?
Good tips for beginners!
Really Helpful! Cheers!
Thanks. I was thinking I needed to use the brew services (sudo brew services nginx start) to start on 80, but this was definitely the right solution (using sudo nginx , etc. etc.).
Well-written. Thanks!
Hi, great tutorial!
How do I create different configurations of my local projects? I have a personal folder named "Projects" where I place all PHP works I make, so it's possible to configure the nginx.conf file and make a "common" configuration for them all? Or should I create different confs for everyone of them?
Thanks in advance!