linux上安装前后端运行环境的两种方法——安装包安装及docker安装两种方式

在裸机上安装并部署Java环境、tomcat环境及Nginx负载均衡及Nginx前端项目。

首先安装要安装的是JDK,我选择的版本为jdk-8u161-linux-x64(百度云提取码:hlf5) ,tomcat版本为apache-tomcat-8.5.57 (百度云提取码:temp),nginx版本为nginx-1.18.0。

首先把这些安装包存放在/usr/local/目录下(举个栗子)

  • 首先需要安装的是JDK环境。

    1. 解压jdk-8u161-linux-x64.tar.gz
    tar -zxf /usr/local/jdk-8u161-linux-x64.tar.gz
    
    1. 解压后的目录为jdk1.8.0_161,开始配置环境变量
    vi /etc/profile
    
    1. 先按shift+G跳转至最后一行,再最后面添加(键入i)
    export JAVA_HOME=/usr/local/jdk1.8.0_161
    export PATH=$PATH:$JAVA_HOME/bin
    
    1. 配置完esc,冒号(英文),wq+ENTER退出,使配置文件生效
    source /etc/profile
    
    1. 确认jdk是否安装成功,正确输出Java版本信息则安装成功
    java -version
    
  • 其次安装tomcat。

    1. 解压apache-tomcat-8.5.57.tar.gz
    tar -zxf /usr/local/apache-tomcat-8.5.57.tar.gz
    
    1. 如果需要修改tomcat的启动端口,则需要修改tomcat的server.xml
    vi /usr/local/apache-tomcat-8.5.57/conf/server.xml
    

    ​ 修改第69行

    <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
    

    ​ 将8080修改为想要启动的端口。

    1. 将war包放入webapps目录下。
    mv $war-dir/project.war /usr/local/apache-tomcat-8.5.57/webapps/
    

    ​ 此时服务访问地址为:http://${ip}:${port}/project/

    mv $war-dir/ROOT.war /usr/local/apache-tomcat-8.5.57/webapps/
    

    ​ 此时服务访问地址为:http://${ip}:${port}/

  • nginx安装相对比较麻烦。从官网下载的nginx需要自己编译生成可以用的nginx。

    1. nginx解压到目录。
    tar -zxf /usr/local/nginx-1.18.0.tar.gz
    
    1. nginx依赖于GCC环境,需要提前安装gcc
    #centos下
    yum install -y gcc
    #ubnutu下
    apt-get install -y gcc
    
    1. 在nginx目录下
    cd /usr/local/nginx-1.18.0/
    mkdir /usr/local/nginx/
    ./configuration --prefix=/usr/local/nginx/
    

    ​ 此时可能会报错:

    ​ 错误1:缺少prce库

    #ubnutu下
    apt-get install libpcre3 libpcre3-dev -y
    #centos下
    yum install libpcre3 libpcre3-dev -y
    

    ​ 错误2:缺少zlib库

    #ubnutu下
    apt-get install zlib1g-dev -y
    #centos下
    yum install zlib1g-dev -y
    

    ​ 错误3:缺少OpenSSL库

    #ubnutu下
    apt-get install openssl libssl-dev  -y
    #centos下
    yum install openssl libssl-dev  -y
    
    1. make生成
    make
    make install
    

    ​ 最后在/usr/local/nginx/目录下为编译好的nginx。

    1. 安装需求修改nginx的配置文件
    vi /usr/local/nginx/conf/nginx.conf
    

在虚拟机上直接安装比使用docker安装更加麻烦一点,可以使用docker安装。

  1. 安装docker,启动docker
yum install -y docker

systemctl start docker
  1. docker中搜索可用的tomcat包
docker search tomcat

