安装JDK并配置Tomcat

Tomcat是轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。实际上Tomcat 部分是Apache服务器的扩展,但它是独立运行的,所以当你运行tomcat时,它实际上作为一个与Apache 独立的进程单独运行的。

一、安装JDK

1、卸载系统安装的jdk

[root@daixuan ~]# java -version

java version "1.7.0_79"

OpenJDK Runtime Environment (rhel-2.5.5.4.el6-i386 u79-b14)

OpenJDK Server VM (build 24.79-b02, mixed mode)

[root@daixuan src]# which java

/usr/bin/java

[root@daixuan src]# rpm -qf /usr/bin/java

file /usr/bin/java is not owned by any package

[root@daixuan src]# yum list | grep jdk  //查看到之前系统安装的jdk1.6和jdk1.7

file:///mnt/repodata/repomd.xml: [Errno 14] Could not open/read file:///mnt/repodata/repomd.xml

尝试其他镜像。

java-1.6.0-openjdk.i686 1:1.6.0.35-1.13.7.1.el6_6 @anaconda-CentOS-201508042139.i386/6.7

java-1.6.0-openjdk-devel.i686 1:1.6.0.35-1.13.7.1.el6_6@anaconda-CentOS-201508042139.i386/6.7

java-1.7.0-openjdk.i686 1:1.7.0.79-2.5.5.4.el6@anaconda-CentOS-201508042139.i386/6.7

java-1.7.0-openjdk-devel.i686 1:1.7.0.79-2.5.5.4.el6@anaconda-CentOS-201508042139.i386/6.7

java-1.6.0-openjdk.i686                  1:1.6.0.37-1.13.9.4.el6_7    updates

java-1.6.0-openjdk-demo.i686               1:1.6.0.37-1.13.9.4.el6_7    updates

java-1.6.0-openjdk-devel.i686              1:1.6.0.37-1.13.9.4.el6_7    updates

java-1.6.0-openjdk-javadoc.i686             1:1.6.0.37-1.13.9.4.el6_7    updates

java-1.6.0-openjdk-src.i686                1:1.6.0.37-1.13.9.4.el6_7    updates

java-1.7.0-openjdk.i686                  1:1.7.0.91-2.6.2.2.el6_7     updates

java-1.7.0-openjdk-demo.i686               1:1.7.0.91-2.6.2.2.el6_7     updates

java-1.7.0-openjdk-devel.i686              1:1.7.0.91-2.6.2.2.el6_7     updates

java-1.7.0-openjdk-javadoc.noarch            1:1.7.0.91-2.6.2.2.el6_7     updates

java-1.7.0-openjdk-src.i686            1:1.7.0.91-2.6.2.2.el6_7     updates

卸载1.6jdk和1.7jdk

[root@daixuan src]# yum remove java-1.6.0-openjdk* java-1.7.0-openjdk*


2、下载jdk1.8并解压

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

注:有些下载地址,linux下载是HTML文件不能解压安装,解决方法是:使用chrome下载,接受许可条款,暂停后或在下载列表中右键获取下载地址

[root@daixuan src]# cd /usr/local/src

注,接受许可条款,在windowd浏览器中点击下载,每个链接地址好像只能下载一次,

[root@daixuan src]#wget (chrome复制的jdk下载地址,注意uname -r查看linux系统是32位或者64位)

[root@daixuan src]# tar -zxvf jdk-8u65-linux-i586.tar.gz\?AuthParam\=1451475118_5e960824c5c45bcb5943c57e877f1ed4

[root@daixuan src]# ls

jdk1.8.0_65

[root@daixuan src]# mv jdk1.8.0_65 /usr/local/


3、设置环境变量

[root@daixuan src]# vim /etc/profile

在末尾输入以下内容:

JAVA_HOME=/usr/local/jdk1.8.0_65/

JAVA_BIN=/usr/local/jdk1.8.0_65/bin

JRE_HOME=/usr/local/jdk1.8.0_65/jre

PATH=$PATH:/usr/local/jdk1.8.0_65/bin:/usr/local/jdk1.8.0_65/jre/bin

CLASSPATH=/usr/local/jdk1.8.0_65/jre/lib:/usr/local/jdk1.8.0_65/lib:/usr/local/jdk1.8.0_65/jre/lib/charsets.jar

export  JAVA_HOME  JAVA_BIN JRE_HOME  PATH  CLASSPATH

保存后使其生效

[root@daixuan src]# source /etc/profile

检测是否生效:

[root@daixuan src]# java -version

java version "1.8.0_65"

Java(TM) SE Runtime Environment (build 1.8.0_65-b17)

Java HotSpot(TM) Server VM (build 25.65-b01, mixed mode)


二、安装tomcat

1、下载tomcat

[root@daixuan src]# wget

http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.67/bin/apache-tomcat-7.0.67.tar.gz


2、解压tomcat

