欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mac下安装配置Tomcat 9, Homebrew安装Tomcat

程序员文章站 2022-07-14 10:12:45
...

1. 下载

登录Apache Tomcat官网,地址 http://tomcat.apache.org ,点击左边的Download,选择需要下载的版本。
Mac下安装配置Tomcat 9, Homebrew安装Tomcat

2. 解压,打开Terminal定位到bin目录

解压后的目录名字为apache-tomcat-9.0.31

% cd /Users/zgpeace/Workspace/apache-tomcat-9.0.31/bin 

3. 启动Tomcat

3.1 启动Tomcat的前提是安装好java sdk,先看看本地安装java版本

% java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

3.2 第一次启动会遇到没有权限的问题

% ./startup.sh 
zsh: permission denied: ./startup.sh

3.3 给目录下所有的可执行命令*.sh 添加可执行权限。

% sudo chmod 755 *.sh

3.4对比*.sh与其它执行文件的权限区别

% ll
total 1728
-rw-r--r--@ 1 zgpeace  staff    34K Feb  5 19:33 bootstrap.jar
-rw-r--r--@ 1 zgpeace  staff   1.7K Feb  5 19:33 catalina-tasks.xml
-rw-r--r--@ 1 zgpeace  staff    16K Feb  5 19:33 catalina.bat
-rwxr-xr-aaa@qq.com 1 zgpeace  staff    23K Feb  5 19:34 catalina.sh

权限说明:

  1. 第一个栏位,表示文件的属性。Linux的文件基本上分为三个属性:可读(r),可写(w),可执行(x)。
  2. 紧接着的3*3个字符分3组,各指示此文件的读、写、执行权限,对于ownergroupothers而言。

因为Linux是多用户多任务系统,所以一个文件可能同时被许多人使用,所以我们一定要设好每个文件的权限,其文件的权限位置排列顺序是(以-rwxr-xr-x为例):

 rwx(Owner)r-x(Group)r-x(Other) 

3.5 再次启动Tomcat,成功

% ./startup.sh 
Using CATALINA_BASE:   /Users/zgpeace/Workspace/apache-tomcat-9.0.31
Using CATALINA_HOME:   /Users/zgpeace/Workspace/apache-tomcat-9.0.31
Using CATALINA_TMPDIR: /Users/zgpeace/Workspace/apache-tomcat-9.0.31/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home
Using CLASSPATH:       /Users/zgpeace/Workspace/apache-tomcat-9.0.31/bin/bootstrap.jar:/Users/zgpeace/Workspace/apache-tomcat-9.0.31/bin/tomcat-juli.jar
Tomcat started.

3.6 Chrome访问http://localhost:8080/Mac下安装配置Tomcat 9, Homebrew安装Tomcat
3.7 停止Tomcat

% ./shutdown.sh 
Using CATALINA_BASE:   /Users/zgpeace/Workspace/apache-tomcat-9.0.31
Using CATALINA_HOME:   /Users/zgpeace/Workspace/apache-tomcat-9.0.31
Using CATALINA_TMPDIR: /Users/zgpeace/Workspace/apache-tomcat-9.0.31/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home
Using CLASSPATH:       /Users/zgpeace/Workspace/apache-tomcat-9.0.31/bin/bootstrap.jar:/Users/zgpeace/Workspace/apache-tomcat-9.0.31/bin/tomcat-juli.jar

验证,再次访问链接http://localhost:8080/失败

% curl http://localhost:8080/
curl: (7) Failed to connect to localhost port 8080: Connection refused

4. 运行自己打包的代码

比如从IDEA中导出SpringBoot的war包,放到Tomcat/webapps/下面, 启动Tomcat后会自动加压为文件夹,比如笔者放了demo-0.0.1-SNAPSHOT.war, 启动后自动解压为demo-0.0.1-SNAPSHOTMac下安装配置Tomcat 9, Homebrew安装Tomcat
启动Tomcat后./startup.sh, 访问路径要代码文件夹的名字。结果如下:
Mac下安装配置Tomcat 9, Homebrew安装Tomcat

5. Tomcat的目录结构及作用

  1. |- bin:存放tomcat的命令。

