tulip notes
首页
  • 学习笔记

    • 《Vue》
  • 踩坑日记

    • JavaScript
  • MQ
  • Nginx
  • IdentityServer
  • Redis
  • Linux
  • Java
  • SpringBoot
  • SpringCloud
  • MySql
  • docker
  • 算法与设计模式
  • 踩坑与提升
  • Git
  • GitHub技巧
  • Mac
  • 网络
  • 项目构建合集
  • 一些技巧
  • 面试
  • 一些杂货
  • 友情链接
  • 项目发布
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Star-Lord

希望一天成为大师的学徒
首页
  • 学习笔记

    • 《Vue》
  • 踩坑日记

    • JavaScript
  • MQ
  • Nginx
  • IdentityServer
  • Redis
  • Linux
  • Java
  • SpringBoot
  • SpringCloud
  • MySql
  • docker
  • 算法与设计模式
  • 踩坑与提升
  • Git
  • GitHub技巧
  • Mac
  • 网络
  • 项目构建合集
  • 一些技巧
  • 面试
  • 一些杂货
  • 友情链接
  • 项目发布
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • Mac

    • Mac环境配置相关
    • 查看一些参数的指令
    • Mac中使用MySql
    • Mac使用技巧
    • Mac与homebrew
      • 简介
      • Mongo的相关资料
      • 操作指令-安装mongo
        • 检测安装
        • mongod作为服务运行
        • mongod手动启动
      • 安装nginx
        • 安装 命令
        • 查看信息
        • 启动与停止
        • 启动与停止
        • 查看端口
        • 常见场景说明
        • 日志文件
        • 卸载
        • 删除残留的配置文件和数据目录
  • 网络

  • 项目构建合集

  • 工具
  • Mac
EffectTang
2025-02-10
目录

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
1

如果 Homebrew 已经安装,此命令会显示 Homebrew 的版本号。如果未安装,则会提示 brew: command not found 或类似的错误信息

安装指令在上述的链接中有详细说明,这里只展示其中一个

brew install mongodb-community@6.0
1

完成安装后,一些配置文件的位置如下:

除了安装 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
1

如果您将其mongod作为服务进行管理,它将使用上面列出的默认路径。要停止服务器实例,请使用:

brew services stop mongodb-community
1

# mongod手动启动

如果您不想要或不需要后台 MongoDB 服务,您可以运行:

mongod --config /usr/local/etc/mongod.conf
1

注意:如果您不包含--config配置文件路径的选项,MongoDB 服务器将没有默认配置文件或日志目录路径,而将使用数据目录路径/data/db。

要mongod手动关闭启动的admin数据库并运行db.shutdownServer():

  • 关闭 MongoDB 社区服务器 6.x 及更高版本:
mongosh admin --eval "db.shutdownServer()"
1
  • 关闭 MongoDB Community Server 5.0 及以下版本:
mongo admin --eval "db.shutdownServer()"
1

# 安装nginx

通过homebrew 安装nginx。

# 安装 命令

执行以下命令来安装 Nginx:

brew install nginx
1

安装过程中,Homebrew 会自动下载并配置 Nginx 及其依赖项。默认安装的为 最近的稳定版。

# 查看信息

安装好后,你可以通过如下几个命令来查看版本信息,和配置文件信息

  • 查看版本信息
brew info nginx
1

通过以上信息你可以查看版本信息,还有配置文件信息,输出信息大致如下:

我们可以发现 版本为 1.27.5,配置文件在,/opt/homebrew/etc/nginx/nginx.conf

此外,还提到了一个额外的目录用于加载额外的服务器配置文件:

nginx will load all files in /opt/homebrew/etc/nginx/servers/.
1

这意味着除了主配置文件外,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\;
1
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
1

该命令不仅会检查配置文件的语法是否正确,还会输出配置文件的路径。

# 启动与停止

# 启动与停止

如果希望 Nginx 在后台运行并随系统启动而自动启动,可以使用:

brew services start nginx

# 停止
brew services stop nginx

# 重启
brew services restart nginx
1
2
3
4
5
6
7

如果不需要后台服务模式运行 Nginx,则可以直接运行:

/opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;
1

# 查看端口

那启动好之后,怎么查看它使用的端口是哪个呢?怎么查看是否启动成功了呢?

可以直接查看 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"
1

大致会输出以下内容:

   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;
1
2
3
4
5
6

当然,如果启动的时候配置文件不是它,就没用了,所以这个命令最重要的前提条件,是找到对应的配置文件。

  • 通过lsof命令
sudo lsof -i -P -n | grep nginx
1

输出示例:

nginx   1234  root    6u  IPv4 0x...   0t0  TCP *:80 (LISTEN)
nginx   1235  nobody  6u  IPv4 0x...   0t0  TCP *:80 (LISTEN)
1
2

*:80 表示监听所有 IP 的 80 端口。

  • sudo lsof -i -P -n: 列出所有网络连接,不进行端口和服务名称的转换,也不进行主机名查找。
  • | grep nginx: 仅保留那些包含 "nginx" 字符串的行,帮助你快速定位与 Nginx 相关的网络活动。

这种方法特别适合于快速检查 Nginx 正在使用的端口或者确认 Nginx 是否正确地监听了预期的端口。如果你发现 Nginx 没有监听任何端口,可能是配置有问题或服务未启动。在这种情况下,你可以检查 Nginx 的日志文件以获取更多信息。

# 常见场景说明

  1. 默认端口:

    • HTTP:80
    • HTTPS:443 若未修改配置,Nginx 通常监听这两个端口。
  2. 自定义端口: 如果修改过配置文件(如 nginx.conf 或 sites-enabled/default),需检查对应的 listen 指令。

  3. 端口冲突: 如果端口被占用,Nginx 会启动失败,可通过以下命令排查:

    sudo lsof -i :80  # 查看占用 80 端口的进程
    
    1

# 日志文件

通常情况下,Nginx 的访问日志和错误日志会被放置在其默认的日志目录下,对于通过 Homebrew 安装的情况,通常是:

/opt/homebrew/var/log/nginx/
1

在这个目录下,你应该能找到 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
1

# 删除残留的配置文件和数据目录

即使你已经卸载了 Nginx,某些配置文件和数据目录可能仍然保留在你的系统中。你可以手动删除这些文件以完成彻底的清理工作。

  • 删除 LaunchAgent 配置文件
rm ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
1

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/
1
2
3
上次更新: 2025/05/11, 15:27:14
Mac使用技巧
网络的基本介绍

← Mac使用技巧 网络的基本介绍→

最近更新
01
面向切面跟自定义注解的结合
05-22
02
时间跟其他数据的序列化
05-19
03
数据加密与安全
05-17
更多文章>
Theme by Vdoing | Copyright © 2023-2025 EffectTang
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式