Ubuntu
AWS Ubuntu + Docker 설정
AWS EC2 환경에서 Ubuntu와 Docker 환경을 설정하는 과정에 대한 기록입니다.

인스턴스 환경: AWS EC2 Ubuntu 16.04 LTS (t3.nano, ebs 20g)


[마스터 인스턴스 세팅]
1. 루트 패스워드 설정
1
2
3
$ sudo su
$ passwd root
~ 0000
cs

2. 스왑 메모리 설정 (메모리 부족할 때)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ free -m
// check memory size
 
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=8
//t3a.nano memory 512MB *2 =  Swap file size 1024MB(bs*8)
 
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
 
$ free -m
// check attached swap memory size
 
$ sudo nano /etc/fstab
 
/swapfile swap swap defaults 0 0
// Write above line to fstab for set swapfile when the instance is reboot.
cs

3. 패키지 업데이트 및 업그레이드
1
2
$ sudo apt update
$ sudo apt dist-upgrade
cs

4. 로케일 타임존 설정
1
2
$ sudo dpkg-reconfigure tzdata
~ Select Asia/Seoul
cs

5. php, 컴포저 설치
1
2
3
4
5
$ sudo apt install -y curl php7.4-cli php7.4-curl
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/bin/composer
$ sudo apt install -y php7.4-bcmath php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip php7.4-mysql
$ composer global require -n laravel/installer
cs

6. node, npm 설치
1
2
3
4
5
$ sudo apt install -y nodejs npm
$ sudo npm install -g n
$ sudo n lts
$ sudo npm install -g npm
$ hash -d npm
cs


[docker 세팅]
1. docker 설치
1
2
3
4
5
6
$ curl -fsSL https://get.docker.com/ | sudo sh
$ sudo usermod -aG docker $USER
$ sudo chmod 666 /var/run/docker.sock
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
cs

2. docker 폴더 세팅
- 폴더 구조
1
2
3
4
5
6
7
8
9
docker
├── docker-compose.yml
├── nginx
│   └── default.conf
├── php
│   ├── Dockerfile
│   └── php.ini
└── proxy
    └── nginx.conf
cs

- docker-compose.yml
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
version: '3'
services:
  php:
    restart: on-failure
    build:
      context: .
      dockerfile: php/Dockerfile
    volumes:
      - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini
      - /var/www:/var/www
  proxy:
    image: nginx:stable
    restart: on-failure
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./proxy/nginx.conf:/etc/nginx/nginx.conf
      - /var/log/nginx:/var/log/nginx
      - /etc/letsencrypt:/etc/letsencrypt
  web:
    image: nginx:stable
    restart: on-failure
    expose:
      - "8000"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /var/www:/var/www
cs

- nginx/default.conf
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
server {
    listen 8000;
    client_max_body_size 0;
 
    server_name _;
 
    root /var/www/web/public;
    // public is laravel root
 
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
 
    index index.html index.htm index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
 
    location ~ /\.(?!well-known).* { deny all; }
 
    error_log /var/log/nginx/api_error.log;
    access_log /var/log/nginx/api_access.log;
}
cs

- php/Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# step 1
FROM php:7.2-fpm
# step 2
WORKDIR /root
RUN apt-get update
# step 3
RUN apt-get install -y zlib1g-dev libzip-dev libpng-dev openssl libc-client-dev$
RUN pecl install mcrypt-1.0.2
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl
RUN docker-php-ext-enable mcrypt
RUN docker-php-ext-install zip pdo pdo_mysql bcmath gd imap
#php-7.4 gd path change
#RUN docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-freetype=/usr/include/
 
# step 4
EXPOSE 9000
CMD ["php-fpm"]
 
cs

- php/php.ini
1
2
3
4
5
date.timezone = Asia/Seoul
upload_max_filesize = 100M
post_max_size = 100M
 
extension=mcrypt.so
cs

- proxy/nginx.conf
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
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {
    client_max_body_size 0;
 
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    proxy_connect_timeout  1d;
    proxy_send_timeout     1d;
    proxy_read_timeout     1d;
 
    #web
    upstream web {
        server web:8000;
    }
 
    server {
        listen        80;
        server_name   _;
 
        location / {
            proxy_pass         http://web;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    #add new upstream/server here
 
    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;
    include             /etc/nginx/conf.d/*.conf;
}
 
 
cs

3. docker 실행
1
$ docker-compose up -d
cs



2년전에는 이 코드로 잘 도커 구성이 되었는데 ubuntu도 16버전 지원중단에 따라서 본문 내용 그대로 세팅하는게 이제는 힘들고, 18버전을 사용하면 php 버전이 무조건 7.4이상으로 강제되어 별도의 추가 설정등이 필요하다.

그래서 이 내용을 기반으로 세팅중 나오는 에러메시지에 따라 계속적으로 부분 조율을 해주어야한다.

하지만 큰 가이드라인은 언제든지 필요하니까, 개인 노트에 있던 내용을 한번 옮겨 담아봤다.
D.2023-06-02 V.1,353