Mac os 安装 nginx 教程(success)

前言

  1. 最近使用Mac系统,从新预习一下nginx,所以写了这篇博客,以便以后和广大好友减少麻烦。
  2. 安装启动完会发现,在Mac下安装nginx,和Linux下安装nginx,其实没有区别。就是先安装三个依赖包,最后安装nginx即可。唯一的区别可能是三个依赖包的安装方式不同罢了。
  3. 注意下最后的bug,很简单,手动创建文件夹即可,原因是因为没有权限创建文件夹而已~

一、homebrew

1、介绍

Mac下包管理工具:homebrew

2、下载安装

安装使用常用命令:
https://blog.youkuaiyun.com/qq_35448985/article/details/123988782
安装开始的截图,接着跟着流程执行即可
在这里插入图片描述

二、安装nginx

1、安装前准备

nginx 依赖于 pcre、zlib、openssl

  1. nginx的http模块使用pcre来解析正则表达式,需要在linux上安装pcre
  2. nginx使用zlib对http包的内容进行gzip,需要在linux上安装安装zlib
  3. 安装openssl库,让 nginx 支持 https(即在ssl协议上传输http)

缺少依赖会报错如下错误

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

a、安装pcre

  1. 执行命令 brew install pcre
  2. 命令执行如下:
    在这里插入图片描述
    (base) C02ZT6HWLVDL:nginx-1.22.1 alsc$ brew install pcre
    Running `brew update --auto-update`...
    ==> Homebrew has enabled anonymous aggregate formula and cask analytics.
    Read the analytics documentation (and how to opt-out) here:
      https://docs.brew.sh/Analytics
    No analytics have been recorded yet (nor will be during this `brew` run).
    
    ==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
      https://github.com/Homebrew/brew#donations
    
    ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/pcre-8.45.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring pcre-8.45.big_sur.bottle.tar.gz
    🍺  /usr/local/Cellar/pcre/8.45: 204 files, 5.8MB
    ==> Running `brew cleanup pcre`...
    Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
    Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
    
    

b、安装zlib

  1. 执行命令 brew install zlib
  2. 命令执行如下:
    在这里插入图片描述
    (base) C02ZT6HWLVDL:nginx-1.22.1 alsc$ brew install zlib
    ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/zlib-1.2.13.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring zlib-1.2.13.big_sur.bottle.tar.gz
    ==> Caveats
    zlib is keg-only, which means it was not symlinked into /usr/local,
    because macOS already provides this software and installing another version in
    parallel can cause all kinds of trouble.
    
    For compilers to find zlib you may need to set:
      export LDFLAGS="-L/usr/local/opt/zlib/lib"
      export CPPFLAGS="-I/usr/local/opt/zlib/include"
    
    ==> Summary
    🍺  /usr/local/Cellar/zlib/1.2.13: 13 files, 424.9KB
    ==> Running `brew cleanup zlib`...
    Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
    Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
    

c、安装openssl

  1. 执行命令 brew install openssl

  2. 命令执行如下:
    在这里插入图片描述
    在这里插入图片描述

    (base) C02ZT6HWLVDL:nginx-1.22.1 alsc$ brew install openssl
    ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/ca-certificates-2022-10-11.all.bottle.tar
    ######################################################################## 100.0%
    ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/openssl%403-3.0.7.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Installing dependencies for openssl@3: ca-certificates
    ==> Installing openssl@3 dependency: ca-certificates
    ==> Pouring ca-certificates-2022-10-11.all.bottle.tar.gz
    ==> Regenerating CA certificate bundle from keychain, this may take a while...
    🍺  /usr/local/Cellar/ca-certificates/2022-10-11: 3 files, 225.4KB
    ==> Installing openssl@3
    ==> Pouring openssl@3-3.0.7.big_sur.bottle.tar.gz
    ==> Caveats
    A CA file has been bootstrapped using certificates from the system
    keychain. To add additional certificates, place .pem files in
      /usr/local/etc/openssl@3/certs
    
    and run
      /usr/local/opt/openssl@3/bin/c_rehash
    
    openssl@3 is keg-only, which means it was not symlinked into /usr/local,
    because macOS provides LibreSSL.
    
    If you need to have openssl@3 first in your PATH, run:
      echo 'export PATH="/usr/local/opt/openssl@3/bin:$PATH"' >> /Users/alsc/.bash_profile
    
    For compilers to find openssl@3 you may need to set:
      export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
      export CPPFLAGS="-I/usr/local/opt/openssl@3/include"
    
    ==> Summary
    🍺  /usr/local/Cellar/openssl@3/3.0.7: 6,454 files, 28.4MB
    ==> Running `brew cleanup openssl@3`...
    Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
    Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
    ==> Caveats
    ==> openssl@3
    A CA file has been bootstrapped using certificates from the system
    keychain. To add additional certificates, place .pem files in
      /usr/local/etc/openssl@3/certs
    
    and run
      /usr/local/opt/openssl@3/bin/c_rehash
    
    openssl@3 is keg-only, which means it was not symlinked into /usr/local,
    because macOS provides LibreSSL.
    
    If you need to have openssl@3 first in your PATH, run:
      echo 'export PATH="/usr/local/opt/openssl@3/bin:$PATH"' >> /Users/alsc/.bash_profile
    
    For compilers to find openssl@3 you may need to set:
      export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
      export CPPFLAGS="-I/usr/local/opt/openssl@3/include"
    

