centos7使用tomcat部署javaweb项目
1.下载二进制安装包
2.下载jdk和配置环境变量
这里就不再多描述不清楚,可以 参考 我的yum安装的jdk
3.在centos7下安装 tomcat
#切换到/usr/local 目录下解压安装
[root@localhost local]# cd /usr/local/ [root@localhost local]# tar xf apache-tomcat-8.0.50.tar.gz
#启动tomcat 脚本为startup.sh
[root@localhost local]# cd apache-tomcat-8.0.50/ [root@localhost apache-tomcat-8.0.50]# ./bin/startup.sh Using CATALINA_BASE: /usr/local/apache-tomcat-8.0.50 Using CATALINA_HOME: /usr/local/apache-tomcat-8.0.50 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.0.50/temp Using JRE_HOME: /usr/lib/jvm/java Using CLASSPATH: /usr/local/apache-tomcat-8.0.50/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.50/bin/tomcat-juli.jar Tomcat started.
#登陆网页验证
如果不能显示,估计是防火墙,要么关闭防火墙或者防火墙设置开放8080端口的规则。
#关闭tomcat脚本为shutdown.sh
[root@localhost apache-tomcat-8.0.50]# ./bin/shutdown.sh Using CATALINA_BASE: /usr/local/apache-tomcat-8.0.50 Using CATALINA_HOME: /usr/local/apache-tomcat-8.0.50 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.0.50/temp Using JRE_HOME: /usr/lib/jvm/java Using CLASSPATH: /usr/local/apache-tomcat-8.0.50/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.50/bin/tomcat-juli.jar
4.编写一个tomcat的启动脚本并加入service系统服务
[root@localhost apache-tomcat-8.0.50]# cat /etc/rc.d/init.d/tomcat #!/bin/bash #chkconfig: 2345 10 90 #description: Starts and Stops the Tomcat daemon. #by benjamin ############################################## #Startup script for Tomcat on Linux #filename tomcat.sh #Make sure the Java and the tomcat installation path has been added to the PATH JAVA_HOME=/usr/lib/jvm/java #JDK安装目录 CATALINA_HOME=/usr/local/apache-tomcat-8.0.50 #tomcat安装目录 export JAVA_HOME export CATALINA_HOME ############################################### start_tomcat=$CATALINA_HOME/bin/startup.sh #tomcat启动文件 stop_tomcat=$CATALINA_HOME/bin/shutdown.sh #tomcat关闭文件 start() { echo -n "Starting tomcat: " ${start_tomcat} echo "tomcat start ok." } stop() { echo -n "Shutting down tomcat: " ${stop_tomcat} echo "tomcat stop ok." } # See how we were called case "$1" in start) start ;; stop) stop ;; restart) stop sleep 10 start ;; *) echo "Usage: $0 {start|stop|restart}" esac exit 0
#给启动脚本授权
chmod 755 /etc/rc.d/init.d/tomcat
#将tomcat加入到系统服务中
chkconfig --add /etc/rc.d/init.d/tomcat
# 在tomcat中的catalina.sh文件中添加如下变量
vim /usr/local/apache-tomcat-8.0.50/bin/catalina.sh
#在配置文件中加入如下变量
export JAVA_HOME=/usr/lib/jvm/java #javajdk的安装路径,使用echo $JAVA_HOME命令可以读取 export CATALINA_HOME=/usr/local/apache-tomcat-8.0.50 export CATALINA_BASE=/usr/local/apache-tomcat-8.0.50 export CATALINA_TMPDIR=/usr/local/kencery/apache-tomcat-8.0.50/temp
# 验证是否已加入service
[root@localhost apache-tomcat-8.0.50]# service tomcat start Starting tomcat: Using CATALINA_BASE: /usr/local/apache-tomcat-8.0.50 Using CATALINA_HOME: /usr/local/apache-tomcat-8.0.50 Using CATALINA_TMPDIR: /usr/local/kencery/apache-tomcat-8.0.50/temp Using JRE_HOME: /usr/lib/jvm/java Using CLASSPATH: /usr/local/apache-tomcat-8.0.50/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.50/bin/tomcat-juli.jar Tomcat started. tomcat start ok.
5.设置tomcat开机自启动
#在开机自启动文件中导入 tomcat的相关路径和所依赖的jdk相关路径
[root@localhost ~]# cat /etc/rc.d/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local export JAVA_HOME=/usr/lib/jvm/java export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin export CATALINA_HOME=/usr/local/apache-tomcat-8.0.50/ #tomcat自启动 $CATALINA_HOME/bin/startup.sh
# reboot后检查8080端口是否还在,在的话证明开机启动设置ok
[root@localhost ~]# netstat -lntup|grep 8080 tcp6 0 0 :::8080 :::* LISTEN 625/java
6.给tomcat网页设置登陆帐号密码
a. 当tomcat安装完成之后,访问http://ip:8080即可以看到tomcat发布的网站,若要管理我们发布的网站,就需要登录tomcat,下面就进行用户名和密码的设置。
b. 在tomcat安装包下面的conf文件下含有一个tomcat-usrs.xml文件,修改此文件即可,命令如下:
vim tomcat-users.xml <role rolename="admin-gui"/> <role rolename="manager-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
#如若我们现在想访问tomcat中图形化管理应用的功能,也就是Manager App,就需要指定manager-gui的权限,如果想访问host manager就必须指定admin-gui权限
c.重启tomcat
[root@localhost conf]# service tomcat restart
d 网页验证
7.使用eclipse打包java web项目
以上所有工作完成之后,我们需要将自己要发布的项目打包,那么在MyEclipse中如何打包项目呢?
a. 随便创建一个JavaWeb项目,如图所示:
b. 在项目上单击右键选择【Export】-【MyEclipse JavaEE】文件夹-【WAR file】,单击【Next>】如图所示,选择存放打包程序的地址,单击【Finish】完成,即实现了打包,在完成之后可以去你打包的地方看一下是否打包成功。
d. MyEclipse10.7打包程序的时候当选择【WAR file】,单击【Next>】的时候IDE崩溃的解决办法。
d.1 此错误可能是在破解的时候没有破解的很干净,所以需要执行下面的步骤;
d.2 下载文件com.genuitec.eclipse.export.wizard_9.0.0.me201211011550.jar ,下载地址为:
d.3 找到MyEclipse安装环境的地址,我的安装环境是:
D:\Program Files\MyEclipse\Common\plugins ,将下载下来的文件解析之后替换到plugins中的同名文件(替换后,最好将jar文件改成.txt文件结尾,然后重启MyEclipse10,然后关闭,再将jar改回.jar为扩展名的状态,重启后即可)。
d.4 已测试,没任何问题,完美实现,如果大家遇到,则按照该方法解决即可。
8、将Java Web项目发布到tomcat8.0下面并且访问展示
a. 以上所有工作都完成之后,完美就可以将打包的JavaWeb程序发布到tomcat下即可。
b. tomcat默认的发布web项目的目录是:webapps
c. 将导出的war包拷贝到webapps根目录下,随之tomcat的启动,war包可以自动被解析。
[root@localhost ROOT]# pwd /usr/local/apache-tomcat-8.0.50/webapps/ROOT
d. 然后在浏览器中输入路径链接,查询是否安装成功,如下图所示,则表示安装成功:
大功告成,成功!!!