
소개.
이 글에서는 Ubuntu 22.04에서 Docker Compose, Nginx, Apache, PHP 8.1, MariaDB 및 Let’s Encrypt를 통해 워드프레스 웹사이트를 만드는 방법을 진행하겠습니다. 이 설정은 현재 가장 최적의 조합으로 간주되며 우분투 22.04 운영 체제를 사용하는 Google Cloud에서 테스트 중입니다. 설치를 시작하기 전에 아래의 필수 조건을 충족하는지 확인해야 합니다:
5단계로 도커 컴포즈, Nginx, 아파치, SSL을 사용하여 워드프레스 설치하기.
사전 필요사항:
- Ubuntu 22.04에 Docker 설치
- Ubuntu 22.04에 Docker Compose 설치
Step1: 프로젝트가 포함된 디렉토리 만들기
(base) kevin_kidong_lim@streamlit-mpi:~$
(base) kevin_kidong_lim@streamlit-mpi:~$ mkdir wp-project
Step1: Docker Compose YML 파일 만들기
base) kevin_kidong_lim@streamlit-mpi:~$ cd wp-project/
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ ls
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ vi docker-compose.yml
version: "3.9"
services:
wordpress:
container_name: wordpress
image: wordpress:php8.1-apache
restart: always
stdin_open: true
tty: true
environment:
WORDPRESS_DB_HOST: mariadb
WORDPRESS_DB_USER: db_user
WORDPRESS_DB_PASSWORD: db_user_pass
WORDPRESS_DB_NAME: db_name
volumes:
- wordpress_data:/var/www/html
- ./wordpress:/var/www/html
mariadb:
container_name: mariadb
image: mariadb
restart: always
environment:
MYSQL_DATABASE: db_name
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
MYSQL_RANDOM_ROOT_PASSWORD: 'root_pass'
volumes:
- db_data:/var/lib/mysql
nginx:
container_name: nginx
image: nginx:latest
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./certbot/conf:/etc/nginx/ssl
- ./certbot/data:/var/www/html
certbot:
container_name: certbot
image: certbot/certbot:latest
command: certonly --webroot --webroot-path=/var/www/html --email youremail@domain.com --agree-tos --no-eff-email -d domain.com -d www.domain.com
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/logs:/var/log/letsencrypt
- ./certbot/data:/var/www/html
volumes:
db_data:
wordpress_data:
Step 3: Configure Nginx
3단계: Nginx 구성하기 docker-compose.yml 파일의 구성 내용에 따라 nginx/conf 경로 내에 default.conf 파일을 생성해야 하므로 다음 명령을 사용하여 필요한 파일/폴더를 생성합니다.
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ mkdir -p nginx/conf
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ vi nginx/conf/default.conf
server {
listen [::]:80;
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
try_files $uri @apache;
}
location ~ ^/.user.ini {
deny all;
}
location ~* .(svg|svgz)$ {
types {}
default_type image/svg+xml;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location @apache {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~[^?]*/$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~ .php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~/. {
deny all;
access_log off;
log_not_found off;
}
}
Step 4: 도커 컴포즈로 워드프레스 배포
base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ pwd
/home/kevin_kidong_lim/wp-project
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ sudo docker compose up -d
[+] Running 37/44
⠦ mariadb [⣿⣿⣿⣿⣿⣿⣿⣿] 113.1MB / 122.1MB Pulling 5.7s
⠸ 00d679a470c4 Extracting [======================================> … 5.3s
✔ 381fb89d9026 Download complete 0.9s
✔ fd042176e576 Download complete 1.2s
✔ 80b21b96f832 Download complete 1.2s
✔ 310e39ef046d Download complete 1.3s
✔ 8e4136144025 Download complete 3.3s
✔ c187e9145a80 Download complete 1.4s
✔ 7b8cfc1ced45 Download complete 1.5s
⠦ certbot [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 41MB / 43.02MB Pulling 5.7s
✔ 619be1103602 Pull complete 0.5s
✔ 36988be9c68b Pull complete 1.1s
✔ d5717d523ed2 Pull complete 4.4s
✔ 8ad54c2a0aeb Pull complete 4.5s
⠙ 0eb61f1af52e Extracting [========================================> … 5.3s
✔ 348e833b8b69 Download complete 0.4s
✔ 2631f0818f28 Download complete 0.4s
✔ 41af1e9eccb9 Download complete 0.5s
✔ afb1a39d2061 Download complete 0.6s
✔ e9a5d20b014d Download complete 0.6s
✔ 8c1bbc6e3d86 Download complete 0.7s
✔ 5363fba50d83 Download complete 1.0s
⠦ wordpress [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⣿⣿] 187MB / 211.1MB Pulling 5.7s
✔ 09f376ebb190 Already exists 0.0s
✔ e2e881592e80 Pull complete 1.6s
⠇ 31a4c251f36c Extracting [==> … 5.3s
✔ 433084d93d64 Download complete 1.8s
✔ 76dd3deb295a Download complete 2.6s
[+] Running 5/5
✔ Network wp-project_default Created 0.1s
✔ Container certbot_2 Started 1.1s
✔ Container nginx_2 Started 1.3s
✔ Container wordpress_2 Started 1.2s
✔ Container mariadb_2 Started 1.3s
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$
모든 컨테이너가 시작되면 certbot 과 wordpress 의 두 디렉터리가 생성되고 docker-compose.yml 파일과 함께 위치하게 됩니다. 그 안에 있습니다:
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ ls
certbot docker-compose.yml nginx wordpress
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ cd certbot/
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project/certbot$ ls
conf data logs
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project/certbot$ cd ..
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ cd wordpress/
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project/wordpress$ ls
index.php wp-admin wp-config-sample.php wp-includes wp-mail.php xmlrpc.php
license.txt wp-blog-header.php wp-config.php wp-links-opml.php wp-settings.php
readme.html wp-comments-post.php wp-content wp-load.php wp-signup.php
wp-activate.php wp-config-docker.php wp-cron.php wp-login.php wp-trackback.php
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project/wordpress$
cerbot: SSL 인증서와 관련된 파일이 포함되어 있습니다.
wordpress: Contains the source code of your WordPress website.
아래 도커 명령어로 실행중인 이미지 확인
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project/wordpress$ sudo docker ps -a
base) kevin_kidong_lim@streamlit-mpi:~/wp-project/wordpress$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adc28c435b31 wordpress:php8.1-apache "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 80/tcp wordpress_2
f327aa5108cf certbot/certbot:latest "certbot certonly --…" 8 minutes ago Exited (1) 7 minutes ago certbot_2
790974b2d2d4 nginx:latest "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:8099->80/tcp, :::8099->80/tcp, 0.0.0.0:453->443/tcp, :::453->443/tcp nginx_2
d2b83ef98ca8 mariadb "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 3306/tcp mariadb_2
Step 5: Nginx로 SSL 암호화 설정하기
Let’s Encrypt SSL 인증서를 받으면 다음과 같이 default.conf 구성 파일을 편집하여 HTTPS를 구성하고 사이트를 HTTPS로 리디렉션할 수 있습니다:
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ vi nginx/conf/default.conf
server {
listen [::]:80;
listen 80;
server_name domain.com www.domain;
return 301 https://domain.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
return 301 https://www.domain.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name www.domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
root /var/www/html;
index index.php;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
try_files $uri @apache;
}
location ~ ^/.user.ini {
deny all;
}
location ~* .(svg|svgz)$ {
types {}
default_type image/svg+xml;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location @apache {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~[^?]*/$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~ .php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~/. {
deny all;
access_log off;
log_not_found off;
}
}
(base) kevin_kidong_lim@streamlit-mpi:~/wp-project$ sudo docker compose restart nginx
WARN[0000] /home/kevin_kidong_lim/wp-project/docker-compose.yml: version
is obsolete
[+] Restarting 1/1
✔ Container nginx_2 Started