`

RED HAT LINUX 9 架设WEB服务器TOMCAT和安装JAVA环境

阅读更多

RED HAT LINUX 9 架设WEB服务器TOMCAT和安装JAVA环境

流程

安装JDK,配置jdk

安装tomcat,配置tomcat

配置系统服务

配置开机启动

建立检测系统

配置80端口

配置系统开机启动服务

 

<!--[if !supportLists]-->一、<!--[endif]-->Linux上安装环境

1.1拷贝文件

jdk-1_5_0_19-linux-i586.rpm

apache-tomcat-5.5.29.tar.gz

放到/usr/local

1.2安装java环境

1.3安装web环境

1安装jdk

jdk-1_5_0_19-linux-i586.rpm

a.赋权限

chmod +x  jdk-1_5_0_19-linux-i586.rpm

b.安装

rpm –ivh jdk-1_5_0_19-linux-i586.rpm

c 配置

执行后默认安装在usr/java文件夹下 //建立符号连接

Ln –s /usr/java/jdk-1_5_0_19  /usr/local/jdk1.5

d最后位置

/usr/local/jdk1.5

2.安装tomcat

apache-tomcat-5.5.29.tar.gz

a赋权:

chmod +x apache-tomcat-5.5.29.tar.gz

b安装

tar -xzvf apache-tomcat-5.5.29.tar.gz

c最后位置

/usr/local/apache-tomcat-5.5.29

3设置系统环境变量,编辑/etc/profile文件

Vi /etc/profile

在后面添加

JAVA_HOME=/usr/local/jdk1.5

CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:

$JAVA_HOME/jre/lib/rt.jar

CATALINA_BASE=/usr/local/apache-tomcat-5.5.29

CATALINA_HOME=/usr/local/apache-tomcat-5.5.29

PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin

export JAVA_HOME CLASSPATH CATALINA_BASE CATALINA_HOME PATH

unset i

保存

设置起效

Source /etc/profile

检验

Java –version                  

二.配置web服务器做为系统服务并开机启动

设置tomcat服务器开机启动使用jsvctomcat启动为Linux的一个进程

[root@tomcat ~]# cd /usr/local/ apache-tomcat-5.5.29/bin/

[root@tomcat bin]# tar -zxvf jsvc.tar.gz

[root@tomcat bin]# cd jsvc-src/

[root@tomcat jsvc-src]# chmod +x configure

[root@tomcat jsvc-src]# ./configure --with-java=/usr/local/jdk1.5

[root@tomcat jsvc-src]# make

#完成jsvc的编译

[root@tomcatjsvc-src]#cp/usr/local/apache-tomcat-5.5.29/bin/jsvc-src/native/Tomcat5.sh

/etc/rc.d/init.d/

#将生成的脚本复制到/etc/rc.d/init.d文件中

[root@tomcat jsvc-src]# cd /etc/rc.d/init.d/

[root@tomcat init.d]# chmod +x Tomcat5.sh 很重要

[root@tomcat init.d]# vi Tomcat5.sh

**************

#!/bin/sh

#chkconfig:2345 85 15

#description:Star and Stop the Tomcat daemon

##############################################################################

#

#   Copyright 2004 The Apache Software Foundation.

#

#   Licensed under the Apache License, Version 2.0 (the "License");

#   you may not use this file except in compliance with the License.

#   You may obtain a copy of the License at

#

#       http://www.apache.org/licenses/LICENSE-2.0

#

#   Unless required by applicable law or agreed to in writing, software

#   distributed under the License is distributed on an "AS IS" BASIS,

#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

#   See the License for the specific language governing permissions and

#   limitations under the License.

##############################################################################

#

# Small shell script to show how to start/stop Tomcat using jsvc

# If you want to have Tomcat running on port 80 please modify the server.xml

# file:

#

#    <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->

#    <Connector className="org.apache.catalina.connector.http.HttpConnector"

#               port="80" minProcessors="5" maxProcessors="75"

#               enableLookups="true" redirectPort="8443"

#               acceptCount="10" debug="0" connectionTimeout="60000"/>

#

# That is for Tomcat-5.0.x (Apache Tomcat/5.0)

#

# Adapt the following lines to your configuration

JAVA_HOME=/usr/local/jdk1.5

CATALINA_HOME=/usr/local/apache-tomcat-5.5.29

DAEMON_HOME=/usr/local/apache-tomcat-5.5.29/bin

TOMCAT_USER=root

 

# for multi instances adapt those lines.

TMP_DIR=/var/tmp

PID_FILE=/var/run/jsvc.pid #注意:将jsvc.pid复制到/var/run

CATALINA_BASE=/usr/local/apache-tomcat-5.5.29

 

CATALINA_OPTS="-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs"

CLASSPATH=\

$JAVA_HOME/lib/tools.jar:\

$CATALINA_HOME/bin/commons-daemon.jar:\

$CATALINA_HOME/bin/bootstrap.jar

 

case "$1" in

  start)

    #

    # Start Tomcat

    #

    $DAEMON_HOME/jsvc-src/jsvc \

    -user $TOMCAT_USER \

    -home $JAVA_HOME \

    -Dcatalina.home=$CATALINA_HOME \

    -Dcatalina.base=$CATALINA_BASE \

    -Djava.io.tmpdir=$TMP_DIR \

    -wait 10 \

    -pidfile $PID_FILE \

    -outfile $CATALINA_HOME/logs/catalina.out \

    -errfile '&1' \

    $CATALINA_OPTS \

    -cp $CLASSPATH \

    org.apache.catalina.startup.Bootstrap

    #

    # To get a verbose JVM

    #-verbose \

    # To get a debug of jsvc.

    #-debug \

    exit $?

    ;;

 

  stop)

    #

    # Stop Tomcat

    #

    $DAEMON_HOME/jsvc-src/jsvc \

    -stop \

    -pidfile $PID_FILE \

    org.apache.catalina.startup.Bootstrap

    exit $?

    ;;

 

  *)

    echo "Usage tomcat.sh start/stop"

    exit 1;;

esac

 

**************

修改后

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc0.d/K90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc1.d/K90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc2.d/S90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc3.d/S90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc4.d/S90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc5.d/S90Tomcat5.sh

[root@tomcat init.d]# ln -s /etc/init.d/Tomcat5.sh /etc/rc6.d/K95Tomcat5.sh

#为不同的运行基本建立启动和停止服务信息

测试tomcat启动脚本

[root@tomcat init.d]# /etc/rc.d/init.d/Tomcat5.sh start

#执行脚本启动tomcat服务

[root@tomcat init.d]# netstat -tnl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

tcp 0 0 :::8009 :::* LISTEN

tcp 0 0 :::8080 :::* LISTEN

tcp 0 0 :::80 :::* LISTEN

tcp 0 0 :::22 :::* LISTEN

#查看服务状态

[root@tomcat init.d]# /etc/rc.d/init.d/Tomcat5.sh stop

[root@tomcat init.d]# netstat -tnl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

tcp 0 0 :::80 :::* LISTEN

tcp 0 0 :::22 :::* LISTEN

#检测服务是否已经停止

tomcat启动脚本注册为系统服务

服务安装完毕,添加到系统服务中去

chkconfig – -add Tomcat5.sh

chkconfig – -level 345 Tomcat5.sh on

自此软件环境安装完毕,系统启动会带动tomcat,并且能使用service指令操作tomcat开启停止

 

三.配置环境和配置tomcat

Server.xml ROOT catalina.sh tomcat-user.xml

配置80端口

配置直接输入IP转发

配置字符编码

配置内存大小

配置manager用户和密码

拷贝ROOT HVAC两个项目到webapp

删除其他项目

 

<!--[if !supportLists]-->二、<!--[endif]-->安装必要的软件包

Tomcat能开机自动启动,但是需要监控是否运行正常

脚本+计划任务

Health.sh

********************

#!/bin/bash

#The WEB Server Status Check and Repair Script.

TMP=/tmp/health.html

HEALTH="WEBSERVEROK"

REQUEST_TOMCAT=http://127.0.0.1:80/health.html

LOGDATE="$(date +%F) $(date +%T) $CATALINA_HOME "

LOGFILE=/var/log/WebServer.log

 

export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/java/jdk1.5.0_19/bin:/usr/local/bin/

 

[ ! -f $LOGFILE ] && touch $LOGFILE

 

function Check_Status()

{

local RETRY=1

local SUCCESS=0

 

while [ $RETRY -le 3 ]; do

 

        [ -f $TMP ] && rm -f $TMP

 

    /usr/local/bin/curl -o $TMP $2  2>&1

                 sleep 2

        STATUS=$(egrep -o "$HEALTH" $TMP)

 

        if [ x$STATUS = x ]; then

                echo "${LOGDATE}$1  check failure repeat $1   $RETRY times,repeat..." >> $LOGFILE

                let RETRY=$RETRY+1

                sleep 2

                continue

        fi

 

        if [ $STATUS = "$HEALTH" ]; then

                echo "${LOGDATE} $1 work fine!" >> $LOGFILE

                SUCCESS=1

                break

                else

                echo "${LOGDATE}$1 check failure repeat $1  $RETRY times,repeat..." >> $LOGFILE

                let RETRY=$RETRY+1

                sleep 2

        fi

 

done

 

if [ $SUCCESS -eq 0 ]; then

        let FC=$RETRY-1

        echo "${LOGDATE} repeat $FC times $1 status  failure,will restart $1!" >> $LOGFILE

        RETURN=0

else

        RETURN=1

fi

 

rm -f $TMP

 

}

 

Check_Status Tomcat $REQUEST_TOMCAT

 

if [ $RETURN -eq 0 ]; then

 

        PID=$(ps -ef | grep tomcat| wc -l)

 

        if [ $PID -gt 1 ]; then

 

                echo "${LOGDATE} Tomcat is presence,kill them and restart..." >> $LOGFILE

                HTTP_PID=$(ps -ef | grep tomcat| awk '{print $2}')

                kill -9 $HTTP_PID > /dev/null 2>&1

                                         sleep 5

                service Tomcat5.sh start > /dev/null 2>&1

        else

                service Tomcat5.sh start > /dev/null 2>&1

 

        fi

                sleep 2

 

        PID=$(ps -ef | grep tomcat | wc -l)

 

        if [ $PID -gt 1 ]; then

 

                echo "${LOGDATE} restart Tomcat success!" >> $LOGFILE

        else

                echo "${LOGDATE}restart Tomcat failure!" >> $LOGFILE

        fi

fi

********************

 

思路:

所有这些都可以将配置文件复制粘贴到相对的位置,注意赋予权限。

<!--[if !supportLists]-->1.<!--[endif]-->使用linux计划任务

<!--[if !supportLists]-->2.<!--[endif]-->执行脚本 /home/oyl/health.sh,访问并下载web根目录下的health.html文件

<!--[if !supportLists]-->3.<!--[endif]-->如果下载的health.html文件包含设置文本,则说明tomcat运行正常

<!--[if !supportLists]-->4.<!--[endif]-->没有下载就表示tomcat运行不正常,此时无法确定tomcat是否是僵死还是停止

<!--[if !supportLists]-->5.<!--[endif]-->检查内存中是否存在,如果存在就全部杀死,然后启动,如果不存在就只是启动

<!--[if !supportLists]-->6.<!--[endif]-->将日志信息放到/var/log/WebServer.log

需要文件:

<!--[if !supportLists]-->1.<!--[endif]-->webapp下的Root下放置health.html 被检测的文件

<!--[if !supportLists]-->2.<!--[endif]-->/root/cron下放置health.sh 监控脚本文件

<!--[if !supportLists]-->3.<!--[endif]-->/var/log/下会自动创建 WebServer.log 启动记录日志文件

<!--[if !supportLists]-->4.<!--[endif]-->会产生临时文件 /tmp/health.html 每次执行都会清空前一次的文件

<!--[if !supportLists]-->5.<!--[endif]-->需要下载linux的一个程序 curl-7.14.0.tar.gz

 

运行步骤:

<!--[if !supportLists]-->1.<!--[endif]-->安装 curl-7.14.0.tar.gz(如果系统自带安装了就无需安装,具体查看有没有安装,可以到 /usr/local/bin/curl 指令是否能正确显示)

<!--[if !supportLists]-->1         <!--[endif]-->#tar -zxvf curl-7.14.0.tar.gz 

<!--[if !supportLists]-->2         <!--[endif]-->#cd curl-7.14.0/ 

<!--[if !supportLists]-->3         <!--[endif]-->#./configure 

<!--[if !supportLists]-->4         <!--[endif]-->#make 

<!--[if !supportLists]-->5         <!--[endif]-->#make test 

<!--[if !supportLists]-->6         <!--[endif]-->#make install

<!--[if !supportLists]-->2.<!--[endif]-->复制文件health.html ROOT下面

<!--[if !supportLists]-->3.<!--[endif]-->复制文件health.sh root/cron/

<!--[if !supportLists]-->4.<!--[endif]-->给脚本赋权限

<!--[if !supportLists]-->5.<!--[endif]-->定时计划任务添加该脚本一天执行一次

Crontab文件的每一行由六个域(minuteshoursday of monthmonthday of week command)成,域之间用空格或Tab分开,其中:

  minutes分钟域,值的范围是059

  hours小时域,值的范围是023

  day of month日期,值的范围是131

  month月份,值的范围是112

  day of week星期,值的范围是06,星期日值为0

command所要运行的命令

30 5 * * * /root/cron/health.sh

Curlcrond中执行有问题

问题解决:

环境变量的问题,我调用 curl指令,系统默认先从环境变量中查找该可执行文件,然后就是本文件夹下找,没有找到就无法执行,我是直接使用curl指令,在任务计划中,系统先去找curl的路径,没有找到就报错,后来,我就给curl添加绝对路径 /usr/local/bin/curl 竟然可以了,最后我将此路径放到脚本的PATH路径中

总结:分析问题要完整,如果a方测试不错,问题就出在b方,b方哪里出错?就通过排除法,将条件先删除,语句是否可以执行。

Linux 脚本编程首先都是确定环境变量,将所有要用到的指令的都添加到PATH路径下,执行一条指令,默认先从环境变量开始找,在从本文件夹下找,找不到就报错

 

需要用到的知识:

<!--[if !supportLists]-->1.<!--[endif]-->crontab Linux定时计划任务

<!--[if !supportLists]-->2.<!--[endif]-->Curl Linux 下载

<!--[if !supportLists]-->3.<!--[endif]-->Linux shell 编程

步骤:

<!--[if !supportLists]-->1.<!--[endif]-->Root  下创建文件夹cron

<!--[if !supportLists]-->2.<!--[endif]-->.复制health.sh   root/cron 文件夹下

<!--[if !supportLists]-->3.<!--[endif]-->复制 curl的包 curl-7.14.0.tar.gz /usr/local (可选)

<!--[if !supportLists]-->4.<!--[endif]-->复制health.html  ROOT项目下(可选)

<!--[if !supportLists]-->5.<!--[endif]-->安装crul

<!--[if !supportLists]-->7         <!--[endif]-->#tar zxvf curl-7.14.0.tar.gz 

<!--[if !supportLists]-->8         <!--[endif]-->#cd curl-7.14.0/ 

<!--[if !supportLists]-->9         <!--[endif]-->#./configure 

<!--[if !supportLists]-->10      <!--[endif]-->#make 

<!--[if !supportLists]-->11      <!--[endif]-->#make test 

<!--[if !supportLists]-->12      <!--[endif]-->#make install

6health.sh 赋权 chmod +x health.sh

7添加计划执行 crontab -e

会打开一个文档,输入内容:  30 5 * * * /root/cron/health.sh

8重启crond service crond restart

 

至此 tomcat监控已经完成

 

 

 

 

 

 

 

三、数据库方面

 mysql必备用户localhost.localdomain 这个用户,并且要对OYLTDB_HVAC有增删改查的权限

 

<!--[if !supportLists]-->四、<!--[endif]-->其他

项目中使用ssh 登录需要用户名和密码,否则无法修改系统时间

Linux :root oyltszrd

项目更新:hvacapp hvacgengxin

项目登录:admin admin

 

总结:

运行的程序

升级  采用tomcat manager 控制台热部署发布新程序,执行流程为停止,卸载,上传

修改Linux系统时间,采用java 执行ssh 操作,执行Linux指令直接执行,需要第三方包

局部刷新,使用dwr实现ajax功能,局部定时刷新,并使用dwr插入数据到库

数据无闪动的原理:判断显示数据的表格内有没有数据,第一次加载则创建所有页面元素,后面次数则是替换数据,

定时器延时,操作定时器,当用户将要操作时,停止当前定时器,开启新的定时器,15s过后,关闭当前新的定时器,开启旧的定时器

管理员登录判断,采用filter过滤器实现,判断该操作是否登录没有则直接转到登录页面

 

数据访问层,查询技术采用离线查询,可以在任一session中执行,增删改则是采用spring事务管理,面向方面编程

 

表示层技术:

样式,特效采用  javascript ,css ,显示数据采用jquerydwrstruts标签

 

中间层技术:

Spring 依赖注入,声明式事务处理,编写baseservice,其他继承基类,业务层所有依赖注入可以在此类执行

数据访问层技术:

Hibernate,编写basedao ,其他继承基类,依赖注入只需在此类执行

 

数据库连接:

采用hibernate +proxool数据库连接池方式,避免mysql数据库8小时断开连接,而且可以收回空闲连接

 

日志记录:

Log4j,基本上在开发期间有用

 

异常处理:

Try  catch  捕获异常往上面,跳转到异常页面,抓好表示层录入数据

 

 

 

 

要执行Linux指令必须要是root权利,所以用户名和密码不能变

服务器配置

Ip直接访问,随系统启动,每天检测一次是否正常

网上大多数的配置环境是apache+tomcat+jk1模式这种方式,但这是针对大型或中小型网站设计的,本应用无需应对网络所有用户,只需在局域网内部使用,因此使用tomcat就可以了

 

Tomcat server.xml需要配置,端口号,虚拟内存,

当然还要配置 tomcat-users.xml 这样可以添加对tomcat的访问权限

 

 最好是直接替换server.xml

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics