sugizakikun / docker-laravel

ポートフォリオ
https://sakopi.site
1 stars 0 forks source link

HTTPS通信を可能にしたい #9

Closed sugizakikun closed 1 month ago

sugizakikun commented 1 month ago

目的

スクリーンショット 2024-08-24 13 12 39

参考にする記事

  1. httpで公開したWebサーバーをhttps通信できるようにする
  2. EC2とRoute53だけでHTTPS Webサイトを構築!Cerbotを使用します ⇦ 一番コスパ良さそうなのでこっちを採用

手順

  1. 失敗して取り返しが付かなくなっちゃぁまずいので検証用のEC2サーバーをもう一台建てる ⇨元々あったEC2サーバーから作成したAMIをもとにEC2を新規作成する
  2. 参考記事をもとにHTTPS通信を可能にする
  3. 2が成功すればsakopi.siteと紐づいているEC2で参考記事の手順んでHTTPS化を行う
sugizakikun commented 1 month ago

ということで以下の記事を参考にEC2の分身を作る (勉強も兼ねて)

起動中のEC2からAMIを取得する

sugizakikun commented 1 month ago

作成手順

  1. Elastic IP44.195.xxx.xxxと紐づいているEC2サーバーAのAMIを作成
  2. 1で作成したAMIからEC2サーバーBを作成する
  3. 2で作成したEC2サーバにElasticIP(35.153.xxx.xxx)を紐付ける
  4. route53のホストゾーンの設定画面にて, Aレコードの値に3のElasticIP(35.153.xxx.xxx)を貼り付ける
スクリーンショット 2024-08-23 20 05 10
  1. nslookup sakopi.siteで検証用に作成したEC2のElastic IP(35.153.xxx.xxx)にドメインが紐づいていることを確認する
スクリーンショット 2024-08-23 20 07 23
sugizakikun commented 1 month ago

CertbotをEC2サーバー(Amazon Linux 2)にインストールする方法

  1. urllib3 のバージョンをダウングレードして、OpenSSL 1.0.2k と互換性があるバージョンに戻す。

    sudo pip3 install 'urllib3<2'
  2. certbotをインストール

    sudo pip3 install certbot
  3. certbot の動作確認

[ec2-user@ip-10-0-1-41 ~]$ certbot --version
certbot 2.7.4
sugizakikun commented 1 month ago

https://wingdoor.co.jp/blog/%E3%80%90amazon-linux-2%E3%80%91-laravel%E3%81%8C%E5%8B%95%E3%81%8F%E7%92%B0%E5%A2%83%E3%82%92%E6%A7%8B%E7%AF%89%E3%81%99%E3%82%8B/

上の記事を参考にする。

sugizakikun commented 1 month ago
certbot certonly \
    --webroot \
    -w /srv/certbot \
    -d sakopi.site \
    -m [ワイのメールアドレス] \
    --agree-tos

を打つと[Errno 13] Permission denied: '/var/log/letsencrypt/letsencrypt.log' Either run as root, or set --config-dir, --work-dir, and --logs-dir to writeable pathsと怒られまくるので、sudoをつけてcertbotへのパスを明記した上で再度実行してみる

ちなみにcertbotへのパスの調べ方はこれ↓

which certbot
sugizakikun commented 1 month ago

大成功👏👏👏👏

[ec2-user@ip-10-0-1-41 ~]$ sudo /usr/local/bin/certbot certonly \
>     --webroot \
>     -w /srv/certbot \
>     -d sakopi.site\
>     -m sugizakikun@gmail.com \
>     --agree-tos
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Python 3.7 support will be dropped in the next planned release of Certbot - please upgrade your Python version.
Requesting a certificate for sakopi.site

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/sakopi.site/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/sakopi.site/privkey.pem
This certificate expires on 2024-11-22.
These files will be updated when the certificate renews.

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sugizakikun commented 1 month ago

/etc/nginx/conf.d/docker-laravel.confにlisten 443の設定を追記

修正前

server {
    listen 80;
    server_name sakopi.site;

    root /var/www/docker-laravel/src/public;  # `public` ディレクトリをルートに設定
    index index.php index.html;
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;

    client_max_body_size 15m;

    location ^~ /.well-known/acme-challenge/ {
        root /srv/certbot;
        default_type "text/plain";
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;  # `index.php` にフォールバック
    }

    location ~ \.php$ {
        try_files $uri =404;  # `index.php` をここでフォールバックするのではなく、リク>エストされた PHP ファイルのみ処理
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_read_timeout 180;
    }
}
sugizakikun commented 1 month ago

以下を/etc/nginx/conf.d/docker-laravel.confに追記する

server {
    listen 443 ssl;
    server_name sakopi.site;

    ssl_certificate /etc/letsencrypt/live/sakopi.site/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sakopi.site/privkey.pem;

    root /var/www/docker-laravel/src/public; 
    index index.php index.html;
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;

    client_max_body_size 15m;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_read_timeout 180;
    }
}
sugizakikun commented 1 month ago

以下のコマンドで変更を反映する

sudo systemctl restart nginx
sugizakikun commented 1 month ago

おし!いけたべさ!!!!

スクリーンショット 2024-08-24 13 54 49
sugizakikun commented 1 month ago

以上の手順で同様に44.195.xxx.xxxにも適用をしてみる (非効率と思われそうだけど何回もやった方が定着する)