인프라

docker-compose apache2 + php + mysql8 (APM)서버 구축

소행성왕자 2023. 2. 8. 13:10
docker-compose 로 APM 서버 구축

- Apache2, Mysql 프로젝트 경로 설정

mkdir -p ./Project/{apache2,html,mysql}

- docker image pull

docker pull httpd
docker pull php:7.4-fpm
docker pull mysql:8.0

- 이미지 확인

docker images

- docker-compose.yml 생성

version: "3"

services:
  apache2:
    build: ./Project/apache2
    restart: always
    container_name: apache2
    links:
      - mysql
    ports:
      - "9080:80"
      - "9022:22"
    volumes:
      - ./Project/apache2/html/:/var/www/html/
      - ./Project/apache2/conf/httpd.conf/:/usr/local/apache2/conf/httpd.conf
    networks:
      - default
      - apm_net

  mysql:
    image: mysql:8.0
    restart: always
    container_name: mysql
    volumes:
      - ./Project/mysql/data/:/var/lib/mysql/
      - ./Project/mysql/log/:/var/log/mysql/
    ports:
      - "3306:3306"
    environment:
      SERVICE_TAG: dev
      SERVICE_NAME: mysql
    env_file:
      - ./env/.mysql.env
    networks:
      - default
      - apm_net


networks:
  apm_net:
    driver: bridge

- mysql 정보 env 생성

mkdir env
cd env
vi .mysql.env
MYSQL_HOST=localhost
MYSQL_DATABASE=crawlDB
MYSQL_USER=crawl
MYSQL_PASSWORD=crawl
MYSQL_ROOT_PASSWORD=root

- mysql 컨테이너 접속

docker exec -it mysql_컨테이너 bash

- apache2 컨테이너 Dockerfile 생성

vi Project/apache2/Dockerfile

FROM ubuntu:20.04

LABEL Maintainer="naya"

# Avoding user interaction with tzdata
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
RUN apt-get install -y apache2 #install Apache web server (Only 'yes')
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:ondrej/php # For Installing PHP 7.4
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y openssh-server
RUN apt-get install -y net-tools
RUN apt-get install -y php7.4
RUN apt-get install -y php7.4-cli
RUN apt-get install -y php7.4-common
RUN apt-get install -y php7.4-curl
RUN apt-get install -y php7.4-zip
RUN apt-get install -y php7.4-gd
RUN apt-get install -y php7.4-mysql
RUN apt-get install -y php7.4-xml
RUN apt-get install -y php7.4-mbstring
RUN apt-get install -y php7.4-json
RUN apt-get install -y php7.4-intl

EXPOSE 80

CMD ["apachectl", "-D", "FOREGROUND"]

- index.php 파일 생성

vi ./Project/apache2/html/index.php
<?php

echo 'safasf 77<br>';

$conn = mysqli_connect(
        '220.72.212.xxx',
        'crawl',
        'crawl',
        'crawl',
    );

    if(mysqli_connect_error()) {
        echo "Failed to Connect Mysql: ".mysqli_connect_error();
    }
    $sql = "SELECT VERSION()";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_array($result);
    print_r($row["VERSION()"]);




phpinfo();

-  vi .gitlab-ci.yml 생성

  deploy-to-server:
     stage: deploy
     only:
       - main
     script:
       - echo 'hello world!'
       - whoami
       - pwd
       - ssh -t -t 'root@172.29.0.3' "cd /var/www/html && git pull && exit"
     tags:
       - deploy

- docker-compose 실행

docker-compose up --build -d

- docker-compose 중지

docker-compose down

- docker-compose 백그라운드로 시작 (호스트와 컨테이너간에 동기화 됨)

docker-compose up -d