- Docker Engine (Linux, Windows, AWS, Azure)
- Docker Compose
- Docker Images (Nginx)
- SSL Certificates file (.crt or .pem)
- Private Key file (.key)
.nginx-docker/
|____docker-compose.yaml
|____nginx/
| |____ssl/
| | |____www_sixcert_co.key
| | |____www_sixcert_co.pem
| |____conf.d/
| |____vhost-sixcert_co.conf
|____public/
|____index.php
-
หลังจากที่ทำการติดตั้ง
Docker Engine
เรียบร้อยแล้วให้ทำการตรวจสอบความพร้อมใช้งานโดย run command$ docker --version Docker version 18.09.0, build 4d60db4
-
ให้ทำการติดตั้ง
docker-compose
เพิ่มเติมเพื่อใช้สำหรับจัดการและส่ัง run docker container ได้อย่างมีประสิทธิภาพและสะดวกยิ่งกว่าการใช้งาน command ผ่านdocker engine
โดยตรง ตรวจสอบการติดตั้งโดย run command$ docker-compose --version docker-compose version 1.23.2, build 1110ad01
-
ให้ทำการสร้าง directory ต่างๆตาม Project directory layout ด้านบนภายในเครื่อง Host ที่ได้ทำการติดตั้ง docker-engine ไว้ประกอบด้วย
- docker-compose.yaml (เก็บ config ต่าง container)
- nginx/ssl/ (เก็บไฟล์ ssl certificates และ private key)
- nginx/conf.d/ (เก็บไฟล์ nginx configuration)
- public/ (เก็บไฟล์ข้อมูลต่างของเว็บไซต์เช่น .php)
-
ทำการกำหนดค่า container ที่ต้องการใช้งานลงในไฟล์
docker-compose.yaml
ดังตัวอย่างversion: '2' services: # Nginx webserver nginx: image: nginx:latest restart: always volumes: - ./nginx/conf.d:/etc/nginx/conf.d:ro - ./nginx/ssl:/etc/nginx/ssl:ro ports: - "80:80" - "443:443" volumes_from: - php # PHP-FPM for compile .php file php: image: php:7.2-fpm-alpine restart: always volumes: - ./public:/var/www/html
คำอธิบาย
- กำหนด nginx container โดยใช้ image version ล่าสุดและทำการ map volumes
./nginx
จากเครื่อง host ที่ run docker-engine เข้าไปใน nginx container เพื่อสามารถอ่านไฟล์ configuration, ssl certificate และ private key จากภายใน container ได้ - ในที่นี้เราได้ทำการเพิ่ม php-fpm container เพิ่มเติมขึ้นมาเพื่อให้เหมือนกับการใช้งานจริงมากยิ่งขึ้น โดยให้ทำการ map volumes
./public
เข้าไปใน container ของ php-fpm ให้อยู่ภายใต้/var/www/html/
เพื่อให้ php-fpm สามารถประมวลผลไฟล์.php
เว็บไซต์ของเราได้
- กำหนด nginx container โดยใช้ image version ล่าสุดและทำการ map volumes
-
นำไฟล์ ssl certificate (.pem or .crt) ที่ได้รับจาก SSL Providers และ private key (.key) ไปวางไว้ที่โฟล์เดอร์
./nginx/ssl/
-
สร้างไฟล์ nginx configuration (virtual host) ตัวอย่างเช่น
vhost-<<domain>>.conf
และวางไว้ในโฟล์เดอร์./nginx/conf.d/
-
โดยกำหนด
server_name
เป็นชื่อโดเมนของเว็บไซต์ที่ต้องการ และกำหนดssl_certificate
และssl_certificate_key
ให้ชี้ไปที่ไฟล์ certificate และ private key ภายใต้/etc/nginx/ssl/
ซึ่งจะเป็น path ภายใน container ที่ได้ทำการ map volumes ไว้จาก docker host ตามขั้นตอนที่ 4... server_name sixcert.co www.sixcert.co; ssl_certificate /etc/nginx/ssl/www_sixcert_co.pem; ssl_certificate_key /etc/nginx/ssl/www_sixcert_co.key; ...
-
ทำการ redirect จาก
http
ไปที่https
ดังนี้โดยกำหนดserver_name
และreturn 301 https://<<domain>>$request_uri;
... server { listen 80; listen [::]:80; server_name sixcert.co www.sixcert.co; return 301 https://www.sixcert.co$request_uri; }
<< Nginx Configuration>>
server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; server_name sixcert.co www.sixcert.co; ssl_certificate /etc/nginx/ssl/www_sixcert_co.pem; ssl_certificate_key /etc/nginx/ssl/www_sixcert_co.key; root /var/www/html; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php { fastcgi_index index.php; fastcgi_pass php:9000; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } server { listen 80; listen [::]:80; server_name sixcert.co www.sixcert.co; return 301 https://www.sixcert.co$request_uri; }
-
-
ทำการ start nginx และ php container ดัวย command ดังต่อไปนี้
$ docker-compose up -d Creating network "nginx-docker_default" with the default driver Creating nginx-docker_php_1 ... done Creating nginx-docker_nginx_1 ... done
หมายเหตุ: เครื่อง docker-engine ที่ยังไม่เคยทำการ download images มาก่อนให้รอ docker-engine ทำการ pull image จาก Docker Hub สักครู่
-
ตรวจสอบสถานะ container ที่ได้สร้างไว้ดังนี้
$ docker-compose ps Name Command State Ports ---------------------------------------------------------------------------------------------------------- nginx-docker_nginx_1 nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp nginx-docker_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
ให้สังเกตุตรงคอลัมน์
State
จะต้องแสดงเป็นUp
ทุก container หากไม่แสดงให้ตรวจสอบ log ของ container โดย command$ docker-compose logs <<container>>
-
ทดสอบเข้าเว็บไซต์ที่ได้ทำการสร้างผ่าน Web Browser
- ตรวจสอบการติดตั้ง SSL Certificate โดยการกดสัญลักษ์รูปกุญบน Address bar ของ Web Browser!