mac 下nginx 安装、卸载、重装,mac下nginx 无法使用80端口

brew又叫Homebrew,

是Mac中的一款软件包管理工具,通过brew可以很方便的在Mac中安装软件或者是卸载软件.
一般Mac电脑会默认安装有brew.
常用指令如下:

brew 搜索软件 

➜  ~ brew search nginx
==> Formulae
nginx ✔


brew 安装软件
brew install nginx

sudo sudo brew install nginx             
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
➜  ~ sudo chown -R $(whoami) /usr/local/Cellar
➜  ~  sudo brew install nginx             
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
➜  ~ brew install nginx 
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (caskroom/cask, homebrew/cask and homebrew/core).
==> New Formulae
austin
==> Updated Formulae
commandbox             mosquitto              pastel                 uriparser
==> Updated Casks
insomnia               macdown                openlp                 path-finder
insomnia               nrlquaker-winbox       openlp                 qutebrowser
macdown                nrlquaker-winbox       path-finder            qutebrowser

Warning: You are using macOS 10.12.
We (and Apple) do not provide support for this old version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Discourse, Twitter or IRC. You are responsible for resolving any issues you
experience while you are running this old version.

==> Downloading https://nginx.org/download/nginx-1.19.0.tar.gz
Already downloaded: /Users/liuyulong/Library/Caches/Homebrew/downloads/d69627dfd1bf017217a448acd00ba1917ea9b2c4a7a39ea0f100ecf20cdf2ce2--nginx-1.19.0.tar.gz
==> tar -cJf /usr/local/Cellar/nginx/1.19.0/share/nginx/src/src.tar.xz --options compressio
==> ./configure --prefix=/usr/local/Cellar/nginx/1.19.0 --sbin-path=/usr/local/Cellar/nginx
==> make install
==> Caveats
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.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd 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:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.19.0: 10 files, 2MB, built in 45 seconds
➜  ~ cd /usr/local/etc/nginx/servers/

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

这句话可以看出到配置文件 路径  默认位置

 /usr/local/etc/nginx/nginx.conf


brew 卸载软件
 

brew uninstall nginx


brew 升级
sudo brew update
查看安装信息(经常用到, 比如查看安装目录等)
sudo brew info nginx

 ~ sudo brew info nginx
Password:
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
➜  ~  brew info nginx 
nginx: stable 1.19.0, HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.19.0 (10 files, 2MB) *
  Built from source on 2020-06-02 at 06:09:11
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb
==> Dependencies
Required: openssl@1.1 ✔, pcre ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
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.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd 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:
  nginx
==> Analytics
install: 32,842 (30 days), 107,792 (90 days), 408,811 (365 days)
install-on-request: 31,865 (30 days), 104,249 (90 days), 393,454 (365 days)
build-error: 0 (30 days)
  1. 安装nginx

  2. sudo brew services start nginx

  3. 查看nginx版本

    nginx -v

  4. 关闭nginx服务

    sudo brew services stop nginx

  5. 重新加载nginx

    nginx -s reload
  6. 停止nginx
    1. nginx -s stop

查看已经安装的软件
brew list 

还有mac 下出现了8080端口被占用,特别奇怪,查了好多

 ~ vim /usr/local/etc/nginx/nginx.conf  

➜  ~ sudo nginx -s reload 

 

1、使用lsof -i:80查看当前占用80端口的进程,如果有就kill掉。

2、关闭mac自带apache的启动。

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

如果哪天你想让它开机启动了,则将unload 改为 load:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

3、也是最常见的一条,mac禁止了普通用户访问1024以下的端口,包括80端口。想要通过80端口访问则需要通过端口转发。命令如下:

sudoipfw add fwd 127.0.0.1,1081 tcp from any to 127.0.0.1 80 in

大致意思是做端口转发,80端口打到1081上,这样web服务都指向了nginx的1081(相当于原来的80端口)

关于ipfw命令:

a、查看规则:sodu ipfw list

localhost:sbin lq$ sudo ipfw list

00100 fwd 127.0.0.1,1081 tcp fromany to 127.0.0.1 dst-port 80 in

65535 allow ip from any to any

b、删除规则:sodu ipfw delete 100【删除的规则是:00100 fwd 127.0.0.1,1081 tcp from any to 127.0.0.1 dst-port 80 in】

c、新增规则:sudo ipfw add fwd 127.0.0.1,1081tcp from any to 127.0.0.1 80 in

 

mac 下nginx加入开机启动
通过brew install nginx后设置开机启动项

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值