vm12下Centos6的javaweb环境搭建

Posted MMMirana

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vm12下Centos6的javaweb环境搭建相关的知识,希望对你有一定的参考价值。

配置linux的javaweb环境之前:

1、在windows安装xshell(非必需,但是推荐)

2、在linux安装Linux与windows文件传输工具RZSZ
[[email protected] ~]# yum install lrzsz

一、安装centos的jre和tomcat

1、windows下载tar压缩包并上传至linux服务器

windows下下载linux的jre(http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.tar.gz?AuthParam=1494032273_7bab4bfca10697b99871cea7baab274c)

windows下下载linux的tomcat7(https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.77/bin/apache-tomcat-7.0.77.tar.gz)

# 下载完成后在linux下创建要存放压缩包的文件夹:
[[email protected]192 ~]# mkdir /usr/wangzf
[[email protected]192 ~]# mkdir /usr/wangzf/downloads
[[email protected]192 ~]# cd /usr/wangzf/downloads/

# 在windows下将jre-7u79-linux-x64.tar.gz和apache-tomcat-7.0.77.tar.gz拖动到xshell窗口,完成tar包的文件传输
[[email protected]192 downloads]# pwd
/usr/wangzf/downloads
[[email protected]192 downloads]# ll
total 54756
-rw-r--r--. 1 root root  8967197 May  5 20:58 apache-tomcat-7.0.77.tar.gz
-rw-r--r--. 1 root root 47098831 May  5 21:02 jre-7u79-linux-x64.tar.gz

 

2、解压到指定目录并重命名

# 解压jre-7u79-linux-x64.tar.gz到/usr/wangzf/
[[email protected]192 downloads]# tar -xzvf jre-7u79-linux-x64.tar.gz -C /usr/wangzf/

# 解压apache-tomcat-7.0.77.tar.gz到/usr/local/tomcat
[[email protected]192 downloads]# tar -xzvf apache-tomcat-7.0.77.tar.gz -C /usr/wangzf/

# 查看解压后的两个文件夹
[[email protected]192 downloads]# cd /usr/wangzf/
[[email protected]192 wangzf]# ll
total 12
drwxr-xr-x. 9 root root 4096 May  5 23:04 apache-tomcat-7.0.77
drwxr-xr-x. 2 root root 4096 May  5 22:47 downloads
drwxr-xr-x. 6 uucp  143 4096 Apr 10  2015 jre1.7.0_79

# 重命名文件夹
[[email protected]192 wangzf]# mv jre1.7.0_79/ jre1.7
[[email protected]192 wangzf]# mv apache-tomcat-7.0.77/ tomcat7
[[email protected]192 wangzf]# ll
total 12
drwxr-xr-x. 2 root root 4096 May  5 22:47 downloads
drwxr-xr-x. 6 uucp  143 4096 Apr 10  2015 jre1.7
drwxr-xr-x. 9 root root 4096 May  5 23:04 tomcat7

# 一般jre1.x都在java文件夹下,现在移动下
[[email protected]192 wangzf]# mkdir /usr/wangzf/java
[[email protected]192 wangzf]# mv jre1.7/ ./java/jre1.7
[[email protected]192 wangzf]# ll
total 12
drwxr-xr-x. 2 root root 4096 May  5 22:47 downloads
drwxr-xr-x. 3 root root 4096 May  5 23:13 java
drwxr-xr-x. 9 root root 4096 May  5 23:04 tomcat7
[[email protected]192 wangzf]# ll ./java/
total 4
drwxr-xr-x. 6 uucp 143 4096 Apr 10  2015 jre1.7

 

3、配置jre和tomcat的环境

3.1、配置jre环境

# 将java环境配置到/etc/profile.d/java.sh
[[email protected]192 wangzf]# vi /etc/profile.d/java.sh

# 将以下内容写入文件:
JAVA_HOME=/usr/wangzf/java
JRE_HOME=$JAVA_HOME/jre1.7
PATH=$PATH:$JRE_HOME/bin
CLASSPATH=$JRE_HOME/lib/rt.jar:$JRE_HOME/lib/ext
export JAVA_HOME
export JRE_HOME
export PATH
export CLASSPATH

# 使用source命令重新执行java.sh中的命令而不用注销并重新登录
[[email protected]192 wangzf]# source /etc/profile.d/java.sh 

