Cách bắt đầu Blog ma của bạn gần như MIỄN PHÍ trên đám mây

Apr 09 2022
Hướng dẫn cách thiết lập blog Ghost bảo mật bằng Docker trong Google Cloud Platform với chi phí gần như bằng không mỗi tháng bằng chương trình Always Free Tier. Giới thiệu Xin chào các bạn, bài hướng dẫn này sẽ hướng dẫn các bạn cách thiết lập một blog Ghost tự lưu trữ gần như miễn phí trong Google Cloud Platform! 😎 Chúng tôi sẽ sử dụng chương trình Google Cloud Always Free Tier để giữ cho chi phí cam kết hàng tháng của chúng tôi thực sự thấp so với các dịch vụ khác ngoài thị trường (tự lưu trữ hoặc được quản lý hoàn toàn) đang tính phí ít nhất 5–9 USD mỗi tháng.

Hướng dẫn cách thiết lập blog Ghost bảo mật bằng Docker trong Google Cloud Platform với chi phí gần như bằng không mỗi tháng bằng chương trình Always Free Tier.

Giới thiệu

Xin chào các bạn, bài này sẽ hướng dẫn các bạn cách thiết lập một blog Ghost tự lưu trữ gần như miễn phí trong Google Cloud Platform ! 😎 Chúng tôi sẽ sử dụng chương trình Google Cloud Always Free Tier để giữ cho chi phí cam kết hàng tháng của chúng tôi thực sự thấp so với các dịch vụ khác ngoài thị trường (tự lưu trữ hoặc được quản lý hoàn toàn) đang tính phí ít nhất 5–9 USD mỗi tháng . Tôi đang trả khoảng 0,05 USD mỗi tháng để duy trì blogmà bạn hiện đang đọc bằng cách sử dụng chính xác phương pháp này tại thời điểm viết bài này! (Tuyên bố từ chối trách nhiệm: Tôi có thể đổi sang máy có thông số kỹ thuật cao hơn hoặc nền tảng khác trong tương lai gần nếu lưu lượng truy cập blog của tôi tiếp tục tăng nhưng nó phục vụ tôi rất tốt tại thời điểm viết bài này! 🤓) Lưu ý rằng chi phí của bạn có thể thay đổi, tùy thuộc vào tổng lưu lượng truy cập trang web hàng tháng.

Chúng tôi rất khuyến khích bạn tham khảo các video hướng dẫn trực tuyến của tôi tại đây để hiểu rõ hơn về những gì từng bước thực hiện bên dưới vì tôi sẽ thực hiện bản trình diễn trực tiếp từng bước bằng cách sử dụng miền giả. MIỄN PHÍ trong 1 tháng nếu bạn đăng ký bằng liên kết của tôi. 🤗

Tài nguyên

Giới hạn sử dụng Bậc miễn phí của Google Cloudhttps://cloud.google.com/free/docs/gcp-free-tier/#compute

Hình ảnh của Google trên Nền tảng đám mây Compute Engine Giới hạn sử dụng cấp miễn phí

Chi phí tiềm năng duy nhất mà chúng tôi sẽ phải trả là chi phí đầu ra mạng nếu chúng tôi vượt quá chi phí đầu ra mạng 1GB miễn phí, tùy thuộc vào lưu lượng truy cập blog của bạn, do đó đây là một giải pháp gần như MIỄN PHÍ. 😉

Bật API công cụ máy tính

Tìm kiếm Công cụ Máy tính -> Bật API Công cụ Máy tính

Tạo đĩa

