配置linux的javaweb环境之前:
1、在windows安装xshell(非必需,但是推荐)
2、在linux安装Linux与windows文件传输工具RZSZ
[root@192 ~]# 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下创建要存放压缩包的文件夹:
[root@ ~]# mkdir /usr/wangzf
[root@ ~]# mkdir /usr/wangzf/downloads
[root@ ~]# cd /usr/wangzf/downloads/ # 在windows下将jre-7u79-linux-x64.tar.gz和apache-tomcat-7.0..tar.gz拖动到xshell窗口,完成tar包的文件传输
[root@ downloads]# pwd
/usr/wangzf/downloads
[root@ downloads]# ll
total
-rw-r--r--. root root May : apache-tomcat-7.0..tar.gz
-rw-r--r--. root root May : jre-7u79-linux-x64.tar.gz
2、解压到指定目录并重命名
# 解压jre-7u79-linux-x64.tar.gz到/usr/wangzf/
[root@ downloads]# tar -xzvf jre-7u79-linux-x64.tar.gz -C /usr/wangzf/ # 解压apache-tomcat-7.0..tar.gz到/usr/local/tomcat
[root@ downloads]# tar -xzvf apache-tomcat-7.0..tar.gz -C /usr/wangzf/ # 查看解压后的两个文件夹
[root@ downloads]# cd /usr/wangzf/
[root@ wangzf]# ll
total
drwxr-xr-x. root root May : apache-tomcat-7.0.
drwxr-xr-x. root root May : downloads
drwxr-xr-x. uucp Apr jre1..0_79 # 重命名文件夹
[root@ wangzf]# mv jre1..0_79/ jre1.
[root@ wangzf]# mv apache-tomcat-7.0./ tomcat7
[root@ wangzf]# ll
total
drwxr-xr-x. root root May : downloads
drwxr-xr-x. uucp Apr jre1.
drwxr-xr-x. root root May : tomcat7 # 一般jre1.x都在java文件夹下,现在移动下
[root@ wangzf]# mkdir /usr/wangzf/java
[root@ wangzf]# mv jre1./ ./java/jre1.
[root@ wangzf]# ll
total
drwxr-xr-x. root root May : downloads
drwxr-xr-x. root root May : java
drwxr-xr-x. root root May : tomcat7
[root@ wangzf]# ll ./java/
total
drwxr-xr-x. uucp Apr jre1.
3、配置jre和tomcat的环境
3.1、配置jre环境
# 将java环境配置到/etc/profile.d/java.sh
[root@ wangzf]# vi /etc/profile.d/java.sh # 将以下内容写入文件:
JAVA_HOME=/usr/wangzf/java
JRE_HOME=$JAVA_HOME/jre1.
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中的命令而不用注销并重新登录
[root@ wangzf]# source /etc/profile.d/java.sh # 查看刚刚的环境变量
[root@ wangzf]# echo $JAVA_HOME
/usr/wangzf/java
[root@ wangzf]# echo $JRE_HOME
/usr/wangzf/java/jre1.
[root@ wangzf]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/wangzf/java/jre1./bin
[root@ wangzf]# echo $CLASSPATH
/usr/wangzf/java/jre1./lib/rt.jar:/usr/wangzf/java/jre1./lib/ext # 测试java环境
[root@ wangzf]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) -Bit Server VM (build 24.79-b02, mixed mode)
3.2、配置tomcat环境
# 将tomcat环境配置到/etc/profile.d/tomcat.sh
[root@ ~]# 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脚本,使之生效
[root@ ~]# source /etc/profile.d/tomcat.sh # 查看环境变量PATH
[root@ ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/wangzf/java/jre1./bin:/usr/wangzf/tomcat7/bin # 查看tomcat的版本状态
[root@ ~]# 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.
Using CLASSPATH: /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.
Server built: Mar :: UTC
Server number: 7.0.77.0
OS Name: Linux
OS Version: 2.6.-.el6.x86_64
Architecture: amd64
JVM Version: 1.7.0_79-b15
JVM Vendor: Oracle Corporation
4、在linux下启动tomcat,在window下访问tomcat主页
# 在centos中,开放iptables防火墙的端口80||3306端口
[root@ ~]# /sbin/iptables -I INPUT -p tcp --dport -j ACCEPT
[root@ ~]# /sbin/iptables -I INPUT -p tcp --dport -j ACCEPT
[root@ ~]# /sbin/iptables -I INPUT -p tcp --dport -j ACCEPT
[root@ ~]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] # 查看防火墙的状态
[root@ ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
ACCEPT tcp -- 0.0.0.0/ 0.0.0.0/ tcp dpt:
ACCEPT tcp -- 0.0.0.0/ 0.0.0.0/ tcp dpt:
ACCEPT tcp -- 0.0.0.0/ 0.0.0.0/ tcp dpt:
ACCEPT all -- 0.0.0.0/ 0.0.0.0/ state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/ 0.0.0.0/
ACCEPT all -- 0.0.0.0/ 0.0.0.0/
ACCEPT tcp -- 0.0.0.0/ 0.0.0.0/ state NEW tcp dpt:
REJECT all -- 0.0.0.0/ 0.0.0.0/ reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
REJECT all -- 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
[root@ ~]# 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.
Using CLASSPATH: /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Tomcat started. # 查看linux的ip(这里是192.168.22.)
[root@ ~]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C:::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/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (63.5 MiB) TX bytes: (1.5 MiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b) # 想要在windows下访问linux的服务器项目,前提是必须能够在windows下ping虚拟机下的centos主机
C:\Users\Administrator>ping 192.168.22.130 正在 Ping 192.168.22.130 具有 字节的数据:
来自 192.168.22.130 的回复: 字节= 时间<1ms TTL=
来自 192.168.22.130 的回复: 字节= 时间=5ms TTL=
来自 192.168.22.130 的回复: 字节= 时间=1ms TTL=
来自 192.168.22.130 的回复: 字节= 时间=1ms TTL= 192.168.22.130 的 Ping 统计信息:
数据包: 已发送 = ,已接收 = ,丢失 = (% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 5ms,平均 = 1ms C:\Users\Administrator> # 在windows下访问http://192.168.22.130:8080/,是不是看到了熟悉的tomcat,大功告成? 如果要关闭tomcat,则执行以下命令: [root@ ~]# 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.
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用来存放发布的项目
[root@ ~]# mkdir /usr/wangzf/Apps
[root@ ~]# cd /usr/wangzf/Apps # 将MyCentos.zip拖到xshell窗口上传到当前目录
[root@ Apps]# rz -E
rz waiting to receive. # 查看上传的文件
[root@ Apps]# ll
total
-rw-r--r--. root root May : MyCentos.zip # 在linux下解压缩zip文件需要安装zip和unzip
[root@ Apps]# yum install zip
[root@ Apps]# yum install unzip # 解压MyCentos.zip下的所有文件()到当前目录
[root@ 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节点,部署项目
[root@ MyCentos]# cd /usr/wangzf/tomcat7/config/
[root@ conf]# cp server.xml server.xml.back
[root@ 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
[root@ 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.
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之前
[root@localhost ~]# ps -ef|grep java
root : pts/ :: grep java # 尚未启动tomcat之前
[root@localhost ~]# ps -ef|grep tomcat
root : pts/ :: grep tomcat # 启动tomcat
[root@localhost ~]# 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.
Using CLASSPATH: /usr/wangzf/tomcat7/bin/bootstrap.jar:/usr/wangzf/tomcat7/bin/tomcat-juli.jar
Tomcat started. # 启动tomcat之后
[root@localhost ~]# ps -ef|grep tomcat
root : pts/ :: /usr/wangzf/java/jre1./bin/java -Djava.util.logging.config.file=/usr/wangzf/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize= -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 : pts/ :: grep tomcat # 启动tomcat(会启动jre)之后
[root@localhost ~]# ps -ef|grep java
root : pts/ :: /usr/wangzf/java/jre1./bin/java -Djava.util.logging.config.file=/usr/wangzf/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize= -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 : pts/ :: grep java
PS:
要把tomcat加入服务设置开机自启动,请查看wangli61289的博客:http://blog.csdn.net/wangli61289/article/details/37924785