JDK+Tomcat+Jenkins部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK+Tomcat+Jenkins部署相关的知识,希望对你有一定的参考价值。

1.Tomcat简介
    Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。
    Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试
JSP程序的首选。
    Tomcat和nginx、Apache(httpd)、lighttpd等Web服务器一样,具有处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是
Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Nginx/Apache服务器。
  
2.Tomcat安装

    2.1软件准备

            JDK下载http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
            Tomcat下载http://tomcat.apache.org/
            Jenkins下载https://jenkins.io/index.html

    2.2 部署java和tomcat环境

                2.2.1将下载的jdk和tomcat源码包解压到/usr/local/include/下
                        tar -zxvf apache-tomcat-8.0.27.tar.gz -C /usr/local/include/tomcat8     #同理解压JDK
                2.2.2在/etc/profile末尾添加如下代码,修改完后执行source /etc/profile,使配置文件立即生效
                        export JAVA_HOME=/usr/local/include/jdk/
                        export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
                        export TOMCAT_HOME=/usr/local/include/tomcat8/
                        export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

    2.3 Tomcat目录介绍

                        .
                        ├── bin         #→用以启动、关闭Tomcat或者其它功能的脚本(.bat文件和.sh文件)
                        ├── conf        #→用以配置Tomcat的XML及DTD文件
                        ├── lib         #→存放web应用能访问的JAR包
                        ├── LICENSE
                        ├── logs        #→Catalina和其它Web应用程序的日志文件
                        ├── NOTICE
                        ├── RELEASE-NOTES
                        ├── RUNNING.txt
                        ├── temp        #→临时文件
                        ├── webapps     #→Web应用程序根目录
                        └── work        #→用以产生有JSP编译出的Servlet的.java和.class文件
                        7 directories, 4 files

    2.4 webapps目录结构
                        [[email protected] tomcat]# cd webapps/
                        [[email protected] webapps]# ll
                        total 20
                        drwxr-xr-x 14 root root 4096 Oct  5 12:09 docs     #→tomcat帮助文档
                        drwxr-xr-x  6 root root 4096 Oct  5 12:09 examples #→web应用实例
                        drwxr-xr-x  5 root root 4096 Oct  5 12:09 host-manager #→管理
                        drwxr-xr-x  5 root root 4096 Oct  5 12:09 manager  #→管理
                        drwxr-xr-x  3 root root 4096 Oct  5 12:09 ROOT     #→默认网站根目录

    2.5 启动Tomcat也可直接使用/usr/local/include/tomcat8/bin/catalina.sh start|stop

        启动程序: /usr/local/include/tomcat8/bin/startup.sh
        关闭程序: /usr/local/include/tomcat8/bin/shutdown.sh

                        [[email protected] ~]# /application/tomcat/bin/startup.sh
                        Using CATALINA_BASE:   /application/tomcat
                        Using CATALINA_HOME:   /application/tomcat
                        Using CATALINA_TMPDIR: /application/tomcat/temp
                        Using JRE_HOME:        /application/jdk
                        Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
                        Tomcat started.

        查看监听端口

                        [[email protected] tomcat8]#  netstat -tunlp|grep java
                        tcp        0      0 :::8009                     :::*                        LISTEN      1914/java          
                        tcp        0      0 :::80                       :::*                        LISTEN      1914/java        
 
        查看进程

[[email protected] tomcat8]# ps -ef|grep java
root      1914     1 13 23:08 pts/0    00:00:19 /usr/local/include/jdk//bin/java -Djava.util.logging.config.file=/usr/local/include/tomcat8//conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /usr/local/include/tomcat8//bin/bootstrap.jar:/usr/local/include/tomcat8//bin/tomcat-juli.jar -Dcatalina.base=/usr/local/include/tomcat8/ -Dcatalina.home=/usr/local/include/tomcat8/ -Djava.io.tmpdir=/usr/local/include/tomcat8//temp org.apache.catalina.startup.Bootstrap start


    2.6 访问网站

        网址:http://192.168.100.81:8080/           #服务器IP地址


    2.7 Tomcat日志

                tomcat实时日志查看

[[email protected] tomcat8]# tailf /usr/local/include/tomcat8/logs/catalina.out
22-Apr-2017 23:08:29.659 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/include/tomcat8/webapps/host-manager has finished in 44 ms
22-Apr-2017 23:08:29.663 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/include/tomcat8/webapps/ROOT
22-Apr-2017 23:08:29.690 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/include/tomcat8/webapps/ROOT has finished in 27 ms
22-Apr-2017 23:08:29.691 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/include/tomcat8/webapps/examples
22-Apr-2017 23:08:29.954 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/include/tomcat8/webapps/examples has finished in 259 ms
22-Apr-2017 23:08:29.955 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/include/tomcat8/webapps/manager
22-Apr-2017 23:08:29.982 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/include/tomcat8/webapps/manager has finished in 27 ms
22-Apr-2017 23:08:29.990 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-80"]
22-Apr-2017 23:08:30.019 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
22-Apr-2017 23:08:30.036 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 26300 ms


