• 03-jenkins集成环境配置


    1)持续集成环境-Jenkins插件管理

    Jenkins本身不提供很多功能,我们可以通过使用插件来满足我们的使用。例如从Gitlab拉取代码,使用
    Maven构建项目等功能需要依靠插件完成。接下来演示如何下载插件。

    a.修改Jenkins插件下载地址

    Jenkins国外官方插件地址下载速度非常慢,所以可以修改为国内插件地址:
    Jenkins->Manage Jenkins->Manage Plugins,点击Available
    image.png

    cd /var/lib/jenkins/updates
    sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json 
    sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
    
    • 1
    • 2
    • 3

    最后,Manage Plugins点击Advanced,把Update Site改为国内插件下载地址

    https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
    
    • 1

    image.png

    b.下载中文汉化插件

    Jenkins->Manage Jenkins->Manage Plugins,点击Available,搜索"Chinese"
    Localization: Chinese (Simplified)
    image.png

    c.Jenkins用户权限管理

    安装Role-based Authorization Strategy插件

    开启权限全局安全配置
    image.png

    d.创建角色

    在系统管理页面进入 Manage and Assign Roles
    image.png

    • Global roles:Global Roles适用于Jenkins中的所有项目,并覆盖您在“Item Roles”中指定的任何内容。也就是说,当您授予角色“Global Roles”中的“Job Read”权限时,则无论您在“Item Roles”中指定的内容如何,都允许该角色读取所有作业。默认情况下,您应该能够看到一个admin用户具有了所有的权限
    • Item roles:项目作业的权限,我们可以使用正则表达式对同类作业匹配。例如:我的流水线项目名称都是按照test-开头的,test-pipeline-service、test-pipeline-web。如果具有良好的项目命名规范,划分权限会更加容易一些。
    • Node roles:代理节点的权限也是可以按照Item Roles通过正则表达式配置,不过一般我们都会在流水线指定好相关的agent名称或者标签不会进行权限的控制,当然如果你需要对代理节点的配置进行权限控制,也是可以在这里实现的。

    创建角色
    我们添加以下三个角色:

    • baseRole:该角色为全局角色。这个角色需要绑定Overall下面的Read权限,是为了给所有用户绑定最基本的Jenkins访问权限。注意:如果不给后续用户绑定这个角色,会报错误:用户名 is missing the Overall/Read permission
    • role1:该角色为项目角色。使用正则表达式绑定"role1.*",意思是只能操作role1开头的项目。
    • role2:该角色也为项目角色。绑定"role2.*",意思是只能操作role2开头的项目。image.png

    e.创建用户

    在系统管理页面进入 Manage Users
    分别创建两个用户:lily/123456,lucy/123456
    image.png

    f.给用户分配角色:

    系统管理页面进入Manage and Assign Roles,点击Assign Roles
    绑定规则如下:
    lily用户分别绑定baseRole和role1角色
    lucy用户分别绑定baseRole和role2角色
    image.png

    g.创建项目测试权限

    使用管理员创建两个项目
    role1-test1
    role2-test1
    image.png

    结果为:
    lily用户登录,只能看到role1项目
    lucy用户登录,只能看到role2项目
    image.png

    image.png

    2)持续集成环境-Jenkins凭证管理

    凭据可以用来存储需要密文保护的数据库密码、Gitlab密码信息、Docker私有仓库密码等,以便
    Jenkins可以和这些第三方的应用进行交互。

    a.安装Credentials Binding插件

    要在Jenkins使用凭证管理功能,需要安装Credentials Binding插件

    安装完成之后:多了个菜单
    image.png

    • Username with password:用户名和密码
    • SSH Username with private key: 使用SSH用户和密钥
    • Secret file:需要保密的文本文件,使用时Jenkins会将文件复制到一个临时目录中,再将文件路径设置到一个变量中,等构建结束后,所复制的Secret file就会被删除。
    • Secret text:需要保存的一个加密的文本串,如钉钉机器人或Github的api token
    • Certificate:通过上传证书文件的方式

    常用的凭证类型有:
    Username with password(用户密码)
    SSH Username with private key(SSH密钥)

    b.安装Git插件和Git工具

    image.png

    # 安装
    yum install git -y 
    # 安装后查看版本
    git --version 
    
    • 1
    • 2
    • 3
    • 4

    c.添加用户密码类型凭证

    Dashboard->zengqingfa->凭据->用户->全局凭据 (unrestricted)
    root/12345678
    zhangsan/12345678
    image.png

    d.测试凭证是否可用

    创建项目:freestyle-project01
    image.png

    配置gitlab信息:
    image.png

    保存即可。
    点击构建:
    image.png

    [root@k8snode1 ~]# cd /var/lib/jenkins/workspace/
    [root@k8snode1 workspace]# ll
    总用量 0
    drwxr-xr-x 4 root root 148 320 21:44 freestyle-project01
    drwxr-xr-x 2 root root   6 320 21:44 freestyle-project01@tmp
    [root@k8snode1 workspace]# cd freestyle-project01
    [root@k8snode1 freestyle-project01]# ll
    总用量 16
    -rw-r--r-- 1 root root 2636 320 21:44 email.html
    -rw-r--r-- 1 root root 1374 320 21:44 Jenkinsfile
    -rw-r--r-- 1 root root  620 320 21:44 pom.xml
    -rw-r--r-- 1 root root    0 320 21:44 README.md
    -rw-r--r-- 1 root root  579 320 21:44 sonar-project.properties
    drwxr-xr-x 3 root root   18 320 21:44 src
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    e.SSH密钥类型

    SSH免密登录示意图
    image.png

    使用root用户生成公钥和私钥
    ssh-keygen -t rsa -C "登录gitlab的邮箱"
    
    • 1

    eg>

    [root@k8snode1 ssh]# ssh-keygen -t rsa -C "zengqingfa_java@163.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:sp/E+DzfXdSp6NYyESKqyVd293uPE4xM+CFtV9feb/E zengqingfa_java@163.com
    The key's randomart image is:
    +---[RSA 2048]----+
    |                .|
    |                +|
    |           o   oo|
    |       . .o.= ..=|
    |      o S .*.= .*|
    |     . B . o= +.E|
    |  . o = + ..+. o.|
    |   + . =...= +.+ |
    |    .   =oo.+.=.o|
    +----[SHA256]-----+
    [root@k8snode1 ssh]# cd /root/.ssh
    [root@k8snode1 .ssh]# ll
    总用量 8
    -rw------- 1 root root 1679 320 21:49 id_rsa
    -rw-r--r-- 1 root root  405 320 21:49 id_rsa.pub
    [root@k8snode1 .ssh]# cat id_rsa
    -----BEGIN RSA PRIVATE KEY-----
    MIIEpAIBAAKCAQEAu5WrCLwizd/Jc4+CE55V5L7TVqkGhxqAv9V+0x51nRtTe
    xH0S8fGyJVJRXCWhTnr/9ej0qMpkggRG+ELo4nhohymF++YvHo/MvtAQ1INEr8ug
    YlskdLxYBL3XveOvMqljnwoUoO/OlwN6DpsMHyABxHm2mWgpWzv6VSgRNnExVW/Y
    FYdjZKUAzGO2GloGiWn74jRj9/RXJMKYydYlmUUDTxczhUm5Twu4EULLzYdXWf9d
    Sm31wKguvn9GuBjfSVC143Sai2WECMtQvIsbEqPwIzD5kskxovE7EpEOk9JlxETZ
    Y2m3UYDQhyiAVcY5P5lAbmmZTJDmh9rhVnmIRQIDAQABAoIBAQCQe1EHI1FzXMDf
    X1OXM63xabGzywcfYfnm8Q6/Yf3uGhCn/+GtWeCztcPO9z5vSRSbTCXGFK4NLAaK
    Fh1Lrs2NUczA/nySbtMwKPY8qimsZYcMGDzqROUpfMAWttprCemxjcKLhsoow0xO
    CIGEqmuFtbNmz5CTTCU+fk3sNTY8xVS5PMTdAt4qylR0hV9RXW31ERDeSYDa5k8t
    q2NawFVBpyW7U7hqa+HIYcIR7C+aF08P5xk9yXgagOJUv+7anWGA9I2bJDNvIC7N
    Ivl13Ob0uQd4Gz6n3Wi48CRfJ49ZPaM44FBzFSkqrouyxu/+TQhwtFtARqPI2Adm
    CXSWNz/pAoGBAONwZ71vihNHIhKiGWCkZrCsRaOs9VU/nT8pQijaO1RKrikjnHdc
    7ZcLixBysomc9fgjqW/oAiZq71/KrPloV8+k2DPRuQBwwxHXqhlDsPVxhiQGJBtY
    X3MPvneQ40wxlYhVT9jGXf/A5ZX0y1MF3yjf+d7K+jzMDidjm20+DAoGBANMk
    CvGR+om1iveqNxVKzFvyd7NJGr/yfpSCMCuU04HJIqWJBXE4X835RzZk8yU4RZ4v
    J1BybFkZAC5CGqTk0zoHS0Rufei8eo3LaArecWK2bU69ZdlamLlW5MHTjbw2dJak
    gSRXHI2fzVVc6C7xIcDoGuxg4hEodzeZXn/uyDaXAoGBAK6BVFd4+UwJTA9X6HjK
    KzBrPZNz5VjfUltf4K9Poi3ujupptGexGWrHJJAeMPZ9RAx78Ek4jk3Dxp6M92L1
    uEpNhMVdqMC5xDIwcwandWMdE3sYayPADpxN/CnpNT0E3mKYcoc0zHKrInyIMmkk
    gp94V+PWjyjKJT1S/bU6jBLZAoGASDo1MCwiy4b58f0deMAspmuMfR1N9Ems+XqL
    pez0aZp3qq7z2KZTR6+1p6ri7mGQlytERKXP4K7HvAtK+At7IoeqPM+bbMlFF0p0
    3fr4SK9lNyFdPPxb8FMEUDXhb3gla/78WoUeNGhtIn10w9IjHM2JoEEINJxmI/Po
    OtVhUiMCgYALMV4hc5h2WhFaAe/bnt1ao5RdGo/qzvFZA/U61lrdYjvGOnzP8PYx
    8oSiP/kiS/+xaDf50Jvz/PbsuIy7twA4fwEC7foypE6jBQ26r59s5q7hbYozZ/Iu
    KDYUywdPwLGuxVP/19wB3f2KvMEFG26oNJfxvjwZTjWPFpuoK6TIfA==
    -----END RSA PRIVATE KEY-----
    [root@k8snode1 .ssh]# cat id_rsa.pub 
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA7lasIvCLN38lzj4ItqQTnlXkvtNWqQaHGoC/1X7THnWdG1N7EfRLx8bIlUlFcJaFOev/16PSoymSCBEb4QujieGiHKYX75i8ej8y+0BDUg0Svy6BiWyR0vFgEvde9468yqWOfChSg786XA3oOmwwfIAHEebaZaClbO/pVKBE2cTFVb9gVh2NkpQDMY7YaWgaJafviNGP39FckwpjJ1iWZRQNPFzOFSblPC7gRQsvNh1dZ/11KbfXAqC6+f0a4GN9JULXjdJqLZYQIy1C8ixsSo/AjMPmSyTGi8TsSkQ6T0mXERNljabdRgNCHKIBVxjk/mUBuaZlMkOaH2uFWeYhF zengqingfa_java@163.com
    [root@k8snode1 .ssh]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    id_rsa.pub:公钥,复制到gitlab平台配置ssh-key
    id_rsa:私钥,复制到jenkins平台配置jenkins凭据

    把生成的公钥放在Gitlab中

    image.png

    验证jenkins容器是否能拉取项目代码

    拷贝ssh路径
    image.png

    在Jenkins中添加凭证,配置私钥

    image.png

    测试构建:
    image.png

    [root@k8snode1 .ssh]# cd /var/lib/jenkins/workspace/
    [root@k8snode1 workspace]# ll
    总用量 0
    drwxr-xr-x 4 root root 148 320 21:44 freestyle-project01
    drwxr-xr-x 2 root root   6 320 22:15 freestyle-project01@tmp
    drwxr-xr-x 4 root root 148 320 23:00 freestyle-project02-ssh
    drwxr-xr-x 2 root root   6 320 23:00 freestyle-project02-ssh@tmp
    [root@k8snode1 workspace]# cd freestyle-project02-ssh
    [root@k8snode1 freestyle-project02-ssh]# ll
    总用量 16
    -rw-r--r-- 1 root root 2636 320 23:00 email.html
    -rw-r--r-- 1 root root 1374 320 23:00 Jenkinsfile
    -rw-r--r-- 1 root root  620 320 23:00 pom.xml
    -rw-r--r-- 1 root root    0 320 23:00 README.md
    -rw-r--r-- 1 root root  579 320 23:00 sonar-project.properties
    drwxr-xr-x 3 root root   18 320 23:00 src
    [root@k8snode1 freestyle-project02-ssh]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3)持续集成环境-Maven安装和配置

    在Jenkins集成服务器上,我们需要安装Maven来编译和打包项目。

    a.安装Maven

    上传mavenjar包

    # 递归创建目录
    mkdir -p /opt/maven 
    
    cd /opt/maven
    
    # 解压
    tar -xzf apache-maven-3.6.2-bin.tar.gz 
    
    cd /opt/maven/apache-maven-3.6.2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    b.配置环境变量

    vi /etc/profile
    
    # 配置环境变量
    export JAVA_HOME=/usr/java/jdk1.8.0_171-amd64 
    export MAVEN_HOME=/opt/maven/apache-maven-3.6.2
    export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
    
    # 配置生效
    source /etc/profile 
    
    # 查找Maven版本
    mvn -v 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    eg>

    [root@k8snode1 jdk1.8.0_171-amd64]# vim /etc/profile
    [root@k8snode1 jdk1.8.0_171-amd64]# source /etc/profile
    [root@k8snode1 jdk1.8.0_171-amd64]# mvn -v
    Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
    Maven home: /opt/maven/apache-maven-3.6.2
    Java version: 1.8.0_171, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_171-amd64/jre
    Default locale: zh_CN, platform encoding: UTF-8
    OS name: "linux", version: "3.10.0-862.11.6.el7.x86_64", arch: "amd64", family: "unix"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    c.全局工具配置关联JDK和Maven

    查看linux中jdk的安装路径

    [root@zqf apache-maven-3.6.2]# which java
    /usr/bin/java
    [root@zqf apache-maven-3.6.2]# cd /usr/bin/java
    -bash: cd: /usr/bin/java: 不是目录
    [root@zqf apache-maven-3.6.2]# ls -lrt /usr/bin/java
    lrwxrwxrwx. 1 root root 22 46 22:41 /usr/bin/java -> /etc/alternatives/java
    [root@zqf apache-maven-3.6.2]# ls -lrt /etc/alternatives/java
    lrwxrwxrwx. 1 root root 71 46 22:41 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/jre/bin/java
    [root@zqf apache-maven-3.6.2]# cd /usr/lib/jvm
    [root@zqf jvm]# ll
    总用量 0
    drwxr-xr-x. 3 root root 17 46 22:41 java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64
    lrwxrwxrwx. 1 root root 21 46 22:41 jre -> /etc/alternatives/jre
    lrwxrwxrwx. 1 root root 27 46 22:41 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
    lrwxrwxrwx. 1 root root 35 46 22:41 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
    lrwxrwxrwx. 1 root root 49 46 22:41 jre-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64 -> java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/jre
    lrwxrwxrwx. 1 root root 29 46 22:41 jre-openjdk -> /etc/alternatives/jre_openjdk
    [root@zqf jvm]# ll
    总用量 0
    drwxr-xr-x. 3 root root 17 46 22:41 java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64
    lrwxrwxrwx. 1 root root 21 46 22:41 jre -> /etc/alternatives/jre
    lrwxrwxrwx. 1 root root 27 46 22:41 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
    lrwxrwxrwx. 1 root root 35 46 22:41 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
    lrwxrwxrwx. 1 root root 49 46 22:41 jre-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64 -> java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/jre
    lrwxrwxrwx. 1 root root 29 46 22:41 jre-openjdk -> /etc/alternatives/jre_openjdk
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Jenkins->Global Tool Configuration->JDK->新增JDK,配置如下:

    /usr/java/jdk1.8.0_171-amd64
    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/jre
    
    • 1
    • 2

    image.png

    # 创建本地仓库目录
    [root@k8snode1 apache-maven-3.6.2]# cd /opt/maven/apache-maven-3.6.2
    
    # 创建仓库地址
    [root@k8snode1 apache-maven-3.6.2]# mkdir repository
    [root@k8snode1 apache-maven-3.6.2]# cd repository/
    [root@k8snode1 repository]# pwd
    /opt/maven/apache-maven-3.6.2/repository
    
    # 修改仓库地址,配置阿里云私服地址
    vim /opt/maven/apache-maven-3.6.2/conf/settings.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    eg>

    <localRepository>/opt/maven/apache-maven-3.6.2/repository</localRepository>
    
    • 1

    阿里云私服地址:

      <mirrors>
        
    <mirror>
        <id>nexus-aliyunid>
        <mirrorOf>centralmirrorOf>
        <name>Nexus aliyunname>
        <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
    mirror>
      mirrors>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    d.测试Maven是否配置成功

    使用之前的gitlab密码测试项目,修改配置
    image.png

    mvn clean package
    
    • 1

    image.png

     > git rev-list --no-walk 27f1d24c38d459ecf40cdd07c4fdc9584bdb533b # timeout=10
    [test02] $ /bin/sh -xe /tmp/jenkins4976375680064281078.sh
    + mvn clean package
    [ERROR] Could not create local repository at /root/repo -> [Help 1]
    [ERROR] 
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LocalRepositoryNotAccessibleException
    Build step 'Execute shell' marked build as failure
    
    #jenkins用户非root用户,为jenkins用户,需要修改jenkins的用户为root
    
    [root@zqf apache-maven-3.6.2]# pwd
    /opt/maven/apache-maven-3.6.2
    [root@zqf apache-maven-3.6.2]# ll
    total 28
    drwxr-xr-x.  2 root       root          97 Apr  8 12:41 bin
    drwxr-xr-x.  2 root       root          42 Apr  8 12:41 boot
    drwxrwxr-x.  3 zengqingfa zengqingfa    63 Apr  8 19:47 conf
    drwxrwxr-x.  4 zengqingfa zengqingfa  4096 Apr  8 12:41 lib
    -rw-rw-r--.  1 zengqingfa zengqingfa 12846 Aug 27  2019 LICENSE
    -rw-rw-r--.  1 zengqingfa zengqingfa   182 Aug 27  2019 NOTICE
    -rw-rw-r--.  1 zengqingfa zengqingfa  2533 Aug 27  2019 README.txt
    drwxr-xr-x. 11 root       root         160 Apr  8 19:48 repository
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4)持续集成环境-Tomcat安装和配置

    服务器:192.168.12.131

    a.安装Tomcat8.5

    上传到服务器

    # 安装JDK(已完成) -y表示遇到选择的情况选择y
    yum install java-8-openjdk* -y
    # 解压
    tar -xzf apache-tomcat-8.5.47.tar.gz 
    #  创建目录
    mkdir -p /opt/tomcat
    # 移动文件
    mv /root/apache-tomcat-8.5.47/* /opt/tomcat 
    # 启动tomcat
    /opt/tomcat/bin/startup.sh 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    eg>

    [root@zengqingfa hello-world-01]# cd /root
    [root@zengqingfa ~]# ls
    2000                           apache-tomcat-8.5.47.tar.gz          jdk-8u171-linux-x64.tar.gz    repo
    apache-maven-3.6.2             a.txt                                jenkins-2.278-1.1.noarch.rpm  sonarqube-6.7.4.zip
    apache-maven-3.6.2-bin.tar.gz  harbor-offline-installer-v1.9.2.tgz  nginx-1.14.2.tar.gz
    [root@zengqingfa ~]# tar -xzf apache-tomcat-8.5.47.tar.gz
    [root@zengqingfa ~]# mkdir -p /opt/tomcat
    [root@zengqingfa ~]# mv /root/apache-tomcat-8.5.47/* /opt/tomcat
    [root@zengqingfa ~]# /opt/tomcat/bin/startup.sh
    [root@zengqingfa tomcat]# ./bin/startup.sh
    Using CATALINA_BASE:   /opt/tomcat
    Using CATALINA_HOME:   /opt/tomcat
    Using CATALINA_TMPDIR: /opt/tomcat/temp
    Using JRE_HOME:        /usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64/jre
    Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
    Tomcat started.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    启动报错:jdk11默认没有jre
    找不到目录:

    [root@iZbp1f7db1tbkqgwvhjwl9Z bin]# cat ../logs/catalina.out
    /opt/tomcat/bin/catalina.sh: line 467: /usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64/jre/bin/java: No such file or directory
    /opt/tomcat/bin/catalina.sh: line 467: /usr/lib/jvm/java-11-openjdk/jre/bin/java: No such file or directory
    /opt/tomcat/bin/catalina.sh: line 467: /usr/lib/jvm/java-11-openjdk/jre/bin/java: No such file or directory
    /opt/tomcat/bin/catalina.sh: line 467: /usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64/jre/bin/java: No such file or directory
    
    • 1
    • 2
    • 3
    • 4
    • 5

    安装jre:

    [root@iZbp1f7db1tbkqgwvhjwl9Z tomcat]# cd /usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64
    [root@iZbp1f7db1tbkqgwvhjwl9Z java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64]# ll
    total 40
    drwxr-xr-x  2 root root 4096 Feb 19 16:40 bin
    drwxr-xr-x  4 root root 4096 Feb 14 23:49 conf
    drwxr-xr-x  4 root root 4096 Feb 14 23:50 demo
    drwxr-xr-x  3 root root 4096 Feb 14 23:49 include
    drwxr-xr-x  2 root root 4096 Feb 14 23:50 jmods
    drwxr-xr-x 73 root root 4096 Jan 22 23:39 legal
    drwxr-xr-x  8 root root 4096 Feb 14 23:50 lib
    -rw-r--r--  1 root root 1212 Jan 22 23:39 release
    drwxr-xr-x  4 root root 4096 Feb 14 23:50 sample
    drwxr-xr-x  2 root root 4096 Feb 14 23:49 tapset
    [root@iZbp1f7db1tbkqgwvhjwl9Z java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64]# jlink --module-path jmods --add-modules java.desktop --output jre
    [root@iZbp1f7db1tbkqgwvhjwl9Z java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64]# ll
    total 44
    drwxr-xr-x  2 root root 4096 Feb 19 16:40 bin
    drwxr-xr-x  4 root root 4096 Feb 14 23:49 conf
    drwxr-xr-x  4 root root 4096 Feb 14 23:50 demo
    drwxr-xr-x  3 root root 4096 Feb 14 23:49 include
    drwxr-xr-x  2 root root 4096 Feb 14 23:50 jmods
    drwxr-xr-x  7 root root 4096 Feb 19 16:41 jre
    drwxr-xr-x 73 root root 4096 Jan 22 23:39 legal
    drwxr-xr-x  8 root root 4096 Feb 14 23:50 lib
    -rw-r--r--  1 root root 1212 Jan 22 23:39 release
    drwxr-xr-x  4 root root 4096 Feb 14 23:50 sample
    drwxr-xr-x  2 root root 4096 Feb 14 23:49 tapset
    [root@iZbp1f7db1tbkqgwvhjwl9Z java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64]# cd jre/
    [root@iZbp1f7db1tbkqgwvhjwl9Z jre]# ll
    total 24
    drwxr-xr-x 2 root root 4096 Feb 19 16:41 bin
    drwxr-xr-x 3 root root 4096 Feb 19 16:41 conf
    drwxr-xr-x 3 root root 4096 Feb 19 16:41 include
    drwxr-xr-x 7 root root 4096 Feb 19 16:41 legal
    drwxr-xr-x 5 root root 4096 Feb 19 16:41 lib
    -rw-r--r-- 1 root root   94 Feb 19 16:41 release
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    配置环境变量:
    vim /etc/profile

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
    export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
    export PATH=$PATH:${JAVA_PATH}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    生效:source /etc/profile
    继续启动:报错

    [root@iZbp1f7db1tbkqgwvhjwl9Z tomcat]# cat ./logs/catalina.out
    NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
    WARNING: Unknown module: java.rmi specified to --add-opens
    Exception in thread "main" java.lang.NoClassDefFoundError: java/util/logging/Logger
            at org.apache.juli.logging.DirectJDKLog.<init>(DirectJDKLog.java:68)
            at org.apache.juli.logging.DirectJDKLog.getInstance(DirectJDKLog.java:188)
            at org.apache.juli.logging.LogFactory.getInstance(LogFactory.java:131)
            at org.apache.juli.logging.LogFactory.getInstance(LogFactory.java:155)
            at org.apache.juli.logging.LogFactory.getLog(LogFactory.java:210)
            at org.apache.catalina.startup.Bootstrap.<clinit>(Bootstrap.java:52)
    Caused by: java.lang.ClassNotFoundException: java.util.logging.Logger
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
            at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            ... 6 more
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    从JDK9开始, 引入了模块化, 可以根据自己的需要生成一个runtime, 所以就不再单独提供jre了, 你需要什么模块, 自己生成就好了, 可以自己定制jre的. https://www.zhihu.com/question/352542780/answer/872011539
    # 我这里没有生成jre, 只是在/etc/profile文件中指定jre的位置为jdk的位置, tomcat启动失败的问题解决!
    修改如下:

    JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.10.0.9-0.el7_9.x86_64
    JRE_HOME=$JAVA_HOME
    CLASSPATH=$JAVA_HOME/lib
    PATH=$JRE_HOME/bin:$JAVA_HOME/bin:$PATH
    export PATH JAVA_HOME CLASSPATH
    
    • 1
    • 2
    • 3
    • 4
    • 5

    启动成功。

    访问:http://121.196.169.191:8080/
    image.png

    b.配置Tomcat用户角色权限

    默认情况下Tomcat是没有配置用户角色权限的
    image.png

    vim /opt/tomcat/conf/tomcat-users.xml
    
    • 1
    <tomcat-users> 
    	<role rolename="tomcat"/> 
    	<role rolename="role1"/> 
    	<role rolename="manager-script"/> 
    	<role rolename="manager-gui"/> 
    	<role rolename="manager-status"/> 
    	<role rolename="admin-gui"/> 
    	<role rolename="admin-script"/> 
    	<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-status,tomcat,admin-gui,admin-script"/> 
    tomcat-users>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    用户和密码都是:tomcat
    注意:为了能够刚才配置的用户登录到Tomcat,还需要修改以下配置

    vim /opt/tomcat/webapps/manager/META-INF/context.xml
    
    • 1
     
    
    • 1
    • 2

    重启Tomcat,访问测试

    /opt/tomcat/bin/shutdown.sh 
    /opt/tomcat/bin/startup.sh 
    
    • 1
    • 2

    image.png
    http://121.196.169.191:8080/
    [外链图片转存中…(img-kjJBY4dD-1666275041474)]

    b.配置Tomcat用户角色权限

    默认情况下Tomcat是没有配置用户角色权限的
    [外链图片转存中…(img-iUHOE7Yj-1666275041474)]

    vim /opt/tomcat/conf/tomcat-users.xml
    
    • 1
    <tomcat-users> 
    	<role rolename="tomcat"/> 
    	<role rolename="role1"/> 
    	<role rolename="manager-script"/> 
    	<role rolename="manager-gui"/> 
    	<role rolename="manager-status"/> 
    	<role rolename="admin-gui"/> 
    	<role rolename="admin-script"/> 
    	<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-status,tomcat,admin-gui,admin-script"/> 
    tomcat-users>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    用户和密码都是:tomcat
    注意:为了能够刚才配置的用户登录到Tomcat,还需要修改以下配置

    vim /opt/tomcat/webapps/manager/META-INF/context.xml
    
    • 1
     
    
    • 1
    • 2

    重启Tomcat,访问测试

    /opt/tomcat/bin/shutdown.sh 
    /opt/tomcat/bin/startup.sh 
    
    • 1
    • 2

    [外链图片转存中…(img-wrvwZiDB-1666275041475)]

  • 相关阅读:
    uniapp 模拟请求自身的JSON模拟数据
    GitHub怎么创建仓库上传文件
    深度强化学习中Double DQN算法(Q-Learning+CNN)的讲解及在Asterix游戏上的实战(超详细 附源码)
    Python 潮流周刊#21:如何提升及测量 Python 代码的性能?
    ESP8266-Arduino网络编程实例-远程固件升级
    【LeetCode】51、N皇后
    CCES软件开发ADSP-21489的详解
    第二章 经典同步练习作业
    < Linux > 进程概念(2)
    CloudCompare——计算点云到点云的距离
  • 原文地址:https://blog.csdn.net/zengqingfa123/article/details/127436396