dgwwuq
Last Updated: September 02, 2017
·
174.7K
· 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!

23 Responses
Add your response

11068

Thank you, this helped greatly.

over 1 year ago ·
12253

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

Thanks! super awesome

over 1 year ago ·
13523

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

sudo apachectl stop

over 1 year ago ·
13652

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

over 1 year ago ·
13924

yep kill apache or httpd running

over 1 year ago ·
13944

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

over 1 year ago ·
14133

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

over 1 year ago ·
15274

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

over 1 year ago ·
15831

Me too, I'm getting 403 forbidden.

over 1 year ago ·
15915

@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

@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

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

over 1 year ago ·
17061

@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

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

Cheers!

over 1 year ago ·
17240

Can you also show how to setup additional vhosts on nginx

over 1 year ago ·
17410

Can you add additional nginx modules this way?

over 1 year ago ·
18782

Good tips for beginners!

over 1 year ago ·
26006

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

over 1 year ago ·
29060

Well-written. Thanks!

over 1 year 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!

over 1 year ago ·