[root@daixuan src]# tar -zxvf apache-tomcat-7.0.67.tar.gz

[root@daixuan src]# mv apache-tomcat-7.0.67 /usr/local/tomcat


3、添加tomcat启动项,并修改启动项配置,启动tomcat

[root@daixuan src]# cp -p /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat

[root@daixuan src]# vim /etc/init.d/tomcat

添加如下信息:

# description: tomcat server init script

# chkconfig:2345 63 37

. /etc/init.d/functions

JAVA_HOME=/usr/local/jdk1.8.0_65/

CATALINA_HOME=/usr/local/tomcat  

保存文件后,添加权限并启动tomcat

[root@daixuan tomcat]# chmod 755 /etc/init.d/tomcat

[root@daixuan tomcat]# chkconfig --add tomcat

[root@daixuan tomcat]# chkconfig tomcat on

[root@daixuan tomcat]# /etc/init.d/tomcat start

或者

[root@daixuan tomcat]# service tomcat start


4、查看tomcat进程并测试tomcat

[root@daixuan tomcat]# ps aux | grep tomcat

root      8268  5.5  4.4 808576 85508 pts/1    Sl   21:08   0:05 /usr/local/jdk1.8.0_65//bin/java 

[root@daixuan tomcat]# netstat -lnp

Proto Recv-Q Send-Q Local Address    Foreign Address   State       PID/Program name

tcp  0    0 ::ffff:127.0.0.1:8005  :::*           LISTEN      8268/java

tcp  0    0 :::8009           :::*           LISTEN      8268/java

tcp  0    0 :::8080           :::*           LISTEN      8268/java

在浏览器中输入http://IP:8080/ 你会看到tomcat的主界面,说明tomcat配置成功

配置Tomcat_配置Tomcat




三、配置tomcat

tomcat默认启动的是8080,如果你想修改为80,则需要修改server.xml文件:

1. 配置tomcat服务的访问端口

[root@daixuan tomcat]# cd /usr/local/tomcat/conf/

[root@daixuan conf]# ls

Catalina         catalina.properties  logging.properties  tomcat-users.xml

catalina.policy  context.xml          server.xml          web.xml

[root@daixuan conf]# vim server.xml         //把8080修改为80

    <Connector port="80" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />


2. 配置新的虚拟主机

[root@daixuan conf]# cd /usr/local/tomcat/conf/

[root@daixuan conf]# vim server.xml   //找到</Host>下一行插入新的<Host>内容如下:

<Host name="www.123.com" appBase="/data/tomcatweb"

    unpackWARs="false" autoDeploy="true"

    xmlValidation="false" xmlNamespaceAware="false">

    <Context path="" docBase="./" debug="0" reloadable="true" crossContext="true"/>

</Host>

[root@daixuan conf]# mkdir -p /data/tomcatweb //新建tomcat指定的目录

[root@daixuan conf]# service tomcat stop

[root@daixuan conf]# service tomcat start


[root@daixuan conf]# netstat -lnp | grep java

tcp   0   0 ::ffff:127.0.0.1:8005   :::*     LISTEN      8827/java

tcp   0   0 :::8009            :::*     LISTEN      8827/java

tcp   0   0 :::80              :::*    LISTEN      8827/java


3、测试tomcat
此时在浏览器中直接输入linux服务器的IP地址:192.168.101.230即可打开tomcat默认网站,原因是将原来的8080端口修改为80端口了


[root@daixuan conf]# vim /data/tomcatweb/111.xtx

[root@daixuan conf]# vim /data/tomcatweb/111.txt

[root@daixuan conf]# curl -xlocalhost:80 www.123.com/111.txt

12345

67890

说明tomcat已经可以解析了

[root@daixuan conf]# vim /data/tomcatweb/111.jsp

<html>

<body>

        <center>

        NOW time is: <%=new java.util.Date()%> //调用了时间函数

</html>

[root@daixuan conf]# curl -xlocalhost:80 www.123.com/111.jsp

<html>

<body>

        <center>

        NOW time is: Wed Dec 30 22:14:11 CST 2015 //显示时间,正常解析了

</html>

在浏览器中输入:http://www.123.com/111.jsp

配置Tomcat_配置Tomcat_02


4、如何登录tomcat呢?


[root@daixuan conf]# pwd

/usr/local/tomcat/conf

[root@daixuan conf]# vim tomcat-users.xml

在<tomcat-users>和</tomcat-users>之间注释或删除掉所有的默认账户和权限,新加一个权限如下:
<tomcat-users>

<role rolename="manager-gui"/>

<user username="tomcat" password="daixuan" roles="manager-gui"/>

</tomcat-users>

保存退出后,重启tomcat,点击Manager app登录管理后台时候,输入用户名tomcat和密码daixuan,即可登录

配置Tomcat_配置Tomcat_03

配置Tomcat_配置Tomcat_04