Mac与homebrew
# Mac与homebrew
# 简介
Homebrew (opens new window)是一个开源的包管理器,主要用于[macOS]和[Linux]系统。它用于安装、更新和管理软件包,极大地简化了在 Apple Silicon(如M1芯片)上的操作。
# Mongo的相关资料
下面是它的官网以及安装方法:
https://github.com/mongodb/homebrew-brew
https://www.mongodb.com/zh-cn/docs/manual/tutorial/install-mongodb-on-os-x/
配置文件
https://www.mongodb.com/zh-cn/docs/v6.0/administration/configuration/#std-label-configuration-file
# 操作指令-安装mongo
# 检测安装
brew --version
如果 Homebrew 已经安装,此命令会显示 Homebrew 的版本号。如果未安装,则会提示 brew: command not found
或类似的错误信息
安装指令在上述的链接中有详细说明,这里只展示其中一个
brew install mongodb-community@6.0
完成安装后,一些配置文件的位置如下:
除了安装 MongoDB 服务器和工具二进制文件之外,该mongodb-community
公式还创建:
- 配置文件:
$(brew --prefix)/etc/mongod.conf
- 日志目录路径:
$(brew --prefix)/var/log/mongodb
- 数据目录路径:
$(brew --prefix)/var/mongodb
默认值brew --prefix
取决于您的 CPU 架构:
- 英特尔:
/usr/local
- M1:
/opt/homebrew
# mongod
作为服务运行
要立即launchd
启动mongod
并在登录时重新启动,请使用:
brew services start mongodb-community
如果您将其mongod
作为服务进行管理,它将使用上面列出的默认路径。要停止服务器实例,请使用:
brew services stop mongodb-community
# mongod
手动启动
如果您不想要或不需要后台 MongoDB 服务,您可以运行:
mongod --config /usr/local/etc/mongod.conf
注意:如果您不包含--config
配置文件路径的选项,MongoDB 服务器将没有默认配置文件或日志目录路径,而将使用数据目录路径/data/db
。
要mongod
手动关闭启动的admin
数据库并运行db.shutdownServer()
:
- 关闭 MongoDB 社区服务器 6.x 及更高版本:
mongosh admin --eval "db.shutdownServer()"
- 关闭 MongoDB Community Server 5.0 及以下版本:
mongo admin --eval "db.shutdownServer()"
# 安装nginx
通过homebrew 安装nginx。
# 安装 命令
执行以下命令来安装 Nginx:
brew install nginx
安装过程中,Homebrew 会自动下载并配置 Nginx 及其依赖项。默认安装的为 最近的稳定版。
# 查看信息
安装好后,你可以通过如下几个命令来查看版本信息,和配置文件信息
- 查看版本信息
brew info nginx
通过以上信息你可以查看版本信息,还有配置文件信息,输出信息大致如下:
我们可以发现 版本为 1.27.5,配置文件在,/opt/homebrew/etc/nginx/nginx.conf
此外,还提到了一个额外的目录用于加载额外的服务器配置文件:
nginx will load all files in /opt/homebrew/etc/nginx/servers/.
这意味着除了主配置文件外,Nginx 还会自动加载位于 /opt/homebrew/etc/nginx/servers/
目录下的所有配置文件(如果有的话)。
==> nginx: stable 1.27.5 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
Installed
/opt/homebrew/Cellar/nginx/1.27.5 (27 files, 2.5MB) *
Poured from bottle using the formulae.brew.sh API on 2025-05-11 at 15:50:36
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/n/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@3 ✔, pcre2 ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
Docroot is: /opt/homebrew/var/www
The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /opt/homebrew/etc/nginx/servers/.
To start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- 查看配置信息
运行以下命令来测试配置文件的有效性,同时也能显示配置文件的位置:
nginx -t
该命令不仅会检查配置文件的语法是否正确,还会输出配置文件的路径。
# 启动与停止
# 启动与停止
如果希望 Nginx 在后台运行并随系统启动而自动启动,可以使用:
brew services start nginx
# 停止
brew services stop nginx
# 重启
brew services restart nginx
2
3
4
5
6
7
如果不需要后台服务模式运行 Nginx,则可以直接运行:
/opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;
# 查看端口
那启动好之后,怎么查看它使用的端口是哪个呢?怎么查看是否启动成功了呢?
可以直接查看 Nginx 的配置文件来确定它被设置为监听哪个端口。默认情况下,Homebrew 安装的 Nginx 的主配置文件位于 /opt/homebrew/etc/nginx/nginx.conf
(对于 Apple Silicon Mac)或 /usr/local/etc/nginx/nginx.conf
(对于 Intel Mac)。打开该文件并查找 listen
指令:
- 查看端口
cat /opt/homebrew/etc/nginx/nginx.conf | grep "listen"
大致会输出以下内容:
listen 8080;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# listen 8000;
# listen somename:8080;
# listen 443 ssl;
2
3
4
5
6
当然,如果启动的时候配置文件不是它,就没用了,所以这个命令最重要的前提条件,是找到对应的配置文件。
- 通过lsof命令
sudo lsof -i -P -n | grep nginx
输出示例:
nginx 1234 root 6u IPv4 0x... 0t0 TCP *:80 (LISTEN)
nginx 1235 nobody 6u IPv4 0x... 0t0 TCP *:80 (LISTEN)
2
*:80
表示监听所有 IP 的 80 端口。
sudo lsof -i -P -n
: 列出所有网络连接,不进行端口和服务名称的转换,也不进行主机名查找。| grep nginx
: 仅保留那些包含 "nginx" 字符串的行,帮助你快速定位与 Nginx 相关的网络活动。这种方法特别适合于快速检查 Nginx 正在使用的端口或者确认 Nginx 是否正确地监听了预期的端口。如果你发现 Nginx 没有监听任何端口,可能是配置有问题或服务未启动。在这种情况下,你可以检查 Nginx 的日志文件以获取更多信息。
# 常见场景说明
默认端口:
- HTTP:
80
- HTTPS:
443
若未修改配置,Nginx 通常监听这两个端口。
- HTTP:
自定义端口: 如果修改过配置文件(如
nginx.conf
或sites-enabled/default
),需检查对应的listen
指令。端口冲突: 如果端口被占用,Nginx 会启动失败,可通过以下命令排查:
sudo lsof -i :80 # 查看占用 80 端口的进程
1
# 日志文件
通常情况下,Nginx 的访问日志和错误日志会被放置在其默认的日志目录下,对于通过 Homebrew 安装的情况,通常是:
/opt/homebrew/var/log/nginx/
在这个目录下,你应该能找到 access.log
和 error.log
文件。
综上所述,根据 brew info nginx
的输出,你可以确定已安装的 Nginx 版本是 1.27.5,且其主要配置文件位于 /opt/homebrew/etc/nginx/nginx.conf
。如果有需要进一步调整或查看的配置,可以编辑这个文件或者检查 /opt/homebrew/etc/nginx/servers/
下是否有额外的配置文件。
# 卸载
使用 Homebrew 卸载 Nginx:
brew uninstall nginx
# 删除残留的配置文件和数据目录
即使你已经卸载了 Nginx,某些配置文件和数据目录可能仍然保留在你的系统中。你可以手动删除这些文件以完成彻底的清理工作。
- 删除 LaunchAgent 配置文件
rm ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Nginx 的配置文件通常位于 /usr/local/etc/nginx/
或 /opt/homebrew/etc/nginx/
(对于 Apple Silicon Mac)。你可以删除这个目录及其内容:
- 删除配置文件
sudo rm -rf /usr/local/etc/nginx/
# 或者
sudo rm -rf /opt/homebrew/etc/nginx/
2
3