개발
AWS 개발 일지

AWS Ubuntu + Docker 설정

1,954 views as of June 2, 2023.
AWS EC2 환경에서 Ubuntu와 Docker 환경을 설정하는 과정에 대한 기록입니다.

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


마스터 인스턴스 세팅

1. 루트 패스워드 설정

$ sudo su
$ passwd root
~ 0000Copy


2. 스왑 메모리 설정 (메모리 부족할 때)

$ 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.Copy


3. 패키지 업데이트 및 업그레이드


$ sudo apt update
$ sudo apt dist-upgradeCopy


4. 로케일 타임존 설정

$ sudo dpkg-reconfigure tzdata
~ Select Asia/SeoulCopy


5. php, 컴포저 설치

$ 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/installerCopy

6. node, npm 설치

$ sudo apt install -y nodejs npm
$ sudo npm install -g n
$ sudo n lts
$ sudo npm install -g npm
$ hash -d npmCopy


docker 세팅

1. docker 설치

$ 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
Copy

2. docker 폴더 세팅

- 폴더 구조
docker
├── docker-compose.yml
├── nginx
│   └── default.conf
├── php
│   ├── Dockerfile
│   └── php.ini
└── proxy
    └── nginx.confCopy

- docker-compose.yml
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/wwwCopy

- nginx/default.conf
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;
}Copy

- php/Dockerfile
# 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"]Copy

- php/php.ini
date.timezone = Asia/Seoul
upload_max_filesize = 100M
post_max_size = 100M
 
extension=mcrypt.soCopy

- proxy/nginx.conf

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;
}Copy


3. docker 실행

$ docker-compose up -dCopy

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

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

하지만 큰 가이드라인은 언제든지 필요하니까, 개인 노트에 있던 내용을 한번 옮겨 담아봤다.
#개발 #AWS #Ubuntu #Docker #설정 #환경
0 개의 댓글
×