服务器上的连接堆栈:Vue js前端+;php api+;博士后数据库

服务器上的连接堆栈:Vue js前端+;php api+;博士后数据库,php,postgresql,docker,vue.js,nginx,Php,Postgresql,Docker,Vue.js,Nginx,我正在努力在服务器上设置/部署我的生产环境。我的前端映像是使用Dockerfile.static构建的 我的nginx配置文件 server { listen 80; server_name www.xxx.org; location / { root /usr/share/nginx/html; try_files $uri $uri/ /index.html; proxy_pass http://backend:900

我正在努力在服务器上设置/部署我的生产环境。我的前端映像是使用Dockerfile.static构建的

我的nginx配置文件

server {
    listen 80;
    server_name www.xxx.org;

    location / {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
        proxy_pass http://backend:9000/;
    }
}
fontend服务是通过我的compose文件中的此代码段设置的

frontend:
    image: gitlab-registry.cern.ch/xxx/xxx/static:latest
    ports:
        - 80:80
    networks:
        - frontend
        - backend
当我在浏览器中进入域时,将提供静态文件。但我无法启动后端连接。只有404

以下是我的php API的Dockerfile:

FROM bitnami/php-fpm:latest
LABEL maintainer="xxx"
COPY . /var/www/html
WORKDIR /var/www/html/public
以及完整的撰写文件:

version: '3'
services:
    backend:
        image: gitlab-registry.cern.ch/xxx/xxx:latest
        expose:
            - 9000
        networks:
            - backend

    frontend:
        image: gitlab-registry.cern.ch/xxx/xxx/static:latest
        ports:
            - 80:80
        networks:
            - frontend
            - backend


    mtg-db:
        container_name: mtg-db
        image: postgres:latest
        env_file:
            - database/database.env
        ports:
            - 5432:5432
        volumes:
             - db_data:/var/lib/postgresql/data
        networks:
            - backend

 volumes:
     db_data:
         driver: local
         driver_opts:
             o: bind
             type: none
             device: home/mtg_web_data/

networks:
    frontend:
    backend:
version: '3'
services:
    backend:
        image: gitlab-registry.cern.ch/xxx:latest
        ports:
            - 9000:9000
        volumes:
            - /home/backend_logs:/var/www/MTGWeb/logs
        networks:
            - backend

    frontend:
        image: gitlab-registry.cern.ch/xxx/static:latest
        ports:
            - 80:80
        networks:
            - backend


    mtg-db:
        container_name: mtg-db
        image: postgres:latest
        env_file:
            - database/database.env
        ports:
            - 5432:5432
        volumes:
            - /home/mtg_web_data:/var/lib/postgresql/data
        networks:
            - backend

volumes:
    db_data:
    backend_logs:

networks:
    backend:
在我的后端容器中,我有一个需要提供的index.php文件。在我的开发环境中,我刚刚使用了php开发服务器,该文件的内容如下所示:

<?php


require $_SERVER['DOCUMENT_ROOT']."/../src/api.php";

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET,POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$url = parse_url($_SERVER['REQUEST_URI']);

if (!isset($url["query"])) {
    $url["query"] = NULL;
}

$API = new Api($_SERVER["REQUEST_METHOD"], $url["path"], $url["query"]);
$API->processRequest();

?>

Api类包含所有端点,并最终处理所有查询并从数据库获取数据

问题1)在我的前端容器中,要从中获取的正确URL是什么?据我所知,容器是通过docker网络后端连接的。所以我试过了http://backend:9000/ +uri_路径

问题2)如何设置后端容器?因为我觉得我错过了什么。需要提供index.php文件。。。我是否需要另一个nginx容器来完成此任务,并将我的php-API集成到该容器中


问题3)我的nginx配置是否正确,或者我是否也遗漏了一些内容?

我认为,对于没有部署经验的人来说,了解如何实现这一点的基本示例可能会很有用。所以,我想提供这样一个例子,如果我要改进我的解决方案,我的下一步将是什么。欢迎反馈

前端容器
  • 服务于静态,这是通过构建vue js项目获得的
  • Dockerfile.static用于构建容器