# 查看刚刚的环境变量
[[email protected]192 wangzf]# echo $JAVA_HOME
/usr/wangzf/java
[[email protected]192 wangzf]# echo $JRE_HOME
/usr/wangzf/java/jre1.7
[[email protected]192 wangzf]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/wangzf/java/jre1.7/bin
[[email protected]192 wangzf]# echo $CLASSPATH
/usr/wangzf/java/jre1.7/lib/rt.jar:/usr/wangzf/java/jre1.7/lib/ext

# 测试java环境
[[email protected]192 wangzf]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

3.2、配置tomcat环境

# 将tomcat环境配置到/etc/profile.d/tomcat.sh
[[email protected]192 ~]# vi /etc/profile.d/tomcat.sh

# 在tomcat.sh中写入以下内容 
CATALINA_BASE=/usr/wangzf/tomcat7
PATH=$PATH:$CATALINA_BASE/bin
export CATALINA_BASE
export PATH

# 执行tomcat.sh脚本,使之生效
[[email protected]192 ~]# source /etc/profile.d/tomcat.sh 

# 查看环境变量PATH
[[email protected]192 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/wangzf/java/jre1.7/bin:/usr/wangzf/tomcat7/bin

# 查看tomcat的版本状态
[[email protected]192 ~]# catalina.sh version
Using CATALINA_BASE:   /usr/wangzf/tomcat7
Using CATALINA_HOME:   /usr/wangzf/tomcat7
Using CATALINA_TMPDIR: /usr/wangzf/tomcat7/temp
Using JRE_HOME:        /usr/wangzf/java/jre1.7
Using CLASSPATH:       /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.77
Server built:   Mar 28 2017 16:01:48 UTC
Server number:  7.0.77.0
OS Name:        Linux
OS Version:     2.6.32-696.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:     Oracle Corporation

4、在linux下启动tomcat,在window下访问tomcat主页

# 在centos中,开放iptables防火墙的端口80|8080|3306端口 
[[email protected]192 ~]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected]192 ~]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
[[email protected]192 ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[[email protected]192 ~]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

# 查看防火墙的状态
[[email protected]192 ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
5    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
8    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

# 在linux下启动tomact
[[email protected]192 ~]# startup.sh
Using CATALINA_BASE:   /usr/wangzf/tomcat7
Using CATALINA_HOME:   /usr/wangzf/tomcat7
Using CATALINA_TMPDIR: /usr/wangzf/tomcat7/temp
Using JRE_HOME:        /usr/wangzf/java/jre1.7
Using CLASSPATH:       /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Tomcat started.

# 查看linux的ip(这里是192.168.22.130)
[[email protected]192 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:45:F2:D8  
          inet addr:192.168.22.130  Bcast:192.168.22.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe45:f2d8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:55440 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16803 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:66627061 (63.5 MiB)  TX bytes:1665738 (1.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

# 想要在windows下访问linux的服务器项目,前提是必须能够在windows下ping虚拟机下的centos主机
C:\Users\Administrator>ping 192.168.22.130

正在 Ping 192.168.22.130 具有 32 字节的数据:
来自 192.168.22.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.22.130 的回复: 字节=32 时间=5ms TTL=64
来自 192.168.22.130 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.22.130 的回复: 字节=32 时间=1ms TTL=64

192.168.22.130 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 5ms,平均 = 1ms

C:\Users\Administrator>

# 在windows下访问http://192.168.22.130:8080/,是不是看到了熟悉的tomcat,大功告成?

如果要关闭tomcat,则执行以下命令:

[[email protected]192 ~]# shutdown.sh
Using CATALINA_BASE:   /usr/wangzf/tomcat7
Using CATALINA_HOME:   /usr/wangzf/tomcat7
Using CATALINA_TMPDIR: /usr/wangzf/tomcat7/temp
Using JRE_HOME:        /usr/wangzf/java/jre1.7
Using CLASSPATH:       /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar

 

二、将javaweb项目放入linux下测试并访问

1、使用Eclipse或者MyEclipse创建一个最简单的web项目(本人这里项目名称是MyCentos)

2、在tomcat目录下找到对应的项目,打包成zip(linux没有安装jdk,只安装了jre,所以在linux下不能解压war包,这里压缩成zip),放入/usr/wangzf/Apps目录

# MyCentos.zip的目录结构

MyCentos.zip
├── MyCentos
    ├── index.jsp
    ├── META-INF
    │   └── MANIFEST.MF
    └── WEB-INF
        ├── classes
        ├── lib
        └── web.xml 

 

 

# 创建目录/usr/wangzf/Apps用来存放发布的项目
[[email protected]192 ~]# mkdir /usr/wangzf/Apps
[[email protected]192 ~]# cd /usr/wangzf/Apps

# 将MyCentos.zip拖到xshell窗口上传到当前目录
[[email protected]192 Apps]# rz -E
rz waiting to receive.

# 查看上传的文件
[[email protected]192 Apps]# ll
total 4
-rw-r--r--. 1 root root 1939 May  5 23:36 MyCentos.zip

# 在linux下解压缩zip文件需要安装zip和unzip
[[email protected]192 Apps]# yum install zip
[[email protected]192 Apps]# yum install unzip

# 解压MyCentos.zip下的所有文件()到当前目录
[[email protected]192 Apps]# unzip MyCentos.zip 
Archive:  MyCentos.zip
   creating: MyCentos/
   creating: MyCentos/META-INF/
 extracting: MyCentos/META-INF/MANIFEST.MF  
   creating: MyCentos/WEB-INF/
   creating: MyCentos/WEB-INF/classes/
  inflating: MyCentos/WEB-INF/index.jsp  
   creating: MyCentos/WEB-INF/lib/
  inflating: MyCentos/WEB-INF/web.xml  
  inflating: MyCentos/index.jsp      

# 备份并修改tomcat server.xml,加入Context节点,部署项目
[[email protected]192 MyCentos]# cd /usr/wangzf/tomcat7/config/
[[email protected]192 conf]# cp server.xml server.xml.back
[[email protected]192 conf]# vi server.xml

# server.xml代码片段
...
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

    <Context docBase="/usr/wangzf/Apps/MyCentos" path="/MyCentosWeb" reloadable="true"/>

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
...

 

3、启动tomcat

[[email protected]192 conf]# startup.sh 
Using CATALINA_BASE: /usr/wangzf/tomcat7
Using CATALINA_HOME: /usr/wangzf/tomcat7
Using CATALINA_TMPDIR: /usr/wangzf/tomcat7/temp
Using JRE_HOME: /usr/wangzf/java/jre1.7
Using CLASSPATH: /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Tomcat started.

 

4、windows客服端访问
在windows下访问“http://192.168.22.130:8080/MyCentosWeb/”,就可以看到index.jsp的内容了!

 

附:在linux下查看tomcat|java是否启动

# 尚未启动java之前
[[email protected] ~]# ps -ef|grep java
root       1428   1413  0 02:47 pts/0    00:00:00 grep java

# 尚未启动tomcat之前
[[email protected] ~]# ps -ef|grep tomcat
root       1430   1413  0 02:47 pts/0    00:00:00 grep tomcat

# 启动tomcat
[[email protected] ~]# startup.sh 
Using CATALINA_BASE:   /usr/wangzf/tomcat7
Using CATALINA_HOME:   /usr/wangzf/tomcat7
Using CATALINA_TMPDIR: /usr/wangzf/tomcat7/temp
Using JRE_HOME:        /usr/wangzf/java/jre1.7
Using CLASSPATH:       /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Tomcat started.

# 启动tomcat之后
[[email protected] ~]# ps -ef|grep tomcat
root       1439      1 23 02:47 pts/0    00:00:00 /usr/wangzf/java/jre1.7/bin/java -Djava.util.logging.config.file=/usr/wangzf/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/wangzf/tomcat7/endorsed -classpath /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/usr/wangzf/tomcat7 -Dcatalina.home=/usr/wangzf/tomcat7 -Djava.io.tmpdir=/usr/wangzf/tomcat7/temp org.apache.catalina.startup.Bootstrap start
root       1450   1413  0 02:47 pts/0    00:00:00 grep tomcat

# 启动tomcat(会启动jre)之后
[[email protected] ~]# ps -ef|grep java
root       1439      1 36 02:47 pts/0    00:00:12 /usr/wangzf/java/jre1.7/bin/java -Djava.util.logging.config.file=/usr/wangzf/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/wangzf/tomcat7/endorsed -classpath /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/usr/wangzf/tomcat7 -Dcatalina.home=/usr/wangzf/tomcat7 -Djava.io.tmpdir=/usr/wangzf/tomcat7/temp org.apache.catalina.startup.Bootstrap start
root       1480   1413  0 02:48 pts/0    00:00:00 grep java

 



以上是关于vm12下Centos6的javaweb环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

阿里云centos6.5下搭建javaWeb运行环境

基于centos6.7的lnmp环境搭建

在VM虚拟机中安装Redhat6.5 / CentOs6.5

vm12下Centos6安装mysql5.7

Java学习心得之 Linux下搭建JavaWeb环境

在linux环境下搭建java web测试环境(非常详细!!)