R.Minami

HerokuでWebサーバ構築

2022.07.31

こんにちは。みなみんです。

Herokuでサーバを構築してみた時のメモ。

用いる環境

構築方法

Heroku垢の用意

適当に作っておく。

Heroku CLIを用意

用意しておきましょう。

Dockerを用意しておく

適当にDockerを用意しとけ。

Windowsの場合、後の起動時にWSLが入ってなかったら入れさせられる。

Herokuサーバにデプロイするファイルの用意

必要最低限のファイルを用意します。

Dockerfile

                    
FROM nginx

ENV PORT=8080
COPY html /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

CMD sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'
                    
                

heroku.yml

                    
build:
    docker:
    web: Dockderfile
                    
                

nginx.conf

                    
server {
    listen $PORT;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}
                    
                

index.html

                    Hello World!
                

index.htmlは[現在のディレクトリ]/html/index.htmlに保存します。

最終的には以下のような配置になっているはず。

                    
                        |-Dockerfile
                        |-heroku.yml
                        |-nginx.conf
                        |-html
                           |-index.html
                    
                

コンテナの作成とHerokuサーバへのデプロイ

Dockerが起動していて、ENGINE RUNNINGになっているか確認する。

それから、PowerShellなり、コマンドプロンプトなり、コマンドを叩く

cd [用意したファイルがあるディレクトリ]
heroku create [アプリ名]
heroku login
heroku stack:set container --app [アプリ名]
heroku container:login heroku container:push web --app [アプリ名]
heroku container:release web --app [アプリ名]

おわり

最後に

一連のコマンドをシェルスクリプトなりPowerShellスクリプトなり、自動化しても良さそう。