Tạo một ổ lưu trữ liên tục riêng biệt cho mục đích lưu trữ dữ liệu. Nó sẽ giúp bạn tiết kiệm rất nhiều thời gian nếu bạn quyết định di chuyển hoặc nâng cấp blog ma của mình trong tương lai! 😁

  1. Điều hướng đến Compute Engine, Trong Storage, nhấp vào Disks
  2. TẠO Đĩa
  3. Dưới tên, ghostvol
  4. Vị trí, Khu đơn. Vùng, miền Tây nước Mỹ1. Khu vực không quan trọng
  5. Loại nguồn đĩa, Đĩa trống
  6. Loại đĩa, Đĩa liên tục tiêu chuẩn. Chúng tôi chọn điều này vì Google cung cấp đĩa liên tục tiêu chuẩn 30GB / tháng!
  7. Kích thước, 10GB là đủ cho 90% các trường hợp sử dụng. Bạn luôn có thể gia hạn nó sau này nên không phải lo lắng!
  8. (Tùy chọn nhưng rất nên làm) Bật lịch chụp nhanh, TẠO LỊCH
    1. Tên, lịch 3 ngày trước
    2. Tần suất lập lịch, Hàng ngày
    3. Thời gian bắt đầu (UTC), chọn bất kỳ lúc nào bạn muốn hoặc cứ để nguyên như mặc định
    4. Tự động xóa ảnh chụp nhanh sau, 3. Có nghĩa là bạn chỉ giữ bản sao lưu 3 ngày qua là quá đủ!
    5. Quy tắc xóa, Giữ ảnh chụp nhanh!
    6. Nhấp vào TẠO
  9. Cuối cùng bấm vào TẠO
  1. Điều hướng đến Compute Engine, Trong các phiên bản VM, CREATE INSTANCE
  2. Dưới tên - ma
  3. Khu vực - us-west1, Zone - us-west1-a (Chọn cùng khu vực với Đĩa của chúng tôi ở trên)
  4. Under Machine configuration, Series — E2 (default), Machine type — Choose e2-micro. (Make sure you choose e2-micro to prevent unnecessary charge!)
  5. Under Boot disk, click CHANGE, Operating system — Debian, Version — Debian GNU/Linux 10 (buster), Boot disk type — Standard persistent disk, Size — 10GB. 10GB should be more than enough for our use case!
  6. Under Firewall, click Allow HTTP traffic and and Allow HTTPS traffic
  7. Under Disk, click on ATTACH EXISTING DISK, choose ghostvol. Everything else leave it as default, SAVE.
  8. Click on CREATE! 😻

This is to prevent your IP changes when you restart your VM or change your VM in the near future.

  1. Search for Exernal IP addresses in Google Cloud Platform Search console
  2. Click on RESERVE for your newly created VM
  3. Name — Put anything you want, I just put my domain name (e.g. chenming-io)

You have to mount the disk onto the VM so you can use it later.

# List the attached disk and format the disk, normally its /dev/sdb
 
sudo lsblk
# DANGER ZONE # Format the disk if it's a new disk! Don't do this to existing DISK!!! It will wipe out all your data!
 
sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb
# Make the mount point and mount the disk to the mountpoint
 
sudo mkdir -p /mnt/ghostvol 
sudo mount -o discard,defaults /dev/sdb /mnt/ghostvol
# Make sure can read/write of the volume
 
sudo chmod a+w /mnt/ghostvol
# Check that you can navigate to the directory now 
# You should see a lost+found folder when you type ls
 
cd /mnt/ghostvol
# Check the id of the disk, copy down the UUID 
# e.g. UUID="xxxxxxxxx"
 
sudo blkid /dev/sdb
# Backup
 
sudo cp /etc/fstab /etc/fstab.backup
# Replace the UUID with the id of the disk in previous step 
 
sudo nano /etc/fstab 
 
UUID=389615e9-8183-4846-a809-f55e0404383a /mnt/ghostvol ext4 discard,defaults,nofail 0 2
 
# Click Ctrl+X -> Y -> Enter
# Double check everything ok
 
cat /etc/fstab
# Optional, can do a reboot to see everything is working fine. It should auto mount when it reboots! 
# Wait for 2 minutes and reconnect to the VM again
 
sudo reboot

This step is specifically designed for : Access to the blog using https://(your_domain_name) and https://www.(your_domain_name) will be redirected to https://(your_domain_name)

In a nutshell:

http://(your_domain_name) -> https://(your_domain_name)
http://www.(your_domain_name) -> https://(your_domain_name)

https://www.(your_domain_name) -> https://(your_domain_name)
https://(your_domain_name) -> https://(your_domain_name)

You may need to adjust accordingly if you want to use https://www.(your_domain_name) instead of https://(your_domain_name)

As explained above, personally I prefer my reader to type less word to access to my website e.g. when they type “chenming.io” in the browser, it will auto redirect to “https://chenming.io". Therefore, I prefer to use this approach, it really boils down to personal preference, there is some slight difference between www and non www domain from SEO perspective but I think it should be okay as long as you are pointing all the links back to the same address in the end.

This guide get most of the inspiration and idea here : https://pentacent./nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71

