common wx:CodingTechWork
,一起学习进步。
引言
本文主要总结如何在Mac电脑上进行Nginx服务的安装,重点讲解使用brew
命令进行安装和验证的过程及问题记录。
安装步骤
安装过程记录
查看nginx信息
首先使用命令brew info nginx
进行本机nginx的查看。
liaojianya@liaojianyadeMacBook-Pro downloads % brew info nginx
nginx: stable 1.21.6 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
Not installed
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@1.1 ✔, pcre2 ✘
==> Options
--HEADInstall HEAD version
==> Caveats
Docroot is: /usr/local/var/wwwThe default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.nginx will load all files in /usr/local/etc/nginx/servers/.To restart nginx after an upgrade:brew services restart nginx
Or, if you don't want/need a background service you can just run:/usr/local/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
install-on-request: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
build-error: 0 (30 days)
可以看到Not installed
,需要去安装。
安装nginx
brew install nginx
liaojianya@liaojianyadeMacBook-Pro downloads % brew install nginx
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/pcre2-10.40.big_sur.bottle.tar.gz
##O=# #
curl: (22) The requested URL returned error: 404
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.40
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/6a61595f05721663f0cee49a258476b0c9c9bca499f44f4cb437a1f9cc2e5d2b--pcre2-10.40.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/blobs/sha256:0108a261b51c0c8628eb94fb92a00e33867dccac8b6756a71a24e47f596125c7
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/4077ce3d6665d1a9874a4f6f3a9b92df56eb22e861284bbfc3142b0f80cd0d08--pcre2--10.40.big_sur.bottle.tar.gz
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/nginx-1.21.6_1.big_sur.bottle.tar.gz
##O=# #
curl: (22) The requested URL returned error: 404
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.21.6_1
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/4fb2767b05e700f1cd175f9f7ac8ce64a15a9f87c8cf8ff9b9897aea93965bce--nginx-1.21.6_1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:4b6822266d64a0aa1baea76f01ead5ec163a90311747fa624cf9f234a7ee41b6
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/977e001977161d0a0bdb1d798b4da0b83965b682cc4f8ab412a7bb5ab9d80e29--nginx--1.21.6_1.big_sur.bottle.tar.gz
==> Installing dependencies for nginx: pcre2
==> Installing nginx dependency: pcre2
==> Pouring pcre2-10.40.big_sur.bottle.tar.gz
Error: No such file or directory @ rb_sysopen - /Users/liaojianya/Library/Caches/Homebrew/downloads/de3e1c15aa2f708af57eb6c8309c36b834615fef7fdd35239e69dac0998e32ca--pcre2-10.40.big_sur.bottle.tar.gz
报错信息:Error: No such file or directory @ rb_sysopen - /Users/liaojianya/Library/Caches/Homebrew/downloads/de3e1c15aa2f708af57eb6c8309c36b834615fef7fdd35239e69dac0998e32ca--pcre2-10.40.big_sur.bottle.tar.gz
解决方案:brew install pcre2
liaojianya@liaojianyadeMacBook-Pro downloads % brew install pcre2
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/pcre2-10.40.big_sur.bottle.tar.gz
#=#=-# #
curl: (22) The requested URL returned error: 404
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.40
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/6a61595f05721663f0cee49a258476b0c9c9bca499f44f4cb437a1f9cc2e5d2b--pcre2-10.40.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/blobs/sha256:0108a261b51c0c8628eb94fb92a00e33867dccac8b6756a71a24e47f596125c7
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/4077ce3d6665d1a9874a4f6f3a9b92df56eb22e861284bbfc3142b0f80cd0d08--pcre2--10.40.big_sur.bottle.tar.gz
==> Pouring pcre2--10.40.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/pcre2/10.40: 230 files, 6.4MB
==> Running `brew cleanup pcre2`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
再次安装nginx:brew install nginx
liaojianya@liaojianyadeMacBook-Pro downloads % brew install nginx
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/nginx-1.21.6_1.big_sur.bottle.tar.gz
##O#- #
curl: (22) The requested URL returned error: 404
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.21.6_1
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/4fb2767b05e700f1cd175f9f7ac8ce64a15a9f87c8cf8ff9b9897aea93965bce--nginx-1.21.6_1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:4b6822266d64a0aa1baea76f01ead5ec163a90311747fa624cf9f234a7ee41b6
Already downloaded: /Users/liaojianya/Library/Caches/Homebrew/downloads/977e001977161d0a0bdb1d798b4da0b83965b682cc4f8ab412a7bb5ab9d80e29--nginx--1.21.6_1.big_sur.bottle.tar.gz
==> Pouring nginx--1.21.6_1.big_sur.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/wwwThe default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.nginx will load all files in /usr/local/etc/nginx/servers/.To restart nginx after an upgrade:brew services restart nginx
Or, if you don't want/need a background service you can just run:/usr/local/opt/nginx/bin/nginx -g daemon off;
==> Summary
🍺 /usr/local/Cellar/nginx/1.21.6_1: 26 files, 2.2MB
==> Running `brew cleanup nginx`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
再次查看nginx信息
brew info nginx
liaojianya@liaojianyadeMacBook-Pro downloads % brew info nginx
nginx: stable 1.21.6 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.21.6_1 (26 files, 2.2MB) *Poured from bottle on 2023-06-20 at 09:12:22
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@1.1 ✔, pcre2 ✔
==> Options
--HEADInstall HEAD version
==> Caveats
Docroot is: /usr/local/var/wwwThe default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.nginx will load all files in /usr/local/etc/nginx/servers/.To restart nginx after an upgrade:brew services restart nginx
Or, if you don't want/need a background service you can just run:/usr/local/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
install-on-request: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
build-error: 0 (30 days)
其中:
- 安装目录信息:
/usr/local/Cellar/nginx/1.21.6_1 (26 files, 2.2MB) *
- 安装来源信息:
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/nginx.rb
- 根目录信息:
Docroot is: /usr/local/var/www
- 配置文件信息及默认端口信息:
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo.
查看下载文件
liaojianya@liaojianyadeMacBook-Pro downloads % ls
4077ce3d6665d1a9874a4f6f3a9b92df56eb22e861284bbfc3142b0f80cd0d08--pcre2--10.40.big_sur.bottle.tar.gz 6a61595f05721663f0cee49a258476b0c9c9bca499f44f4cb437a1f9cc2e5d2b--pcre2-10.40.bottle_manifest.json
4fb2767b05e700f1cd175f9f7ac8ce64a15a9f87c8cf8ff9b9897aea93965bce--nginx-1.21.6_1.bottle_manifest.json 977e001977161d0a0bdb1d798b4da0b83965b682cc4f8ab412a7bb5ab9d80e29--nginx--1.21.6_1.big_sur.bottle.tar.gz
启动nginx服务
brew services start nginx
liaojianya@liaojianyadeMacBook-Pro downloads % open /usr/local/etc/nginx/
liaojianya@liaojianyadeMacBook-Pro downloads % open /usr/local/Cellar/nginx
liaojianya@liaojianyadeMacBook-Pro downloads % brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
验证安装效果
查看nginx进程
ps -ef | grep nginx
liaojianya@liaojianyadeMacBook-Pro nginx % ps -ef | grep nginx501 30392 1 0 9:14AM ?? 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off; 501 30395 30392 0 9:14AM ?? 0:00.00 nginx: worker process
访问nginx首页
访问http://localhost:8080
地址
修改配置文件
- 编辑nginx配置文件
vim nginx.conf
修改端口号为8081 - 重启nginx服务
重启命令:brew services restart nginx
liaojianya@liaojianyadeMacBook-Pro nginx % brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx**)**
- 再次访问nginx首页
访问http://localhost:8081
地址
配置静态资源访问
创建图片路径
# 临时创建photo文件夹
liaojianya@liaojianyadeMacBook-Pro ~ % mkdir photo
liaojianya@liaojianyadeMacBook-Pro ~ % cd photo
liaojianya@liaojianyadeMacBook-Pro photo % pwd
/Users/liaojianya/photo
# 拷贝一张图片到该目录下
liaojianya@liaojianyadeMacBook-Pro photo % ls
111.jpeg
修改nginx配置文件
vim nginx.conf
,在server块中增加
location ~* \.(gif|jpg|jpeg|png)$ {root /Users/liaojianya/photo/;}
验证nginx.conf配置文件正确性
使用nginx -t
命令
liaojianya@liaojianyadeMacBook-Pro nginx % nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
重启nginx服务
使用命令brew services restart nginx
重启
liaojianya@liaojianyadeMacBook-Pro nginx % brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
访问静态图片
在浏览器中打开地址:http://localhost:8081/111.jpeg
至此nginx安装验证完毕。