通过 Homebrew 安装 nginx 和 php-fpm。
我通过运行以下命令禁用了 OSX 自带的原生 Apache 2.4:
glfx:~ glfx$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
它甚至说它不再运行了:
/System/Library/LaunchDaemons/org.apache.httpd.plist: Could not find specified service
然后我运行我的 nginx 并检查绑定到我的端口 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)
我的 nginx 配置是:
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/*;
}
和站点配置:
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;
}
}
当我尝试在浏览器中访问 signals.dev 时 - 根本没有连接到 Web 服务器,尽管 signals.dev:8080 显示 Nginx 404 未找到。
为什么我无法绑定我的 nginx 来使用 80 端口?
答案1
您必须使用 sudo 绑定 1024 以下的任何端口,特权端口。我会尝试总结所有内容。首先通过在文件中添加禁用来禁用内部 apache/系统/库/LaunchDaemons/org.apache.httpd.plist
<key>Disabled</key>
<true/>
然后将 nginx 的 plist 文件复制到/库/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>
使用此参数。检查 plist 文件所有权。它必须属于 root:wheel
-rw-r--r-- 1 根轮 571 十二月 21 19:39 /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
然后您可以以权限启动 nginx 来绑定端口 80 和 443。
PS:您可以使用午餐的(一个用于管理 launchctl 的简单 ruby 应用程序)非常适合这项工作。例如:sudo lunchy edit apache、sudo lunchy start nginx 等。
您还可以在使用 launchctl 时看到错误系统日志-w命令。
答案2
如果您需要在端口 80 上运行,请为 .plist 文件设置 root 权限nginx
。apache
(我正在nginx
吃午餐)
例如:
sudo chown root ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
sudo chgrp wheel ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist