Tomcat的安装非常简单,这里介绍几个简单的配置。
配置文件目录:
[root@cp4 tomcat]# cd /usr/local/tomcat/conf/
[root@cp4 conf]# ls
Catalina catalina.properties logging.properties tomcat-users.xml
catalina.policy context.xml server.xml web.xml
配置1:更改端口8080为80
编辑配置文件:
[root@cp4 conf]# vim server.xml
把<Connector port="8080" >这个模块的port改为80:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
重启tomcat,先stop,后start:
[root@cp4 conf]# vim server.xml
[root@cp4 conf]# /etc/init.d/tomcat stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@cp4 conf]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看端口:(已经成功改成80)
[root@cp1 conf]# netstat -lnp | grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2045/java
tcp 0 0 :::8009 :::* LISTEN 2045/java
tcp 0 0 :::80 :::* LISTEN 2045/java
配置2:添加虚拟主机,根目录为/data/tomcatweb:
创建网站根目录:
[root@cp4 conf]# mkdir -p /data/tomcatweb
编辑配置文件:
[root@cp4 conf]# vim server.xml
找到</Host>,在下面插入新的Host内容如下(参考模板:):
<Host appBase="/data/tomcatweb"
unpackWARs="false" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="./" reloadable="true" crossContext="true"/>
</Host>
配置好后重启tomcat:
[root@cp4 conf]# /etc/init.d/tomcat stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@cp4 conf]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
在网站根目录下随便写点东西:
[root@cp4 conf]# echo "This is a test file" > /data/tomcatweb/1.txt
测试访问结果:
[root@cp4 conf]# curl -xlocalhost:80
This is a test file for jsp
测试jsp解析:
[root@cp4 conf]# vim /data/tomcatweb/1.jsp
<html>
<body>
<center>
Now time is: <%=new java.util.Date()%>
</center>
</body>
</html>
测试访问结果:
[root@cp4 conf]# curl -xlocalhost:80
<html>
<body>
<center>
Now time is: Sun Mar 12 00:59:48 CST 2017
</center>
</body>
</html>
如果解析出Date()函数显示时间则表示我们配置的虚拟主机成功了。
更多Tomcat相关教程见以下内容: