OSX Yosemite can't bind brew nginx to port 80

8,308

Solution 1

You have to use sudo to bind any port below 1024, privileged ports. i will try to sum up all. first disable internal apache by adding disabled to file /System/Library/LaunchDaemons/org.apache.httpd.plist

       <key>Disabled</key>
       <true/>

then copy nginx's plist file to /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

    <key>Label</key>
<string>homebrew.mxcl.nginx</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
    <string>/usr/local/opt/nginx/bin/nginx</string>
    <string>-g</string>
    <string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>

with this parameters. check the plist file ownership. which has to belong root:wheel

-rw-r--r-- 1 root wheel 571 Dec 21 19:39 /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

then you can start nginx with privilege to bind port 80 & 443.

PS: you can use lunchy (a simple ruby app to manage launchctl.) which is perfect for this job. like: sudo lunchy edit apache, sudo lunchy start nginx etc.

Also you can see what is wrong while playing launchctl with syslog -w command.

Solution 2

If you need to run nginx or apache at port 80, set root privileges for .plist file.

(I'm running nginx by lunchy)

For example:

sudo chown root ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
sudo chgrp wheel ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Share:
8,308

Related videos on Youtube

deb0rian
Author by

deb0rian

SFSWD

Updated on September 18, 2022

Comments

  • deb0rian
    deb0rian over 1 year

    Installed nginx and php-fpm via Homebrew.

    I've disabled native Apache 2.4 that comes with OSX by running:

    glfx:~ glfx$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
    

    It even says it's not running anymore:

    /System/Library/LaunchDaemons/org.apache.httpd.plist: Could not find specified service
    

    Then I run my nginx and check what's bound to my port 80:

    glfx:~ glfx$ lsof -i :80
    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    nginx     266 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     267 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     268 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     269 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     270 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     271 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     272 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    nginx     273 glfx    9u  IPv4 0x6267c63df8016e53      0t0  TCP *:http (LISTEN)
    

    My nginx configs are:

    worker_processes  8;
    user glfx staff;
    
    events {
       worker_connections  1024;
    }
    
    http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format main '$remote_addr - $remote_user [$time_local] $status '
                  '"$request" $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "http_x_forwarded_for"';
    
    access_log  logs/nginx/access.log  main;
    error_log   logs/nginx/error.log   debug;
    
    sendfile       on;
    
    tcp_nopush     on;
    tcp_nodelay    off;
    
    gzip  on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_proxied any;
    
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 20000;
    proxy_headers_hash_bucket_size 128;
    proxy_headers_hash_max_size 20000;
    
    underscores_in_headers on;
    
    include /usr/local/etc/nginx/sites/*;
    }
    

    And site config:

    server {
        listen 80;
    
        server_name signals.dev;
        root /Users/glfx/Projects/signalsplatform.dev/public_html;
    
        access_log  /usr/local/var/log/nginx/signals.dev.access.log;
        error_log  /usr/local/var/log/nginx/signals.dev.error.log;
    
        rewrite ^/app_dev\.php/?(.*)$ /$1 permanent;
    
        location / {
                index app_dev.php;
                try_files $uri @rewriteapp;
        }
    
        location @rewriteapp {
                rewrite ^(.*)$ /app_dev.php/$1 last;
        }
    
        location ~ ^/(app|app_dev|config)\.php(/|$) {
                root /Users/glfx/Projects/signalsplatform.dev/public_html;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        }
    
    }
    

    When I try to access signals.dev in my browser - No connection to the web server at all, though signals.dev:8080 is giving me Nginx 404 not found.

    Why I can't bind my nginx to use 80 port?

    • Nhat Nguyen
      Nhat Nguyen over 8 years
      Did you ever figure out what was wrong? I'm having the same issue. Thanks.
    • Jichao
      Jichao about 8 years
  • deb0rian
    deb0rian over 9 years
    Apache already had this key added, also Nginx configuration is exactly as you provided and path/privileges of the plist file is the same. So something is wrong.
  • deb0rian
    deb0rian over 9 years
    That didn't show any anomalies. :)
  • risyasin
    risyasin over 9 years
    did you try to start nginx with sudo directly for testing? maybe something else is wrong. try "sudo nginx -c /usr/local/etc/nginx/nginx.conf"
  • deb0rian
    deb0rian over 9 years
    Yup. Feb 2 15:44:41 glfx.local sudo[1036] <Notice>: glfx : TTY=ttys001 ; PWD=/usr/local/etc/nginx/sites ; USER=root ; COMMAND=/usr/local/bin/nginx -c /usr/local/etc/nginx/nginx.conf This is output of syslog. No errors reported anywhere.
  • Matt Fletcher
    Matt Fletcher almost 6 years
    It won't allow me to edit the answer unless I change 6 or more chars (most stupid rule on the whole of stack exchange...) but you should remove the full stops/periods from the ends of the commands.
  • Matt Fletcher
    Matt Fletcher almost 6 years
    Also I don't believe this even works. Why would closing off permissions make it work, when root would be able to run files belonging to your home user anyway?
  • Marina Anayko
    Marina Anayko almost 6 years
    @MattFletcher You have to use sudo to bind any port below 1024, privileged ports as it was sad above. So .plist need to be available under root