# Tomcat
## 下载
官网地址:[http://tomcat.apache.org](http://tomcat.apache.org)
下载地址:[https://tomcat.apache.org/download-90.cgi](https://tomcat.apache.org/download-90.cgi)
下载地址:[https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz](http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz)
使用 wget 命令下载 Tomcat9 。
```
[root@localhost ~] wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
```
## 在线安装
```
[root@localhost ~] tar xvf apache-tomcat-9.0.34.tar.gz -C /opt
```
## 配置
### 管理员密码
打开并编辑配置文件“/opt/tomcat9/conf/tomcat-users.xml”,在“”之前添加入下配置信息。
``` xml
```
配置截图如下:

manager默认只允许本地访问,要支持远程访问必须修改远程访问配置。打开并编辑配置文件“/opt/tomcat9/webapps/manager/META-INF/context.xml”。修改允许远程访问的PC的地址为“\d+\.\d+\.\d+\.\d+”。

### 测试远程配置
打开浏览器在浏览器地址栏输入Tomcat服务器地址与,“http://服务器地址:8080”:

点击“Manager App”按钮,管理服务器上部署的应用。

输入在配置文件“tomcat-users.xml”中配置的用户名“tomcat”,密码“123456”,点击“登录”按钮。

### 配置成系统服务
创建文件“/etc/systemd/system/tomcat.service”。
```
[Unit]
Description=tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/opt/tomcat9/
ExecStart=/opt/tomcat9/bin/startup.sh
ExecStop=/opt/tomcat9/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
启动tomcat
```
[root@localhost ~] systemctl start tomcat
```
停止tomcat
```
[root@localhost ~] systemctl stop tomcat
```
设置开机重启tomcat
```
[root@localhost ~] systemctl enable tomcat
```
取消开机重启tomcat
```
[root@localhost ~] systemctl disable tomcat
```
### 自定义读取yml文件配置
打开/opt/tomcat9/bin 新建文件名为setenv.sh

文件内容 active=prod 等号后面是读取的 后置为prod 的yml文件
```
export CATALINA_OPTS="$CATALINA_OPTS -Dspring.profiles.active=prod"
```

## 常见问题处理