3.Tomcat配置文件

    3.1 Tomcat配置文件介绍

            [[email protected] tomcat8]# ll /usr/local/include/tomcat8/conf/
            total 228
            drwxr-x--- 3 root root   4096 Apr 22 15:00 Catalina
            -rwxr-xr-x 1 root root  12895 Apr 13 20:58 catalina.policy
            -rwxr-xr-x 1 root root   7376 Apr 13 20:58 catalina.properties
            -rwxr-xr-x 1 root root   1338 Apr 22 21:41 context.xml
            -rwxr-xr-x 1 root root   1149 Apr 13 20:58 jaspic-providers.xml
            -rwxr-xr-x 1 root root   2358 Apr 13 20:58 jaspic-providers.xsd
            -rwxr-xr-x 1 root root   3622 Apr 13 20:58 logging.properties
            -rwxr-xr-x 1 root root   7509 Apr 22 15:05 server.xml               #→主配置文件
            -rwxr-xr-x 1 root root   2298 Apr 22 22:10 tomcat-users.xml         #→Tomcat管理用户配置文件
            -rwxr-xr-x 1 root root   2633 Apr 13 20:58 tomcat-users.xsd
            -rwxr-xr-x 1 root root 168133 Apr 13 20:58 web.xml
 

    3.2 Tomcat配置管理界面(注意:测试功能,生产环境不要用。
    
            Tomcat管理功能用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用。在默认情况下是处于禁用状态的。如果需要开启这个功能,就需要配置管理用户,即配置上面提到的tomcat-users.xml。

            配置tomcat-users.xml文件
 

            [[email protected] tomcat8]# vim /usr/local/include/tomcat8/conf/tomcat-users.xml
            …………
            <role rolename="admin-gui"/>
            <role rolename="manager-gui"/>
            <role rolename="manager-status"/>
            <role rolename="manager-script"/>
            <role rolename="manager-jmx"/>
            <user username="tomcatadmin" password="Foxconn99" roles="admin-gui,manager-gui,manager-status,manager-script,manager-jmx" />

            </tomcat-users>  #在此行前加入上面六行

                配置完后,重新启动tomcat服务生效

                [[email protected] ~]# /usr/local/include/tomcat8/bin/shutdown.sh
                [[email protected] ~]# /usr/local/include/tomcat8/bin/startup.sh


            配置context.xml文件

                [[email protected] conf]# find / -name context.xml
                /usr/local/include/tomcat8/webapps/host-manager/META-INF/context.xml    #修改访问IP列表
                /usr/local/include/tomcat8/webapps/manager/META-INF/context.xml         #修改访问IP列表
                /usr/local/include/tomcat8/conf/context.xml     #此处不用修改
 

             修改/usr/local/include/tomcat8/webapps/host-manager/META-INF/context.xml,添加“192.\d+.\d+.\d+”代表可远程访问该服务器的IP地址段,192...*(此处注意正则表达式)
 

                [[email protected] conf]# vim /usr/local/include/tomcat8/webapps/host-manager/META-INF/context.xml
                <?xml version="1.0" encoding="UTF-8"?>
                <Context antiResourceLocking="false" privileged="true" >
                <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                             allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.\d+\.\d+\.\d+" />
                </Context>
 

            修改/usr/local/include/tomcat8/webapps/host-manager/META-INF/context.xml,添加“192.\d+.\d+.\d+”代表可远程访问该服务器的IP地址段,192...*(此处注意正则表达式)

                [[email protected] conf]# vim /usr/local/include/tomcat8/webapps/manager/META-INF/context.xml
                <?xml version="1.0" encoding="UTF-8"?>
                <Context antiResourceLocking="false" privileged="true" >
                <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                             allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.\d+\.\d+\.\d+" />
                </Context>
 

            重新启动tomcat服务

                [[email protected] conf]# /usr/local/include/tomcat8/bin/catalina.sh stop
                Using CATALINA_BASE:   /usr/local/include/tomcat8
                Using CATALINA_HOME:   /usr/local/include/tomcat8
                Using CATALINA_TMPDIR: /usr/local/include/tomcat8/temp
                Using JRE_HOME:        /usr/local/include/jdk/
                Using CLASSPATH:       /usr/local/include/tomcat8/bin/bootstrap.jar:/usr/local/include/tomcat8/bin/tomcat-juli.jar
                [[email protected] conf]# /usr/local/include/tomcat8/bin/catalina.sh start
                Using CATALINA_BASE:   /usr/local/include/tomcat8
                Using CATALINA_HOME:   /usr/local/include/tomcat8
                Using CATALINA_TMPDIR: /usr/local/include/tomcat8/temp
                Using JRE_HOME:        /usr/local/include/jdk/
                Using CLASSPATH:       /usr/local/include/tomcat8/bin/bootstrap.jar:/usr/local/include/tomcat8/bin/tomcat-juli.jar
                Tomcat started.


        此时可以就可以正常开启tomcat的Manager App、Host Manager、Server Status等页面



4.在Tomcat上部署Jenkins系统
    4.1将下载好的Jenkins的war包放在/data/www/jenkins/文件夹下面

                    [[email protected] jenkins]# pwd
                    /data/www/jenkins
                    [[email protected] jenkins]# ll
                    total 67064
                    -rwxrwxrwx 1 root root 68669812 Apr 11 21:32 jenkins.war


    4.2在/usr/local/include/tomcat8/conf/Catalina/localhost/目录下面新建一个名为jenkins.xml的文件。
           文件内容如下:
                                <Context path="jenkins" docBase="/data/www/jenkins/jenkins.war" />
 

            注意:实际上jenkins的war包直接复制到/usr/local/include/tomcat8/webapps/下,就会自动解压,在webapps目录下面自动生成jenkins目录。而且,在发布war包的应用程式时,最好提前停止Tomcat服务。

以上是关于JDK+Tomcat+Jenkins部署的主要内容,如果未能解决你的问题,请参考以下文章

使用jekins自动构建部署java maven项目(jdk1.7+tomcat7.0+jenkins2.19.3)

Jenkins实践之入门体验

centos部署jenkins

部署jenkins,采用普通用户tomcat来启动tomcat服务

Linux下的Jenkins+Tomcat+Maven+Git+Shell环境的搭建使用(jenkins自动化部署)

在 Tomcat 8上部署 Jenkins