Tomcat在Centos部署

建立用戶

sudo useradd --system --home /opt/tomcat --shell /sbin/nologin --comment "tomcat user" --user-group tomcat

部署tomcat

解壓安裝包:html

tar -xvf apache-tomcat-9.0.21.tar.gz

移動解壓包:java

sudo mv apache-tomcat-9.0.21 /opt/tomcat/apache-tomcat-9.0.21

建立軟連接:linux

sudo ln -s /opt/tomcat/apache-tomcat-9.0.21 /opt/tomcat/latest

分配目錄用戶者權限:web

sudo chown -R tomcat: /opt/tomcat

分配tomcat的bin目錄執行權限:shell

sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

配置systemd

檢查jvm可以使用的最大內存:apache

java -Xms16G -Xmx16G -version

配置systemd配置文件:vim

vi /etc/systemd/system/tomcat.service

內容以下:centos

[Unit]
Description=Tomcat 9 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms16G -Xmx16G -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

從新加載配置文件:tomcat

sudo systemctl daemon-reload

啓用配置,開啓tomcat服務:安全

sudo systemctl enable tomcat && sudo systemctl start tomcat

查看服務狀態:

sudo systemctl status tomcat

配防火牆

開放80端口

sudo firewall-cmd --zone=public --permanent --add-port=80/tcp

轉發80端口到8080端口(小於1024的端口,非root用戶不能訪問,這裏使用防火牆的轉發端口功能解決這個問題):

sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080

重載防火牆配置:

sudo firewall-cmd --reload

安全

刪除多餘頁面:

cd /opt/tomcat/latest/webapps
rm -rf docs examples host-manager manager ROOT

隱藏tomcat版本號:

vim /opt/tomcat/latest/conf/server.xml
# 添加一行:
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false"/>

參考

相關文章
相關標籤/搜索