CentOS 7 安装 Tomcat

rpm -qa | grep java
rpm -e --nodeps java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64
rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.141-1.b16.el7_3.x86_64
rpm -e --nodeps java-1.7.0-openjdk-1.7.0.141-2.6.10.1.el7_3.x86_64
rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.141-2.6.10.1.el7_3.x86_64

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

wget http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jdk-8u144-linux-x64.rpm
rpm -ivh jdk-8u144-linux-x64.rpm

vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_144
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${PATH}

source /etc/profile

java -version




http://tomcat.apache.org/download-80.cgi
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
tar -xvzf apache-tomcat-8.5.20.tar.gz
mv apache-tomcat-8.5.20 /usr/local/

启动Tomcat:
/usr/local/apache-tomcat-8.5.20/bin/startup.sh
关闭Tomcat:
/usr/local/apache-tomcat-8.5.20/bin/shutdown.sh

为Tomcat添加服务:
在Tomcat的安装目录下的bin目录下增加setenv.sh文件:
vi /usr/local/apache-tomcat-8.5.20/bin/setenv.sh
#set JAVA_HOME and JRE_HOME and CATALINA_HOME and CATALINA_BASE
export JAVA_HOME=/usr/java/jdk1.8.0_144
export JRE_HOME=/usr/java/jdk1.8.0_144/jre
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.20
export CATALINA_BASE=/usr/local/apache-tomcat-8.5.20
#set JAVA_HOME and JRE_HOME and CATALINA_HOME and CATALINA_BASE

#add tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#add Java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"


创建tomcat.service文件:
vi /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat 8.5.20
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/apache-tomcat-8.5.20/tomcat.pid
ExecStart=/usr/local/apache-tomcat-8.5.20/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


然后可以执行如下命令来检验是否配置正确:
systemctl enable tomcat.service
systemctl disable tomcat.service
systemctl start tomcat.service
systemctl stop tomcat.service
systemctl restart tomcat.service

出现403 Access Denied
By default the Host Manager is only accessible from a browser running on the same machine as Tomcat
vi /usr/local/apache-tomcat-8.5.20/webapps/manager/META-INF/context.xml


vi /usr/local/apache-tomcat-8.5.20/conf/tomcat-users.xml




systemctl restart tomcat

管理页面:
http://127.0.0.1:8080

代码部署:
1.利用Tomcat自动部署
将WebSitexxx目录拷贝到$CATALINA_HOME/webapps下
/usr/local/apache-tomcat-8.5.20/webapps/WebSitexxx/
访问地址如下:http://localhost:8080/WebSitexxx/

2.修改Server.xml文件部署
更改$CATALINA_HOME/conf/server.xml文件,
vi /usr/local/apache-tomcat-8.5.20/conf/server.xml
编辑Host节点: