dgwwuq
Last Updated: July 15, 2017
·
115.1K
· reiaguilera

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!

Say Thanks
Respond

23 Responses
Add your response

11068
2f0700f42f0f2c3d1241a14e93b78343

Thank you, this helped greatly.

over 1 year ago ·
12253
Dsc 0694 normal

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?

over 1 year ago ·
13405
00a2de23cf78cb5e57700fa2776a2fb5

Thanks! super awesome

over 1 year ago ·
13523
Goxv4py1 normal

@weizhang185 , try to stop apache ( if it's already loaded )

sudo apachectl stop

over 1 year ago ·
13652
2af4ea9bb6380b59d05d2b9b6d8112e9

I had to brew install pcre --universal to make it work

over 1 year ago ·
13924
2af4ea9bb6380b59d05d2b9b6d8112e9

yep kill apache or httpd running

over 1 year ago ·
13944
2af4ea9bb6380b59d05d2b9b6d8112e9

This should run without using sudo nginx anyone know why I have to keep using sudo?

over 1 year ago ·
14133
Default profile 5 normal

@pixel67 anything that needs to listen on port 80 would need sudo in my opinion.

over 1 year ago ·
15274
0 lwfcfpvn9sgcfiyclwaafrsnnil 5ldc1urjfrpwwa5gstvbjs0mwkebs5ad6g2rkedyhbakgal4

I followed the tutorial to the letter, only it is showing 403 Forbidden

over 1 year ago ·
15831
3f387ad225e6e44d1f40142c55e63689

Me too, I'm getting 403 forbidden.

over 1 year ago ·
15915
Perfil

@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).

over 1 year ago ·
15916
Perfil

@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.

over 1 year ago ·
15937

update post, thanks @pablohenrique @zeu666 @pixel67 and more :D

over 1 year ago ·
16381
D5bf47bb33a1e34432167a07204cdeb5 normal

Excellent post! Do you know how can I launch nginx at boot?

over 1 year ago ·
17061
0 ocf8sd4jm4awn61eh8p0swdovzhiq5ceh5r0sew2eopri3voqk0uio0guveqbc qitdycdvd73gm

@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.

=)

over 1 year ago ·
17062
D5bf47bb33a1e34432167a07204cdeb5 normal

Cool man, your awesome! I got it yesterday actually, anyway it's good to know I did right.

Cheers!

over 1 year ago ·
17240
97297d534d6f83c30f51daff8795cc27

Can you also show how to setup additional vhosts on nginx

over 1 year ago ·
17410
None

Can you add additional nginx modules this way?

over 1 year ago ·
18782
Github pic

Good tips for beginners!

over 1 year ago ·
26006
0 hc1ylgy1yabrqnoqmq dl0m1y2rvq1uqg6f3l0shtdxiibunukitwxjakckxbqs43ta8oz07v u9

Really Helpful! Cheers!

over 1 year ago ·
28365

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.).

8 months ago ·
29060

Well-written. Thanks!

about 2 months ago ·
29109

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!

about 1 month ago ·