Centos7 安装 Tomcat

环境:

CentOS 7

JDK 1.8

1、下载tomcat

登陆网站 “http://tomcat.apache.org/”,下载自己所需要的版本

以下示例以下载“apache-tomcat-9.0.0.M26.tar.gz” 为例。

 

2、添加安装目录并解压

假设:下载文件存放路径“~/apache-tomcat-9.0.0.M26.tar.gz”

# cd ~

# tar zxf apache-tomcat-9.0.0.M26.tar.gz

# mkdir /usr/tomcat

# mv apache-tomcat-9.0.0.M26  /usr/tomcat

 

3、检查安装是否成功

# cd /usr/tomcat/apache-tomcat-9.0.0.M26

# cd bin

# ./startup.sh

Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-9.0.0.M26
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-9.0.0.M26
...
Tomcat started.

通过检查端口默认8080是否存在监听,确认tomcat启动成功

# telnet localhost 8080

成功显示

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

不成功需要检查安装目录日志文件“catalina.*.log”,是否存在异常,如果不存在log文件,可通过关闭命令查看具体错误输出,如

[root@master bin]# ./shutdown.sh
Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-9.0.0.M26
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-9.0.0.M26
Using CATALINA_TMPDIR: /usr/tomcat/apache-tomcat-9.0.0.M26/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_131/jre
Using CLASSPATH:       /usr/tomcat/apache-tomcat-9.0.0.M26/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-9.0.0.M26/bin/tomcat-juli.jar
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=64M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
MaxTenuringThreshold of 31 is invalid; must be between 0 and 15
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

则需要根据提示修正参数

4、开机启动

在catalina.sh文件,设置pid,安装根目录

# cd  /usr/tomcat/apache-tomcat-9.0.0.M26

# cd  bin

# vi catalina.sh

 添加“ CATALINA_PID="$CATALINA_BASE/tomcat.pid" ”,如下

# Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"

CATALINA_PID="$CATALINA_BASE/tomcat.pid"

设置pid 文件在tomcat根目录

 

添加系统开机启动配置项

#  vi /etc/systemd/system/tomcat.service

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking

Environment="JAVA_HOME=/usr/java/jdk1.8.0_144"

PIDFile=/usr/tomcat/apache-tomcat-9.0.0.M26/tomcat.pid
ExecStart=/usr/tomcat/apache-tomcat-9.0.0.M26/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


# systemctl daemon-reload

# systemctl enable tomcat

# systemctl start tomcat

 

 5、优化配置

  • bin/catalina.sh 文件可根据服务器设置JVM运行环境

如在文件开头#注释结束后加以下配置

# OS specific support.  $var _must_ be set to either true or false.

export JAVA_OPTS="-server -Xms256M -Xmx256M -Xss512k -XX:+AggressiveOpts -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=15 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC  -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=64m  -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true "

 

  • conf/server.xml

       可根据自己的需求更改connector配置

 <Connector port="8080" protocol="HTTP/1.1"
       URIEncoding="UTF-8"
       enableLookups="false" disableUploadTimeout="true" connectionTimeout="20000"
       compression="on" compressionMinSize="2048"
       compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
       redirectPort="8443" />

 

 

 参考文章:

http://blog.csdn.net/nimasike/article/details/51896100

 http://blog.csdn.net/lifetragedy/article/details/7708724 

 

posted @ 2017-09-22 19:33  hobinly  阅读(433)  评论(0编辑  收藏