# Create nginx configuration folder
 
mkdir -p /mnt/ghostvol/nginx/conf

Remember to replace your_domain_name with your own domain

# cd to the directory and create the file
 
cd /mnt/ghostvol/nginx/conf
nano ghost.conf
# Paste the below content into the file 
# Please replace all the your_domain_name with the domain you purchase
 
server {
    listen 80;
    server_name your_domain_name;
    server_tokens off;
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
    location / {
        return 301 https://$host$request_uri;
    }
}
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name your_domain_name;
    gzip off;
    ssl_certificate /etc/letsencrypt/live/your_domain_name/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain_name/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://ghost:2368;
        proxy_hide_header X-Powered-By;
    }
    location ~ /.well-known {
        allow all;
    }
    client_max_body_size 50m;
}

Remember to replace your_domain_name with your own domain

# cd to the directory and create the file
cd /mnt/ghostvol/nginx/conf
nano www.conf
# Paste the below content into the file 
# Please edit all the your_domain_name with the domain you purchase
 
server {
    listen 80;
    server_name www.your_domain_name;
    server_tokens off;
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
    location / {
        return 301 https://your_domain_name$request_uri;
    }
}
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.your_domain_name;
    gzip off;
    ssl_certificate /etc/letsencrypt/live/your_domain_name/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain_name/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    location / {
        return 301 https://your_domain_name$request_uri;
    }
    location ~ /.well-known {
        allow all;
    }
    client_max_body_size 50m;
}

Remember to replace domains, email in the script below!

mkdir -p /mnt/ghostvol/create-ghost-blog
 
cd /mnt/ghostvol/create-ghost-blog
 
nano init-letsencrypt.sh
# Please replace domains with your (domain_name) e.g. www.hello.io, hello.com 
# Please replace your email to your email e.g. [email protected]
# You might want to edit your data_path if you are using different mounting point
#!/bin/bash
if ! [ -x "$(command -v docker-compose)" ]; then
  echo 'Error: docker-compose is not installed.' >&2
  exit 1
fi
domains=(hello.io www.hello.io)
rsa_key_size=4096
data_path="/mnt/ghostvol/certbot"
email="[email protected]" # Adding a valid address is strongly recommended
staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits
if [ -d "$data_path" ]; then
  read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
  if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
    exit
  fi
fi
if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
  echo "### Downloading recommended TLS parameters ..."
  mkdir -p "$data_path/conf"
  curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
  curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
  echo
fi
echo "### Creating dummy certificate for $domains ..."
path="/etc/letsencrypt/live/$domains"
mkdir -p "$data_path/conf/live/$domains"
docker-compose run --rm --entrypoint "\
  openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\
    -keyout '$path/privkey.pem' \
    -out '$path/fullchain.pem' \
    -subj '/CN=localhost'" certbot
echo
echo "### Starting nginx ..."
docker-compose up --force-recreate -d nginx
echo
echo "### Deleting dummy certificate for $domains ..."
docker-compose run --rm --entrypoint "\
  rm -Rf /etc/letsencrypt/live/$domains && \
  rm -Rf /etc/letsencrypt/archive/$domains && \
  rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
echo
echo "### Requesting Let's Encrypt certificate for $domains ..."
#Join $domains to -d args
domain_args=""
for domain in "${domains[@]}"; do
  domain_args="$domain_args -d $domain"
done
# Select appropriate email arg
case "$email" in
  "") email_arg="--register-unsafely-without-email" ;;
  *) email_arg="--email $email" ;;
esac
# Enable staging mode if needed
if [ $staging != "0" ]; then staging_arg="--staging"; fi
docker-compose run --rm --entrypoint "\
  certbot certonly --webroot -w /var/www/certbot \
    $staging_arg \
    $email_arg \
    $domain_args \
    --rsa-key-size $rsa_key_size \
    --agree-tos \
    --force-renewal" certbot
echo
echo "### Reloading nginx ..."
docker-compose exec nginx nginx -s reload

chmod u+x /mnt/ghostvol/create-ghost-blog/init-letsencrypt.sh

Chúng tôi sẽ chạy mọi thứ bên trong vùng chứa thay vì cục bộ, điều này là để tách các giải pháp của chúng tôi để dễ dàng nâng cấp / di chuyển trong tương lai gần.

  1. SSH vào máy ảo của bạn
  2. Chạy các lệnh sau để cài đặt docker-soạn

