🐈‍⬛

Laravel SailでURL変更&mkcertを使ったhttps化(OS問わず)

2024/11/25に公開

Laravel SailでURL変更&mkcertを使ったhttps化(OS問わず)

Laravelインストール

composer create-project laravel/laravel:^10.0 example-app-ssl-enable

ディレクトリ作成

mkdir -p nginx/templates
mkdir nginx-cert

CA証明書の発行

cd nginx-cert
mkcert dev.ketsu.app localhost 127.0.0.1

sail・sail-sslインストール

composer require laravel/sail --dev
cd example-app-ssl-enable
./vendor/bin/sail artisan sail:install
./vendor/bin/sail up -d
cp vendor/ryoluo/sail-ssl/nginx/templates/default.conf.template nginx/templates/default.conf.template

hostsに設定を追加

127.0.0.1 127.0.0.1 dev.ketsu.app

.envの書き換え

APP_URLを変更する。

APP_URL=http://localhost

APP_URL=https://dev.ketsu.app

ASSET_URL=https://dev.ketsu.app
VITE_DEV_SERVER=true

docker-compose.ymlへの書き換え

nginxのコンテナを書き換え

        volumes:
            - 'sail-nginx:/etc/nginx/certs'
            - './vendor/ryoluo/sail-ssl/nginx/templates:/etc/nginx/templates'
            - './vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.sh:/docker-entrypoint.d/99-generate-ssl-cert.sh'

        volumes:
            - 'sail-nginx:/etc/nginx/certs'
            - './nginx/templates:/etc/nginx/templates'
            - './vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.sh:/docker-entrypoint.d/99-generate-ssl-cert.sh'
            - './nginx-cert/dev.ketsu.app+2-key.pem:/etc/certs/dev.ketsu.app+2-key.pem'
            - './nginx-cert/dev.ketsu.app+2.pem:/etc/certs/dev.ketsu.app+2.pem'

vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.shの書き換え

KEY=/etc/nginx/certs/server.key
CERT=/etc/nginx/certs/server.pem

KEY=/etc/certs/dev.ketsu.app+2-key.pem
CERT=/etc/certs/dev.ketsu.app+2.pem

nginx/templates/default.conf.templateの書き換え

    ssl_certificate     /etc/nginx/certs/server.pem;
    ssl_certificate_key /etc/nginx/certs/server.key;

    ssl_certificate     /etc/certs/dev.ketsu.app+2.pem;
    ssl_certificate_key /etc/certs/dev.ketsu.app+2-key.pem;

証明書の登録

キーチェーンにpemの登録

Windowsでは、CA証明書を証明書でインポートしてください。
https://atmarkit.itmedia.co.jp/ait/articles/1602/05/news039.html

確認

./vendor/bin/sail down
./vendor/bin/sail up -d

Discussion