序言: 在學習Tomcat時, 部署虛擬服務主機時,遇到了無響應的情況.原以為是應為Tomcat默認端口8080在調整至(進行端口轉發設置)默認端口80會和Mac自帶Apache起沖突.但是也有同學使用Mac卻得到響應.這里筆者重新記錄一下.
1 Tomcat的安裝
- 進入阿帕奇官網http://tomcat.apache.org/ 如下圖(最好點擊Which version 判斷你需要哪一個版本)
2. 筆者使用8.5.23版本 兼容Java7以及之后的版本(由於先在大部分情況下公司使用Java6 or Java7)
點擊 Core下的tar.gz 開始下載
3.將下載好的文件該名稱為Tomcat方便后續使用
4.進入Mac下的資源庫(/Library/) 將文件夾放到該處
5 進入終端(Terminal) 輸入 以下命令行
~$ cd /Library/Tomcat/bin ~$ ./startup.sh
./startup.sh表示開啟Tomcat服務器
此時在頁面地址欄輸入localhost:8080(127.0.0.1 Tomcat默認端口是8080)會出現可愛的小貓即Tomcat頁面
./shutdown.sh表示關閉服務器
注意:由於Mac是Unix,同Linux一樣1024以下端口是沒有監聽權限的,當將Tomcat設置為默認80端口是無法響應的
2 設置默認80端口
mac下8080端口到80端口的轉發
MAC OS 本質上還是 Unix 系統, Unix 系統大多默認情況下非root用戶是無法使用小於1024的常用端口的.這時候如果你開發中需要在普通用戶下用到80端口, 比如 tomcat, 比如 vitualbox 下構建了一個 http 服務, 若你想直接通過 瀏覽器的 localhost 訪問的話(不用加上莫名其妙的”:端口”的話)你就需要做一些系統端口轉發的工作.
MAC OS 10.10 上 pfctl 就可以做這一件事情, 詳情請參見
1、創建文件 :
sudo vim /etc/pf.anchors/eclipse.tomcat.forwarding
文件內容 :
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 443 -> 127.0.0.1 port 8443
2、創建文件 :
sudo vim /etc/pf-tomcat.conf
文件內容 :
rdr-anchor "forwarding"
load anchor "forwarding"from "/etc/pf.anchors/eclipse.tomcat.forwarding"
3、啟動
sudo pfctl -ef /etc/pf-tomcat.conf
執行結果
復制代碼
root@ymdeMacBook-Air etc# sudo pfctl -ef /etc/pf-tomcat.conf
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
復制代碼
假設你的工程 http://localhost/myDemo.html 這時訪問你的web工程, 你會發現 http://localhost/myDemo.html 這個不需要8080端口即可訪問,加上 http://localhost:8080/myDemo.html 訪問不了。
4、關閉
sudo pfctl -d
執行結果
root@ymdeMacBook-Air etc# sudo pfctl -d
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
這時你訪問你的web工程, 你會發現 http://localhost/myDemo.html 這個訪問不了,加上 http://localhost:8080/myDemo.html 即可訪問。
或者全部關閉
pfctl -F all -f /etc/pf.conf
然並卵 本人依然無法使用80端口 localhost顯示 it works 是Apache服務器使用着
輸入以下代碼 查看80端口的占用情況
~$ lsof -i:80
SiegdeMacBook-Pro:bin Sieg$ lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Google 270 Sieg 63u IPv4 0x2fa1ca9090a3144f 0t0 TCP 192.168.1.101:63417->47.95.49.160:http (ESTABLISHED) Google 270 Sieg 64u IPv4 0x2fa1ca909bced777 0t0 TCP 192.168.1.101:63325->117.122.217.19:http (ESTABLISHED) Google 270 Sieg 95u IPv4 0x2fa1ca90883bd777 0t0 TCP 192.168.1.101:63386->117.122.217.19:http (ESTABLISHED) Google 270 Sieg 124u IPv4 0x2fa1ca90883b9777 0t0 TCP 192.168.1.101:62863->58.218.215.168:http (ESTABLISHED) Google 270 Sieg 133u IPv4 0x2fa1ca9090a2f06f 0t0 TCP 192.168.1.101:63080->58.218.215.161:http (ESTABLISHED) Google 270 Sieg 134u IPv4 0x2fa1ca90909d544f 0t0 TCP 192.168.1.101:63081->58.218.215.161:http (ESTABLISHED) Google 270 Sieg 136u IPv4 0x2fa1ca90883f344f 0t0 TCP 192.168.1.101:63422->101.201.169.146:http (ESTABLISHED) Google 270 Sieg 137u IPv4 0x2fa1ca909bd94777 0t0 TCP 192.168.1.101:63423->47.95.49.160:http (ESTABLISHED) Google 270 Sieg 142u IPv4 0x2fa1ca909c2ba967 0t0 TCP 192.168.1.101:63424->47.95.49.160:http (ESTABLISHED) Google 270 Sieg 145u IPv4 0x2fa1ca909bd69e7f 0t0 TCP 192.168.1.101:62869->58.218.215.161:http (ESTABLISHED) Google 270 Sieg 151u IPv4 0x2fa1ca909c230777 0t0 TCP 192.168.1.101:63427->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 152u IPv4 0x2fa1ca90909d144f 0t0 TCP 192.168.1.101:63428->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 153u IPv4 0x2fa1ca909bd7306f 0t0 TCP 192.168.1.101:63429->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 159u IPv4 0x2fa1ca909bda844f 0t0 TCP 192.168.1.101:63430->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 174u IPv4 0x2fa1ca9090eebe7f 0t0 TCP 192.168.1.101:62984->58.218.215.161:http (ESTABLISHED) Google 270 Sieg 185u IPv4 0x2fa1ca909bd20b57 0t0 TCP 192.168.1.101:63432->124.14.23.249:http (ESTABLISHED) Google 270 Sieg 186u IPv4 0x2fa1ca9090eaf25f 0t0 TCP 192.168.1.101:63433->124.14.23.249:http (ESTABLISHED) Google 270 Sieg 187u IPv4 0x2fa1ca909bd70b57 0t0 TCP 192.168.1.101:63434->124.14.23.249:http (ESTABLISHED) Google 270 Sieg 188u IPv4 0x2fa1ca909bdb0e7f 0t0 TCP 192.168.1.101:63436->61.135.186.152:http (CLOSE_WAIT) Google 270 Sieg 189u IPv4 0x2fa1ca909bd13b57 0t0 TCP 192.168.1.101:63435->61.135.162.115:http (CLOSE_WAIT) Google 270 Sieg 192u IPv4 0x2fa1ca908834125f 0t0 TCP 192.168.1.101:62906->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 193u IPv4 0x2fa1ca909be1644f 0t0 TCP 192.168.1.101:62905->58.218.215.161:http (ESTABLISHED) Google 270 Sieg 194u IPv4 0x2fa1ca9090a38b57 0t0 TCP 192.168.1.101:62907->124.14.23.249:http (ESTABLISHED) Google 270 Sieg 201u IPv4 0x2fa1ca909bd19777 0t0 TCP 192.168.1.101:63084->58.218.215.168:http (ESTABLISHED) Google 270 Sieg 206u IPv4 0x2fa1ca909bd2c25f 0t0 TCP 192.168.1.101:63089->117.122.217.6:http (ESTABLISHED) Google 270 Sieg 239u IPv4 0x2fa1ca9090ef106f 0t0 TCP 192.168.1.101:62989->124.14.14.215:http (ESTABLISHED) lantern 442 Sieg 37u IPv4 0x2fa1ca909c22a25f 0t0 TCP 192.168.1.101:50602->a23-41-75-27.deploy.static.akamaitechnologies.com:http (ESTABLISHED) lantern 442 Sieg 42u IPv4 0x2fa1ca909c0de44f 0t0 TCP 192.168.1.101:65367->a23-41-75-27.deploy.static.akamaitechnologies.com:http (ESTABLISHED) lantern 442 Sieg 47u IPv4 0x2fa1ca9090ef4777 0t0 TCP 192.168.1.101:62995->101.201.170.241:http (ESTABLISHED)
- 筆者關閉了lantern 並關閉了Apache服務器
關閉Apache
~$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
啟動Apache
~$ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
- 關閉Apache之后,查看80端口 現在已經沒有占用情況了
重新設置80端口轉發
~$ sudo pfctl -ef /etc/pf-tomcat.conf
再去瀏覽器輸入localhost看看
成功了!!!