LoginSignup
1
2

More than 5 years have passed since last update.

awsでgolangのサーバーを立てる & basic認証

Last updated at Posted at 2017-07-20

ec2のインスタンスを立てるところとgolangのプログラムは割愛させていただきます。
主に、apacheの設定周りの説明をします

apacheの設定

sudo yum install httpd
sudo chkconfig httpd on
sudo /etc/init.d/httpd start

basic認証を設定する
- hogeの部分がbasic認証のユーザ名になります。
- パスワードを聞かれるので、任意のパスワードを設定します
- 複数のユーザを設定したい場合は、-cを抜いてhoge2みたいな感じでユーザを追加します
余談ですが、chromeでbasic認証のテストをする場合、cmd + shift + n でシークレットウィンドウを開いてテストすると楽です

sudo htpasswd -c /etc/httpd/.htpasswd hoge

確認はこちらでできます

cat .htpasswd

  • httpd.confを編集します。hoge.com部分はサーバ名が決まったらそれを
  • ServerNameに指定してください。
  • golangはport8888で設定するものとします

※最低限の設定なので、最低限の設定としてご参照ください

sudo vim /etc/httpd/conf/httpd.conf 

////httpd.confの中身

//ServerNameを変更する。
ServerName hoge.com:80

//VirtualHostを追加する
<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName hoge.com:80
  ServerAlias hoge.com
  ProxyPass / http://localhost:8888/
  ProxyPassReverse / http://localhost:8888/
  <Proxy *>
   AllowOverride all
   Order deny,allow
   Allow from all
   AuthType Basic
   AuthUserFile /etc/httpd/.htpasswd
   AuthName "Please enter your ID and password"
   Require valid-user
  </Proxy>
</VirtualHost>

あとは、簡易なgolangのサーバを立てて、サーバをポート8888で設置して、

go run app.go

を実行すればOKです。#app.goは任意です

1
2
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
1
2