nginx服务器的配置文件取自。。。我只需要为我的前端执行的URL请求添加代理传递,以便从数据库中获取数据

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  www.xxx.org;
    location / {
      root   /usr/share/nginx/html;
      index  index.html;
      try_files $uri $uri/ /index.html;
    }
    location /sets {
      proxy_pass http://backend:9000;
    }
    location /cards {
      proxy_pass http://backend:9000;
    }
    location /types {
      proxy_pass http://backend:9000;
    }
    location /supertypes {
      proxy_pass http://backend:9000;
    }
    location /uploads {
      proxy_pass http://backend:9000;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
}
  • http://backend:9000 这是因为我正在定义一个容器网络,其中我的三个容器相互通信,称为后端,主机端口9000连接到后端容器的端口9000(请参见docker compose)

  • 对于我的前端请求,有了所有这些代理传递,在我的代码中生成的实际请求url地址将减少到“/”(在开发模式中,我将使用类似于http://localhost:8000/)

  • 作为下一步,我将在配置()中添加SSL配置

后端/API 此Dockerfile用于构建后端容器

FROM php:7.4-cli
LABEL maintainer="xxx"
RUN apt-get update
# install pgsql libary which is required in my case
RUN apt-get install -y libpq-dev \
  && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pdo pdo_pgsql pgsql
COPY . /var/www/MTGWeb
WORKDIR /var/www/MTGWeb/public
# start php development server which listens to port 9000
CMD ["php", "-S", "0.0.0.0:9000"]
  • 使用开发服务器肯定不是最优的,但对于像我这样的小规模项目来说,它似乎是最简单的解决方案(下一步,应该用另一个nginx web服务器来取代它)
分贝 我正在使用标准的postgres映像来构建DB容器。为了在后端和DB容器之间建立DB连接,您必须使用DB服务的名称作为主机名(在我的例子中,您可以在docker compose文件中看到mtg DB)

部署 我正在使用gitlab ci构建我的前端和后端映像。我的私有ssh密钥作为环境变量添加到我的repo中,以允许自动访问服务器。在我的主分支中推送一些东西之后,图像被构建,拉到服务器上,然后一切都被重新设置

stages:
    - build-statics
    - build-images
    - deploy_staging

build-statics:
    image: node:latest
    stage: build-statics
    script:
        - cd app
        - npm ci
        - npm run build
    artifacts:
        paths:
            - app/dist
    only:
        - master

backend-image:
    stage: build-images
    tags:
        - docker-image-build
    script:
        - ""
    dependencies: []
    variables:
        TO: $CI_REGISTRY_IMAGE:latest
        DOCKER_FILE: Dockerfile
    only:
        - master

static-image:
    stage: build-images
    tags:
        - docker-image-build
    script:
        - ""
    dependencies:
        - build-statics
    variables:
        TO: $CI_REGISTRY_IMAGE/static:latest
        DOCKER_FILE: Dockerfile.static
    only:
        - master

before_script:
    - apt-get update
    - apt-get -y install git
    - 'which ssh-agent || ( apt-get install openssh-client )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

deploy_staging:
    image: ubuntu:latest
    stage: deploy_staging
    script:
        - ssh root@xxx.net "cd .. && cd var/www/mtg-web/ && docker pull gitlab-registry.cern.ch/xxx && docker pull gitlab-registry.cern.ch/xxx/static && docker-compose up -d"
    only:
        - master
给你!这是一个完整的工作示例,演示了如何使用js前端框架(如vue js、基于php的API和postgres DB)在服务器上设置3个容器堆栈

stages:
    - build-statics
    - build-images
    - deploy_staging

build-statics:
    image: node:latest
    stage: build-statics
    script:
        - cd app
        - npm ci
        - npm run build
    artifacts:
        paths:
            - app/dist
    only:
        - master

backend-image:
    stage: build-images
    tags:
        - docker-image-build
    script:
        - ""
    dependencies: []
    variables:
        TO: $CI_REGISTRY_IMAGE:latest
        DOCKER_FILE: Dockerfile
    only:
        - master

static-image:
    stage: build-images
    tags:
        - docker-image-build
    script:
        - ""
    dependencies:
        - build-statics
    variables:
        TO: $CI_REGISTRY_IMAGE/static:latest
        DOCKER_FILE: Dockerfile.static
    only:
        - master

before_script:
    - apt-get update
    - apt-get -y install git
    - 'which ssh-agent || ( apt-get install openssh-client )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

deploy_staging:
    image: ubuntu:latest
    stage: deploy_staging
    script:
        - ssh root@xxx.net "cd .. && cd var/www/mtg-web/ && docker pull gitlab-registry.cern.ch/xxx && docker pull gitlab-registry.cern.ch/xxx/static && docker-compose up -d"
    only:
        - master
version: '3'
services:
    backend:
        image: gitlab-registry.cern.ch/xxx:latest
        ports:
            - 9000:9000
        volumes:
            - /home/backend_logs:/var/www/MTGWeb/logs
        networks:
            - backend

    frontend:
        image: gitlab-registry.cern.ch/xxx/static:latest
        ports:
            - 80:80
        networks:
            - backend


    mtg-db:
        container_name: mtg-db
        image: postgres:latest
        env_file:
            - database/database.env
        ports:
            - 5432:5432
        volumes:
            - /home/mtg_web_data:/var/lib/postgresql/data
        networks:
            - backend

volumes:
    db_data:
    backend_logs:

networks:
    backend: