linux 安装jdk 配置tomcat

Linux(ubuntu)下安装JDK、Tomcat

 

 

一、安装jdk

1)首先以root用户登录进去,在根目录下建立/usr/java的目录,我们将下载的东西都放到该目录下去。

2)参考的https://help.ubuntu.com/community/Java

Manual method
  • oracle java 6 jre 32-bit
 $ wget http://download.oracle.com/otn-pub/java/jdk/6u34-b04/jre-6u34-linux-i586.bin
 $ chmod u+x jre-6u34-linux-i586.bin
 $ ./jre-6u34-linux-i586.bin
 $ sudo mkdir -p /usr/lib/jvm
 $ sudo mv jre1.6.0_34 /usr/lib/jvm/
 $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jre1.6.0_34/bin/java" 1
 $ sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/usr/lib/jvm/jre1.6.0_34/lib/i386/libnpjp2.so" 1
 $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jre1.6.0_34/bin/javaws" 1
or
  • oracle java 6 jdk 64-bit
 $ wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin
 $ chmod u+x jdk-6u45-linux-x64.bin
 $ ./jdk-6u45-linux-x64.bin
 $ sudo mv jdk1.6.0_45 /opt
 $ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.6.0_45/bin/java" 1
 $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk1.6.0_45/bin/javac" 1
 $ sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/opt/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so" 1
 $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/jdk1.6.0_45/bin/javaws" 1

  • IMPORTANT choose the java you installed as default

 $ sudo update-alternatives --config java
 $ sudo update-alternatives --config javac

实际执行下列操作:

 $ wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin
 $ chmod u+x jdk-6u45-linux-x64.bin
 $ ./jdk-6u45-linux-x64.bin
 $ sudo mv jdk1.6.0_45 /opt
 $ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.6.0_45/bin/java" 1
 $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk1.6.0_45/bin/javac" 1
 $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/jdk1.6.0_45/bin/javaws" 1

 $update-alternatives --install /usr/bin/java  java /opt/jdk1.6.0_45/bin/java 300
 $update-alternatives --install /usr/bin/javac javac /opt/jdk1.6.0_45/bin/javac 300

 

3)设置环境变量,在 /etc/profile中加入如下内容(可以使用vi进行编辑profile):

export JAVA_HOME=/opt/jdk1.6.0_45/
export JRE_HOME=/opt/jdk1.6.0_45/jre 
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

4)然后运行source  /etc/profile使环境变量生效,貌似只能在当前命令窗口生效,最好重启下机器。

5)使用java -version 查看是否显示版本信息。

6)在终端执行命令 javac java 都能产生很多的信息,就说明安装成功了,你可以用一下例子测试下!

public class hello {
public static void main(String args [ ])
{
System.out.println("Hello World!");
}
}
root@ubuntu: javac hello.java
root@ubuntu: java hello

如果终端有Hello World输出 证明一切都已经好了!

二、安装Tomcat

下面我们开始安装apache-tomcat ,首先从官网下载apache-tomcat的linux包apache-tomcat-6.0.35.tar.gz  下载地址如下  http://tomcat.apache.org/download-60.cgi 

1.首先将tomcat安装包拷贝到/opt下 

root@ubuntu: cp apache-tomcat-6.0.35.tar.gz /opt

2.然后解压apache-tomcat-6.0.35.tar.gz包,解压后的目录为apache-tomcat-6.0.35

root@ubuntu:cd /opt
root@ubuntu:tar xzvf apache-tomcat-6.0.35.tar.gz

3.然后在终端输入:

[root@localhost heshan]# /opt/apache-tomcat-6.0.35/bin/startup.sh  // 启动Tomcat

显示:

Using CATALINA_BASE:   /opt/apache-tomcat-6.0.35
Using CATALINA_HOME:   /opt/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.35/temp
Using JRE_HOME:        /opt/jdk1.6.0_31/
Using CLASSPATH:       /opt/apache-tomcat-6.0.35/bin/bootstrap.jar
复制代码
[root@localhost heshan]# cd /opt/apache-tomcat-6.0.35/logs      //切换到logs目录下
[root@localhost heshan]# tail -f catalina.out     //显示Tomcat的控制台输出信息
INFO: Deploying web application directory ServletTest
init actionservlet
Mar 24, 2012 1:33:32 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-80
Mar 24, 2012 1:33:32 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 24, 2012 1:33:32 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/38  config=null
Mar 24, 2012 1:33:32 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2110 ms
复制代码


通过以上步骤tomcat已经安装完成,现在使用浏览器访问 http://localhost:8080,出现tomcat默认页面,说明已经安装成功。
现在说下怎么样关闭Tomcat

[root@localhost logs]# /opt/apache-tomcat-6.0.35/bin/shutdown.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-6.0.353
Using CATALINA_HOME:   /opt/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.35/temp
Using JRE_HOME:        /opt/jdk1.6.0_31/
Using CLASSPATH:       /opt/apache-tomcat-6.0.35/bin/bootstrap.jar

这样就关闭了Tomcat,不过在我们我们启动Tomcat的时候最后先看一下Tomcat是否正在运行

如下:

复制代码
[root@localhost logs]# ps -ef|grep tomcat
root      5820     1 22 13:40 pts/2    00:00:01 /opt/jdk1.6.0_31//bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-6.0.33/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=10.48.17.193 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Djava.endorsed.dirs=/opt/apache-tomcat-6.0.353/endorsed -classpath /opt/apache-tomcat-6.0.35/bin/bootstrap.jar -Dcatalina.base=/opt/apache-tomcat-6.0.35 -Dcatalina.home=/opt/apache-tomcat-6.0.35 -Djava.io.tmpdir=/opt/apache-tomcat-6.0.35/temp org.apache.catalina.startup.Bootstrap start

root      5855 14306  0 13:40 pts/2    00:00:00 grep tomcat
复制代码

 这时就说明Tomcat正在运行,所以我们必须按照上面的步骤先关闭Tomcat,然后再启动,否则会产生以一些问题。
至此,我们在linux下已经安装好jdk和Tomcat了,我们也可以开始工作了。

 

为了操作方便,在/usr/local 下建立软链接,名字是tomcat6

cd /usr/local sudo ln -/opt/apache-tomcat-6.0.35 tomcat6
测试tomcat是否能够正常启动
shell>sudo /usr/local/tomcat6/bin/startup.sh

 

四、tomcat开机自启动

方法一:简单方法

将tomcat的启动脚本命令如本例中:/opt/apache-tomcat-6.0.35/bin/startup.sh
放进/etc/rc.local文件下即可,注意命令脚本需要放在 exit 0 之前。

方法二:使用自启动工具 sysv-rc-conf

参考下面链接:

http://hi.baidu.com/yjbqzsf/item/24de91318bb8b9b8124b1459

http://blog.sina.com.cn/s/blog_644e226401013p3t.html

 

 

posted @ 2014-01-14 17:09  milkty  阅读(600)  评论(0编辑  收藏  举报