Mac下Tomcat安装&配置&80默认端口设置

序言: 在学习Tomcat时, 部署虚拟服务主机时,遇到了无响应的情况.原以为是应为Tomcat默认端口8080在调整至(进行端口转发设置)默认端口80会和Mac自带Apache起冲突.但是也有同学使用Mac却得到响应.这里笔者重新记录一下.

 1 Tomcat的安装 

  1. 进入阿帕奇官网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看看

成功了!!!

posted @ 2017-10-12 12:44  Kaffee  阅读(5177)  评论(0编辑  收藏  举报