catalina.sh命令:
startup.sh-> catalina.sh start
shutdown.sh- > catalina.sh stop

  1. |- conf:存放tomcat的配置信息。其中server.xml文件是核心的配置文件。

  2. |-lib:支持tomcat软件运行的jar包。其中还有技术支持包,如servlet,jsp

  3. |-logs:运行过程的日志信息

  4. |-temp:临时目录

  5. |-webapps:共享资源目录。web应用目录。(注意不能以单独的文件进行共享)

  6. |-work:tomcat的运行目录。jsp运行时产生的临时文件就存放在这里

  7. |- WebRoot :web应用的根目录

    8.1 |-静态资源(html+css+js+image+vedio)

    8.2 |-WEB-INF:固定写法。

    8.3 |-classes:(可选)固定写法。存放class字节码文件

    8.4 |-lib:(可选)固定写法。存放jar包文件。

    8.5 |-web.xml

    >注意:
    1)WEB-INF目录里面的资源不能通过浏览器直接访问
    2)如果希望访问到WEB-INF里面的资源,就必须把资源配置到一个叫web.xml的文件中
    

6. Homebrew 安装Tomcat

6.1 安装Tomcat指令

brew install tomcat
# 指定版本9
brew install tomcat@9

6.2 如果Hombrew版本太旧,请先升级

% brew -v
Homebrew 2.1.9
Homebrew/homebrew-core (git revision 8cb6a; last commit 2020-02-27)
Homebrew/homebrew-cask (git revision c7f16; last commit 2020-02-27)
9:34 zgpeace@zgpeaces-MBP /Users/zgpeace/Workspace/Tomcat
% brew update
Already up-to-date.

6.3 安装具体细节

% brew install tomcat@9
Updating Homebrew...
==> Installing dependencies for tomcat: openjdk
==> Installing tomcat dependency: openjdk
==> Downloading https://homebrew.bintray.com/bottles/openjdk-13.0.2+8_2.catalina
==> Downloading from https://akamai.bintray.com/65/65adca036393f528e3830cab8b0aa
######################################################################## 100.0%
==> Pouring openjdk-13.0.2+8_2.catalina.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk 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.

If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> Summary
????  /usr/local/Cellar/openjdk/13.0.2+8_2: 631 files, 314.6MB
==> Installing tomcat
==> Downloading https://www.apache.org/dyn/closer.lua?path=tomcat/tomcat-9/v9.0.
==> Downloading from http://mirrors.gigenet.com/apache/tomcat/tomcat-9/v9.0.31/b
######################################################################## 100.0%
==> Caveats
To have launchd start tomcat now and restart at login:
  brew services start tomcat
Or, if you don't want/need a background service you can just run:
  catalina run
==> Summary
????  /usr/local/Cellar/tomcat/9.0.31_1: 642 files, 14.9MB, built in 57 seconds
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/zgpeace/Library/Caches/Homebrew/ios-webkit-debug-proxy--1.8.5.mojave.bottle.tar.gz... (139.7KB)
Removing: /Users/zgpeace/Library/Caches/Homebrew/libtasn1--4.14.mojave.bottle.tar.gz... (120.4KB)
Removing: /Users/zgpeace/Library/Logs/Homebrew/carthage... (64B)
Pruned 2 symbolic links from /usr/local
==> Caveats
==> openjdk
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk 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.

If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> tomcat
To have launchd start tomcat now and restart at login:
  brew services start tomcat
Or, if you don't want/need a background service you can just run:
  catalina run

6.4 启动Tomcat, 访问http://localhost:8080/成功

catalina run

6.5 上面Tomcat启动会有log在控制台,关掉控制台后Tomcat停止。下面方法为优雅的启动、停止Tomca。

#启动
% catalina start
#停止
% catalina stop

6.6 Homebrew services启动、停止Tomcat

#启动
brew services start tomcat
#停止
brew services stop tomcat

参考

https://www.cnblogs.com/kongzhongqijing/p/3488884.html

https://docs.brew.sh/FAQ

https://medium.com/@fahimhossain_16989/installing-apache-tomcat-on-macos-mojave-using-homebrew-28ce039b4b2e