INDEX       NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/tomcat                        Apache Tomcat is an open source implementa...   2810      [OK]
docker.io   docker.io/tomee                         Apache TomEE is an all-Apache Java EE cert...   80        [OK]
docker.io   docker.io/dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   55                   [OK]
docker.io   docker.io/bitnami/tomcat                Bitnami Tomcat Docker Image                     35                   [OK]
docker.io   docker.io/kubeguide/tomcat-app          Tomcat image for Chapter 1                      29
docker.io   docker.io/consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              17                   [OK]
docker.io   docker.io/cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
docker.io   docker.io/aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                   [OK]
docker.io   docker.io/arm32v7/tomcat                Apache Tomcat is an open source implementa...   10
docker.io   docker.io/maluuba/tomcat7-java8         Tomcat7 with java8.                             6
docker.io   docker.io/rightctrl/tomcat              CentOS , Oracle Java, tomcat application s...   6                    [OK]
docker.io   docker.io/unidata/tomcat-docker         Security-hardened Tomcat Docker container.      4                    [OK]
docker.io   docker.io/amd64/tomcat                  Apache Tomcat is an open source implementa...   2
docker.io   docker.io/arm64v8/tomcat                Apache Tomcat is an open source implementa...   2
docker.io   docker.io/jelastic/tomcat               An image of the Tomcat Java application se...   2
docker.io   docker.io/99taxis/tomcat7               Tomcat7                                         1                    [OK]
docker.io   docker.io/camptocamp/tomcat-logback     Docker image for tomcat with logback integ...   1                    [OK]
docker.io   docker.io/cfje/tomcat-resource          Tomcat Concourse Resource                       1
docker.io   docker.io/chenyufeng/tomcat-centos      tomcat基于centos6的镜像                              1                    [OK]
docker.io   docker.io/oobsri/tomcat8                Testing CI Jobs with different names.           1
docker.io   docker.io/ppc64le/tomcat                Apache Tomcat is an open source implementa...   1
docker.io   docker.io/picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAG...   0                    [OK]
docker.io   docker.io/s390x/tomcat                  Apache Tomcat is an open source implementa...   0
docker.io   docker.io/secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]
docker.io   docker.io/softwareplant/tomcat          Tomcat images for jira-cloud testing            0                    [OK]
  1. docker pull官方的tomcat,默认版本为最新版本,也可以指定版本
docker pull docker.io/tomcat 
docker pull docker.io/tomcat:latest
docker pull docker.io/tomcat:{version}
  1. 启动docker容器
docker run -idt -p 80:8080 -p 443:8443 --name service -v /root/webapps:/usr/local/tomcat/webapps --restart=always -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime
 tomcat

-p为端口映射,前面的端口为本机端口,后面的端口为容器内端口,意思为将docker容器的端口8080映射到本地的80端口,将8443端口映射到443端口,-v为挂载,将/root/webapps挂载在tomcat容器内/usr/local/tomcat/webapps/目录,此时将文件存放到/root/webapps/目录下就相当于存放到容器内,另外一个-v /etc/localtime:/etc/localtime主要用于解决docker容器内时区为UCT的问题,将本地的时区配置文件挂载到容器,保证时区显示正常。--restart=always表示容器退出是项目总数要重启。

  1. nginx与tomcat安装类似,不同的在于docker的启动命令
docker search nginx

INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        13638     [OK]
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1864                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   783                  [OK]
docker.io   docker.io/linuxserver/nginx                  An Nginx container, brought to you by Linu...   126
docker.io   docker.io/bitnami/nginx                      Bitnami nginx Docker Image                      88                   [OK]
docker.io   docker.io/tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   88                   [OK]
docker.io   docker.io/jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   78
docker.io   docker.io/alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from s...   72                   [OK]
docker.io   docker.io/nginxdemos/hello                   NGINX webserver that serves a simple page ...   59                   [OK]
docker.io   docker.io/jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        53                   [OK]
docker.io   docker.io/nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         40
docker.io   docker.io/privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & ...   32                   [OK]
docker.io   docker.io/schmunk42/nginx-redirect           A very simple container to redirect HTTP t...   19                   [OK]
docker.io   docker.io/nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  17
docker.io   docker.io/nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15
docker.io   docker.io/centos/nginx-112-centos7           Platform for running nginx 1.12 or buildin...   13
docker.io   docker.io/centos/nginx-18-centos7            Platform for running nginx 1.8 or building...   13
docker.io   docker.io/raulr/nginx-wordpress              Nginx front-end for the official wordpress...   13                   [OK]
docker.io   docker.io/bitwarden/nginx                    The Bitwarden nginx web server acting as a...   7
docker.io   docker.io/mailu/nginx                        Mailu nginx frontend                            7                    [OK]
docker.io   docker.io/sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats...   7                    [OK]
docker.io   docker.io/bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Con...   6                    [OK]
docker.io   docker.io/flashspys/nginx-static             Super Lightweight Nginx Image                   6                    [OK]
docker.io   docker.io/ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
docker.io   docker.io/wodby/nginx                        Generic nginx                                   1                    [OK]

pull nginx镜像

docker pull docker.io/nginx:lastest

docker run --name nginx-test -p 8080:80 -itd nginx

你可能感兴趣的:(docker,linux,tomcat)