2、下载nginx

官网:http://nginx.org/
在这里插入图片描述
在这里插入图片描述

3、安装

  1. 我这里下载的是1.22.1的包,将你下载的 nginx-1.22.1.tar.gz 放在你想放的路径,
  2. 解压:tar -zxvf nginx-1.22.1.tar.gz
  3. 编译:sudo ./configuresudo是 super user do 的简称,使用管理员执行命令,主要是为辅助超级管理员完成一些超级管理员不能登录下的操作
  4. 安装:sudo make && make install
  5. 安装完成

4、 安装的异常:Permission denied

最后执行流程截图,发现异常:
在这里插入图片描述

/Library/Developer/CommandLineTools/usr/bin/make -f objs/Makefile install
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
mkdir: /usr/local/nginx: Permission denied
make[1]: *** [install] Error 1
make: *** [install] Error 2
(base) C02ZT6HWLVDL:nginx-1.22.1 alsc$ ls

意思应该是创建文件夹没有权限,导致make install错误,安装错误。
所以手动创建,并改权限即可

sudo mkdir -p /usr/local/nginx
sudo chmod -R 777 /usr/local/nginx
sudo make && make install

执行完安装命令,则显示如下,无报错信息,能发现 /usr/local/nginx/ 已经有了数据,则success
在这里插入图片描述
然后:

cd /usr/local/nginx/sbin
./nginx

输入地址 http://localhost:80 看到如图内容,即nginx部署成功
补充说明:每次启动会麻烦可以建立全局变量
方案一:ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
方案二:
vi /etc/profile
export NGINX_HOME=/usr/local/nginx
export PATH= P A T H : PATH: PATH:NGINX_HOME/sbin
source /etc/profile
配置后启动:
nginx

在这里插入图片描述

### macOS安装 Nginx #### 使用 Homebrew 安装 Nginx 对于大多数用户而言,最简便的方法是通过 Homebrew 工具来安装 Nginx。Homebrew 是一款 Mac 操作系统下的软件包管理工具,提供多种功能并易于操作[^2]。 ```bash brew install nginx ``` 此命令将会自动处理依赖关系并将 Nginx 安装到系统的合适位置。 #### 手动离线安装 Nginx 如果遇到无法使用 Homebrew 的情况,则可以选择手动离线安装方式: 1. **准备工作** 需要预先准备好 PCRE 和 OpenSSL 文件作为 Nginx 编译所需的支持库。可以从官方网站获取这些资源,并将其放置于本地磁盘上的指定路径中以便后续编译过程调用[^1]。 2. **下载与解压源码包** 将官方发布的稳定版 Nginx 压缩包以及上述提到的两个支持库压缩包一并下载下来,并依次执行解压缩操作。例如针对 PCRE 库可以这样做: ```bash tar xvzf pcre-8.45.tar.gz ``` 3. **配置编译环境** 切换至已解压后的 Nginx 源代码根目录内,接着利用 `./configure` 脚本来设定具体的编译参数。这里特别指定了 PCRE 库的位置以确保能够顺利集成正则表达式匹配等功能特性。 ```bash cd ~/Downloads/nginx-1.23.2 ./configure --with-pcre=~/Downloads/pcre-8.45 ``` 4. **编译和安装** 接着就可以正式开始编译工作了,在确认无误的情况下继续完成最终的安装步骤。这一步可能会请求管理员权限来进行某些写入操作。 ```bash sudo make install ``` 默认情况下,Nginx 会被部署在 `/usr/local/nginx/` 目录之下[^3]。 5. **验证安装成果** 启动服务端程序并通过浏览器测试页面加载状况。当看到 "Welcome to nginx!" 字样时即说明整个安装流程已经顺利完成。 ```bash cd /usr/local/nginx/sbin sudo ./nginx ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java冯坚持

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值