mac使用homebrew无法安装nginx解决方案

本文档介绍了在使用Homebrew安装nginx时遇到因网络问题导致的下载失败错误,特别是curl请求超时的问题。解决方法是切换到阿里云的Homebrew镜像源进行加速,详细步骤包括修改brew.git、homebrew-core.git的远程仓库URL以及设置HOMEBREW_BOTTLE_DOMAIN环境变量。此外,还提供了恢复默认下载源的命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

使用 homebrew 安装 nginx

brew install nginx

报错:

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openssl%401.1-1.1.1k.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/pcre-8.44.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/nginx-1.19.9.mojave.bottle.tar.gz

curl: (22) The requested URL returned error: 404 
Trying a mirror...
==> Downloading https://ghcr.io/v2/homebrew/core/bottles/nginx-1.19.9.mojave.bottle.tar.gz
==> Downloading from https://github.com/-/v2/packages/container/package/homebrew%2Fcore%2Fbottles%2Fnginx-1.19.9.mojave.bottle.tar.gz
Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.
Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.

curl: (28) Connection timed out after 15002 milliseconds
Error: Failed to download resource "nginx"
Download failed: https://ghcr.io/v2/homebrew/core/bottles/nginx-1.19.9.mojave.bottle.tar.gz

原因分析

是因为网络的原因导致的,即homebrew的下载源是外网的,因此下载速度很慢或者会导致下载失败。

解决办法

可能所有的mac用户都遇到过下面这种操蛋情况,在网络环境不太好的时候,你满怀期望的敲下 brew install 应用名称,静静的等待安装结果时,Homebrew 在 Updating 的地方卡死了,真是令人沮丧,当然了,这不是Homebrew的错,Homebrew 是一款 Mac OS 平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

Homebrew 主要分两部分:git repo(位于 GitHub)和二进制 bottles,这两个网站在国内访问起来都慢的可以。接下来操作一下如何使用阿里云来加速这两部分的下载。

我们可以考虑使用 阿里云 的 Homebrew 镜像源进行加速。
假设你没有更换过镜像源,执行 brew 命令安装应用的时候,下面3 个仓库地址将影响你的下载速度:
brew.git
homebrew-core.git
homebrew-bottles

首先,更换 brew.git

cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

然后更换 homebrew-core.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

运行brew config 来检查一下地址是否更新成功

brew config

最后一步:更换 homebrew-bottles

首先查看一下你的系统当前shell版本
echo $SHELL
根据版本不同,会输出 2 种结果,/bin/zsh 或 /bin/bash,根据不同类型运行不同的命令

/bin/zsh

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

/bin/bash

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

如果阿里云镜像挂了,可以执行下面的命令进行下载源的恢复

# 重置brew.git:
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

# 重置homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

随后可以删掉 HOMEBREW_BOTTLE_DOMAIN 环境变量,将你终端文件 ~/.bash_profile 或者 ~/.zshrc 中的HOMEBREW_BOTTLE_DOMAIN这一行删掉,随后执行命令:

source ~/.bash_profile
source ~/.zshrc
<think>好的,我需要帮助用户删除通过Homebrew安装Nginx并重新安装。首先,我得回忆一下Homebrew的基本命令。用户之前提供的引用里提到卸载软件用`brew uninstall`,比如`brew uninstall nginx`,所以第一步应该是执行这个命令来卸载现有的Nginx。 接下来,用户可能想彻底清理残留文件,虽然Homebrew通常卸载得比较干净,但有时配置文件可能还在。根据引用[3],MySQL的配置文件在`/opt/homebrew/etc`下,所以Nginx的配置可能也在类似的位置,比如`/usr/local/etc/nginx`或者Homebrew安装路径下。可以建议用户手动删除这些目录,或者使用`brew cleanup`来清理旧文件。 然后重新安装Nginx,用`brew install nginx`。但用户之前可能遇到安装问题,如引用[2]提到的镜像问题,如果安装失败,可能需要重置下载源。用户提供的解决方案包括重置brew.git和homebrew-core的远程URL到官方源,这可以解决镜像问题。如果用户遇到下载慢,还可以建议替换为国内镜像源,比如中科大或清华的源,但需要具体步骤指导。 安装完成后,启动服务需要用`brew services start nginx`,并检查是否运行正常,比如用`brew services list`查看状态,或者直接访问localhost:8080。可能还需要提醒用户配置文件的位置,方便他们后续修改。 最后,确保回答覆盖卸载、清理、重新安装的每个步骤,并引用提供的资料,比如卸载命令来自引用[1],解决安装问题的方法来自引用[2],配置文件位置参考引用[3]。同时,语言要简洁明了,步骤清晰,避免用户混淆。</think>### 卸载并重新安装Nginx的步骤 #### 1. **卸载Nginx** 首先使用Homebrew卸载已安装Nginx: ```bash brew uninstall nginx ``` 此命令会移除Nginx的主程序和相关依赖(若没有其他软件依赖它们)[^1]。 #### 2. **清理残留文件(可选)** Homebrew卸载后可能残留配置文件,需手动删除: ```bash # 删除Nginx配置文件目录(路径可能因版本而异) sudo rm -rf /usr/local/etc/nginx # 或针对M1芯片MacHomebrew路径 sudo rm -rf /opt/homebrew/etc/nginx ``` 此步骤可避免旧配置影响新安装[^3]。 #### 3. **修复Homebrew源(若安装失败)** 如果安装过程中遇到网络问题(如镜像失效),重置Homebrew到官方源: ```bash # 重置brew.git仓库 cd "$(brew --repo)" git remote set-url origin https://github.com/Homebrew/brew.git # 重置homebrew-core仓库 cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://github.com/Homebrew/homebrew-core.git # 更新Homebrew brew update ``` 此操作可解决因镜像源失效导致的安装失败问题[^2]。 #### 4. **重新安装Nginx** 执行安装命令: ```bash brew install nginx ``` 安装完成后,Homebrew会输出Nginx的配置路径和默认端口(通常为`8080`)。 #### 5. **启动Nginx服务** ```bash brew services start nginx ``` 验证服务状态: ```bash brew services list ``` 访问 `http://localhost:8080` 应显示Nginx欢迎页面。 --- ### 关键配置说明 - **配置文件路径**:`/usr/local/etc/nginx/nginx.conf`(Intel芯片)或 `/opt/homebrew/etc/nginx/nginx.conf`(M1芯片) - **默认网站根目录**:`/usr/local/var/www` 或 `/opt/homebrew/var/www` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值