[docker] redmine 세팅

세팅환경

  • CentOS Linux release 7.8.2003 (Core)
  • Docker version 20.10.6

docker-compose.yml

version: '3.2'

services:

  redmine:
    image: redmine
    restart: always
    ports:
      - 8080:3000
    environment:
      REDMINE_DB_MYSQL: db
      REDMINE_DB_PASSWORD: PASSWORD
      REDMINE_SECRET_KEY_BASE: supersecretkey
    
  db:
    image: mysql:5.7
    restart: always
    ports:
     - 3330:3306
    environment:
      MYSQL_ROOT_PASSWORD: PASSWORD
      MYSQL_DATABASE: redmine 
    volumes:
     - db_data:/var/lib/mysql
    command:
     - --character-set-server=utf8mb4
     - --collation-server=utf8mb4_unicode_ci

volumes:
  db_data:

httpd-vhost.conf

<VirtualHost *:80>
  ServerName redmine.ujsstudio.com
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

해당 도메인으로 접속하면 아파치 서버에서 localhost:8080 으로 넘겨줘서 외부에서 접속 가능하게 됨

https://github.com/jisung87kr/docker-redmine

php를 이용한 웹크롤링

의존성

composer.json

{
    "require": {
        "guzzlehttp/guzzle": "^7.0",
        "symfony/dom-crawler": "^5.3",
        "symfony/css-selector": "^5.3"
    }
}

요청과 응답페이지 요소탐색

use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;

$url = 'http://ujsstudio.com';
$client = new Client();
$res = $client->get($url);
$res = $res->getBody();
$html = (string)$res; // 문자열로 형변환
// dom 필터링
$crawler = new Crawler($html);
$nodeValues = $crawler->filter("#primary a")->each(function(Crawler $node, $i){
    return $node->attr('href');
});

페이지네이션 처리

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Symfony\Component\DomCrawler\Crawler;

$result = [];
$client = new Client();

// 페이지 조회 익명함수(제너레이터) 생성
$requests = function ($total) use ($client) {
    $uri = 'http://ujsstudio.com/page';
    for ($i = 0; $i < $total; $i++) {
        yield function() use ($client, $uri, &$i) {
            $uri = $uri.'/'.$i+1;
            return $client->getAsync($uri);
        };
    }
};

$pool = new Pool($client, $requests(10), [
        'concurrency' => 5,
        'fulfilled' => function (Response $response, $index) use (&$result) {
            $res = $response->getBody();
            $html = (string)$res;

            // 요소탐색
            $crawler = new Crawler($html);
            $nodeValues = $crawler->filter("#primary a")->each(function(Crawler $node, $i){
                return $node->attr('href');
            });

            $result[] = $nodeValues;
        },
        'rejected' => function (RequestException $reason, $index) {
            // this is delivered each failed request
        },
]);

$promise = $pool->promise();
$promise->wait();

[php]stdClass object배열로 변환

function arrayCastRecursive($array)
{
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $array[$key] = arrayCastRecursive($value);
            }
            if ($value instanceof stdClass) {
                $array[$key] = arrayCastRecursive((array)$value);
            }
        }
    }
    if ($array instanceof stdClass) {
        return arrayCastRecursive((array)$array);
    }
    return $array;
}

$result = arrayCastRecursive($stdObj);

[docker] redmine 구축하기

docker-compose.yml 작성
https://github.com/jisung87kr/docker-redmine

version: '3.2'

services:

  redmine:
    image: redmine
    restart: always
    ports:
      - 8080:3000
    environment:
      REDMINE_DB_MYSQL: db
      REDMINE_DB_PASSWORD: PASSWORD
      REDMINE_SECRET_KEY_BASE: supersecretkey
    
  db:
    image: mysql:5.7
    restart: always
    ports:
     - 3330:3306
    environment:
      MYSQL_ROOT_PASSWORD: PASSWORD
      MYSQL_DATABASE: redmine 
    volumes:
     - db_data:/var/lib/mysql
    command: #문자셋 지정
     - --character-set-server=utf8mb4
     - --collation-server=utf8mb4_unicode_ci

volumes:
  db_data:

포트개방

docker-comse.yml 에서 지정한 포트가 개방되어 있어야 한다.

apache 설정

<VirtualHost *:80>
  ServerName redmine.ujsstudio.com
  #ProxyRequests Off
  #ProxyPreserveHost On
  ProxyPass / http://localhost:8888/
  ProxyPassReverse / http://localhost:8888/
</VirtualHost>

역프록시 설정을 해서 지정한 포트로 리다이렉트 처리

참고

[docker] systemctl 사용할 수 없을때

도커에서 권한 문제로 systemctl 커맨드를 사용할수 없음
privileged 옵션과 /sbin/init 커맨드를 전달해서 해결

docker run -itd --privileged --name centos7 centos:7 /sbin/init