# You can just copy paste most of the commands except for the username
# Make sure you use your own username!!!
sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
# Add Docker’s official GPG key:
 
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Set up the stable repository
 
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install the latest docker engine
 
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
# Add your user to docker and activate group
# Replace user_xyz with your username, can use command "whoami" to
check your username

whoami
 
sudo usermod -aG docker your_username
 
newgrp docker
# You should be able to run this command
 
docker ps
# Install docker compose
 
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 
sudo chmod +x /usr/local/bin/docker-compose

  1. Đi tới nhà cung cấp miền của bạn và cập nhật miền của bạn, chúng tôi trỏ cả miền gốc (@) và miền www tới địa chỉ IP của Máy ảo.
  2. Hình ảnh của Tác giả trên Bản đồ DNS Mẫu từ namecheap.com
Hình ảnh của Tác giả trên Kiểm tra DNS từ dnschecker.org

Thiết lập Docker Compose

Trước tiên, bạn sẽ cần một tệp docker-compile để xác định tất cả các thiết lập. Bạn sẽ tạo tệp này trong / mnt / ghostvol / create-ghost-blog

Vì mục đích nhất quán, tôi đã sử dụng image: ghost: 4.41.3-alpine cho bản demo này, nếu bạn muốn sử dụng phiên bản mới nhất, có thể thay thế nó bằng image: ghost: 4-alpine nhưng tôi sẽ không thể đảm bảo 100%. rằng nó sẽ hoạt động như mong đợi!

Thay thế your_domain_name bằng tên miền của bạn

cd /mnt/ghostvol/create-ghost-blog

nano docker-compose.yml
version: '3.1'
services:
  nginx:
    image: nginx:1.21.6-alpine
    restart: unless-stopped
    volumes:
      - /mnt/ghostvol/nginx/conf/ghost.conf:/etc/nginx/conf.d/ghost.conf:ro
      - /mnt/ghostvol/nginx/conf/www.conf:/etc/nginx/conf.d/www.conf:ro
      - /mnt/ghostvol/certbot/conf/:/etc/letsencrypt/:ro
      - /mnt/ghostvol/certbot/www/:/var/www/certbot/:ro
      - /mnt/ghostvol/www_data:/var/www/html
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - ghost
      - certbot
    networks:
      - nginx
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
  certbot:
    image: certbot/certbot
    restart: unless-stopped
    volumes:
      - /mnt/ghostvol/certbot/conf/:/etc/letsencrypt/:rw
      - /mnt/ghostvol/certbot/www/:/var/www/certbot/:rw
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
ghost:
    image: ghost:4.41.3-alpine
    volumes:
      - /mnt/ghostvol/ghost_data/:/var/lib/ghost/content
    restart: always
    environment:
      database__client: mysql
      database__connection__host: db-mysql
      database__connection__user: root
      database__connection__password: [email protected]
      database__connection__database: ghost
      url: https://your_domain_name
    depends_on:
      - db-mysql
    networks:
      - nginx
      - db_mysql
db-mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: [email protected]
    volumes:
      - /mnt/ghostvol/mysql_data:/var/lib/mysql
    networks:
      - db_mysql
networks:
  db_mysql:
  nginx:

Khi bước này kích hoạt tệp docker-compile, nó sẽ tạo chứng chỉ SSL, tự động tải xuống tất cả các tệp phụ thuộc và khởi động cả blog ma. Hãy để điều kỳ diệu bắt đầu 🎩🐰

cd /mnt/ghostvol/create-ghost-blog 
 
./init-letsencrypt.sh

cd /mnt/ghostvol/create-ghost-blog 
 
docker-compose up -d

  1. Bây giờ bạn có thể đăng nhập vào blog của mình bằng https: // your_domain_name / ghost , đăng ký và bạn đã hoàn tất! 🤗 Bạn có thể bắt đầu hành trình viết lách của mình mà không cần lo lắng nhiều về chi phí!
  2. (Tùy chọn) Bạn có thể chơi vớihttp://your_domain_namehoặc www.your_domain_name hoặchttp://www.your_domain_namehoặchttps://www.your_domain_name,tất cả họ sẽ chỉ đến cùng một địa chỉ!

© Copyright 2021 - 2023 | vngogo.com | All Rights Reserved