mac自带apache服务,并占用80端口,如果需要使用nginx,需要禁用apache并自己安装nginx
一、禁用自带apache
1.关闭apache
sudo apachectl -k stop
如果出现如下报错:
httpd: could not reliably determine the server’s fully qualified domain name, using pgydemacbook-pro.local. set the ‘servername’ directive globally to suppress this message
(1) 打开apache配置目录:
vim /etc/apache2/httpd.conf
(2) 搜索:#servername www.example.com:80
增加一行 servername localhost:80
(3) 重新启动apache
sudo apachectl restart
(4) 关闭apache
sudo apachectl -k stop
2.禁止apache自启动
sudo launchctl unload -w /system/library/launchdaemons/org.apache.httpd.plist
二、使用 homebrew 安装 nginx
在 macos 上使用 homebrew 安装 nginx 时,nginx 的默认安装目录通常是 /usr/local/cellar/nginx/
。具体的安装路径可以通过以下命令查看:
brew --prefix nginx
1. nginx 配置文件和目录
安装完成后,nginx 的主要配置文件和目录通常位于以下位置:
- 主配置文件:
/usr/local/etc/nginx/nginx.conf
- 站点配置文件:
/usr/local/etc/nginx/servers/
- 日志文件:
/usr/local/var/log/nginx/
- html 文件:
/usr/local/var/www/
2. 安装 nginx
使用 homebrew 安装 nginx:
brew install nginx
3. 启动和管理 nginx
安装完成后,可以使用以下命令启动、停止和重启 nginx:
# 启动 nginx brew services start nginx # 停止 nginx brew services stop nginx # 重启 nginx brew services restart nginx
4. 配置 nginx
您可以编辑 nginx 的主配置文件 /usr/local/etc/nginx/nginx.conf
,或者在 /usr/local/etc/nginx/servers/
目录中添加新的站点配置文件。
例如,创建一个新的站点配置文件 /usr/local/etc/nginx/servers/www.test.com.conf
:
server { listen 80; server_name www.test.com; # 将所有 http 请求重定向到 https, 如果需要强制https开启这项 # return 301 https://$host$request_uri; location / { proxy_pass http://localhost:9000; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-forwarded-proto $scheme; } } server { listen 443 ssl; server_name www.test.com; ssl_certificate /usr/local/etc/nginx/certs/certificate.pem; ssl_certificate_key /usr/local/etc/nginx/certs/certificate.key; location / { proxy_pass http://localhost:9000; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-forwarded-proto $scheme; } }
5. 验证配置并重启 nginx
(1)验证 nginx 配置是否正确(如果不确定配置是否正确这个命令很有效):
nginx -t
(2)如果配置正确,重启 nginx 以应用更改:
brew services restart nginx
三、生成 ssl/自签名 证书
在你指定的文件夹中打开命令行工具
# x509 根据现有的证书请求生成自签名根证书 # -days 设置证书的有效天数 # rsa:2048 现代的 ssl/tls 配置通常要求至少 2048 位的密钥 openssl req -newkey rsa:2048 -nodes -keyout www.test.com.key -x509 -days 365 -out www.test.com.crt
country name (2 letter code) [国家]:cn
state or province name (full name) [省份]:beijing
locality name (eg, city) [城市]:beijing
organization name (eg, company) [组织/公司]:test
organizational unit name (eg, section) [部门/单位]:test
common name (eg, fully qualified host name) [域名]:www.test.com
email address [邮箱]:test@outlook.com
到此这篇关于mac使用nginx设置代理,并禁用自带apache的文章就介绍到这了,更多相关mac使用nginx内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论