编辑
2023-06-13
Linux
00

目录

Tomcat 部署步骤

Tomcat 部署步骤

  1. 下载 Apache Tomcat

检查JDK是否已配置java -version(先决条件)

```bash cd /home/ctm/software wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.90/bin/apache-tomcat-8.5.90.tar.gz ```

2. 解压并安装 Apache Tomcat

```bash mkdir -p /home/ctm/evn tar xvf apache-tomcat-8*tar.gz -C /home/ctm/evn/tomcat --strip-components=1 ```

3. 设置环境变量

在 `~/.bash_profile` 文件中添加以下内容: ```bash export CATALINA_HOME=/home/ctm/evn/tomcat ``` 并执行以下命令来立即生效: ```bash source ~/.bash_profile ```

4. 创建一个新用户和组

```bash sudo groupadd tomcat sudo useradd -s /bin/false -g tomcat -d /home/ctm/evn/tomcat tomcat ```

5. 更改新创建的 Tomcat 目录的所有权

```bash sudo chown -R tomcat:tomcat /home/ctm/evn/tomcat ```

6. 创建一个新的 systemd 服务单元文件

```bash sudo vim /etc/systemd/system/tomcat.service ``` **填充以下内容:(路径按需调整)** ```ini [Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking User=tomcat Group=tomcat ExecStart=/home/ctm/evn/tomcat/bin/startup.sh ExecStop=/home/ctm/evn/tomcat/bin/shutdown.sh UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target ```

7. 启动并检查服务的状态

```bash sudo systemctl daemon-reload sudo systemctl start tomcat sudo systemctl status tomcat ``` > 也可以通过访问8080端口检查:`http://IP:8080`,出现tomacat介绍页面则表示服务启动成功 > > 如果查看状态报错,启动失败,可以查看详细信息:`sudo journalctl -xe` > > 比如我这个报错就是指启动系统服务被SELinux拦截的问题:
bash
[root@bytter-centos-s-4vcpu-8gb-sgp1-01 bin]# sudo systemctl status tomcat ● tomcat.service - Apache Tomcat Web Application Container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: activating (auto-restart) (Result: exit-code) since Tue 2023-06-13 02:12:16 UTC; 15ms ago Process: 30656 ExecStart=/home/ctm/evn/tomcat/bin/startup.sh (code=exited, status=203/EXEC) 6月 13 02:12:16 bytter-centos-s-4vcpu-8gb-sgp1-01 systemd[1]: Failed to start Apache Tomcat Web Application Container. [root@bytter-centos-s-4vcpu-8gb-sgp1-01 bin]# sudo chown -R tomcat:tomcat /home/ctm/evn/tomcat [root@bytter-centos-s-4vcpu-8gb-sgp1-01 bin]# sudo systemctl start tomcat Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details. [root@bytter-centos-s-4vcpu-8gb-sgp1-01 bin]# sudo systemctl status tomcat ● tomcat.service - Apache Tomcat Web Application Container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: activating (auto-restart) (Result: exit-code) since Tue 2023-06-13 02:12:47 UTC; 5s ago Process: 30750 ExecStart=/home/ctm/evn/tomcat/bin/startup.sh (code=exited, status=203/EXEC) [root@bytter-centos-s-4vcpu-8gb-sgp1-01 bin]# sudo journalctl -xe -- Subject: 进程 /home/ctm/evn/tomcat/bin/startup.sh 无法执行 -- Defined-By: systemd -- Support: https://access.redhat.com/support -- -- 进程 /home/ctm/evn/tomcat/bin/startup.sh 无法被执行并已失败。 -- -- 该进程返回的错误代码为 13。 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 systemd[1]: tomcat.service: Control process exited, code=exited status=203 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 systemd[1]: tomcat.service: Failed with result 'exit-code'. -- Subject: Unit failed -- Defined-By: systemd -- Support: https://access.redhat.com/support -- -- The unit tomcat.service has entered the 'failed' state with result 'exit-code'. 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 systemd[1]: Failed to start Apache Tomcat Web Application Container. -- Subject: tomcat.service 单元已失败 -- Defined-By: systemd -- Support: https://access.redhat.com/support -- -- tomcat.service 单元已失败。 -- -- 结果为“failed”。 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 setroubleshoot[26304]: AnalyzeThread.run(): Cancel pending alarm 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 setroubleshoot[26304]: SELinux is preventing /usr/lib/systemd/systemd from execute access on the file startup.> 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 setroubleshoot[26304]: SELinux is preventing /usr/lib/systemd/systemd from execute access on the file startup.> ***** Plugin catchall (100. confidence) suggests ************************** If you believe that systemd should be allowed execute access on the startup.sh file by> Then you should report this as a bug. You can generate a local policy module to allow this access. Do allow this access for now by executing: # ausearch -c '(artup.sh)' --raw | audit2allow -M my-artupsh # semodule -X 300 -i my-artupsh.pp 6月 13 02:12:57 bytter-centos-s-4vcpu-8gb-sgp1-01 setroubleshoot[26304]: AnalyzeThread.run(): Set alarm timeout to 10 6月 13 02:12:58 bytter-centos-s-4vcpu-8gb-sgp1-01 sudo[30824]: root : TTY=pts/1 ; PWD=/home/ctm/evn/tomcat/bin ; USER=root ; COMMAND=/bin/journalctl -xe 6月 13 02:12:58 bytter-centos-s-4vcpu-8gb-sgp1-01 sudo[30824]: pam_systemd(sudo:session): Cannot create session: Already running in a session or user slice 6月 13 02:12:58 bytter-centos-s-4vcpu-8gb-sgp1-01 sudo[30824]: pam_unix(sudo:session): session opened for user root by (uid=0) lines 2686-2725/2725 (END)
  1. 如果遇到 SELinux 问题

    您可以暂时禁用 SELinux,但这应该被视为临时解决方案:

    bash
    sudo setenforce 0

    然后尝试再次启动您的服务:

    bash
    sudo systemctl start tomcat sudo systemctl status tomcat
  2. 如果服务启动正常,记得重新开启 SELinux

    bash
    sudo setenforce 1
  3. 关闭系统自启动

如果不想自启动,改为手动启动模式,则只需要关闭系统注册的tomcat服务

```bash # 禁用 sudo systemctl disable tomcat # 启用 sudo systemctl enable tomcat ```

手动启动命令如下:

```bash sudo systemctl start tomcat ```
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Golovin

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!