Tomcat deployment in Centos

Keywords: Programming Tomcat sudo firewall Apache

Create user

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

Deploying tomcat

Unzip the installation package:

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

Mobile decompression package:

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

To create a soft link:

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

Assign directory user permissions:

sudo chown -R tomcat: /opt/tomcat

Assign tomcat's bin directory execution permission:

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

Configure systemd

Check the maximum memory that the jvm can use:

java -Xms16G -Xmx16G -version

To configure the systemd configuration file:

vi /etc/systemd/system/tomcat.service

The contents are as follows:

[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

Reload profile:

sudo systemctl daemon-reload

To enable configuration and tomcat service:

sudo systemctl enable tomcat && sudo systemctl start tomcat

View service status:

sudo systemctl status tomcat

Firewalls

Open 80 ports

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

Forward port 80 to port 8080 (Port less than 1024 can not be accessed by non root users. Here, use the forward port function of firewall to solve this problem):

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

Reload firewall configuration:

sudo firewall-cmd --reload

Reference resources

Posted by xadmin on Tue, 29 Oct 2019 08:44:19 -0700