LoginSignup
4
6

More than 5 years have passed since last update.

Laravelをherokuにアップし、basic認証をかけるまで

Posted at

タイトルどおり。herokuでLaravel,nginxを使い、Basic認証をかけるまでの手順
前提として、github→CircleCI→herokuという流れを作っている。
環境構築は以下を参照。
LaravelアプリをCircleCIを使ってherokuにデプロイ

手順

herokuやLaravelの準備

このあたりはLaravelアプリをCircleCIを使ってherokuにデプロイでやったので割愛

Procfileを編集

web: vendor/bin/heroku-php-nginx -C nginx_app.conf public/

nginx_app.confを作成

nginx_app.confをプロジェクトルートに作成

nginx_app.conf
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
}

location @rewriteapp {
# rewrite all to index.php
rewrite ^(.*)$ /index.php/$1 last;
}

location ~ ^/(app|app_dev|config)\.php(/|$) {
try_files @heroku-fcgi @heroku-fcgi;
internal;
}

basic認証

環境変数を設定

heroku login
heroku config:set BASIC_AUTH_USERNAME=[ユーザ名]
heroku config:set BASIC_AUTH_PASSWORD=[パスワード]

nginx_app.confを修正

nginx_app.conf
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
auth_basic           "Restricted";
auth_basic_user_file $document_root/../.htpasswd;
}

.htpasswdを作成する

mkdir etc/nginx
mkdir .profile.d
cd .profile.d
vi gen-htpasswd.sh
gen-htpasswd.sh
#!/usr/bin/env bash
set -ex

echo -e "${BASIC_AUTH_USERNAME}:$(perl -le 'print crypt($ENV{"BASIC_AUTH_PASSWORD"}, rand(0xffffffff));')" > /app/.htpasswd

参考

https://devcenter.heroku.com/articles/custom-php-settings
https://mistymagich.wordpress.com/2016/02/12/nginx-basic-authentication-in-php-on-heroku/

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6