[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

Apacheを使ってbasic認証かけてみた

こんにちは、けんです。今回初めてのブログを書いていきますので、とても緊張しています。多分、、、、、。コーヒー大好きで、会社でもコーヒーをほぼ毎日いれてます、そして他のビヨンドメンバーに飲んでね!と毎日押しつけて、勧めています。

 

今回使用したもの

virtualbox バージョン 6.1

vagrant バージョン 2.2.19

vagrant box: centos/7

apache バージョン 2.4.6

 

basic認証とは?

まずは、wikipediaさんの説明を見ていきましょう。

Basic認証(ベーシックにんしょう、Basic Authentication)とは、HTTPで定義される認証方式(HTTP認証)の一つ。基本認証と呼ばれることも。

Basic認証では、ユーザ名とパスワードの組みをコロン ":" でつなぎ、Base64でエンコードして送信する。このため、盗聴や改竄が簡単であるという欠点を持つが、ほぼ全てのWebサーバおよびブラウザで対応しているため、広く使われている。

盗聴や改竄を防ぐため、後にDigest認証というユーザ名とパスワードをMD5でハッシュ化して送る方法が考えられた。

引用:https://ja.wikipedia.org/wiki/Basic%E8%AA%8D%E8%A8%BC (2022/08/12現在)

へっ???何を言っているの??wikipediaよ!!

一言でいうと、webサイトにアクセス制限をかけること、で、それを簡単に数行だけで行おうということです。サイトにログインする前に、ユーザー名とパスワード打ってね、それじゃないとログインできませんよっていうやつです。

 

ApacheでVirtualhostを立てて、basic認証をかけてみよう!

今回はvagrantにインストールしたVirtualHostで設定した公開画面にbasic認証をかけることをゴールとします。

Vagrantfileの設定を変更して、vagrantにログインしてみよう

local環境内でインターネット接続できるようにするために、vagrantfileを編集をする。

Vagrantfileの一例が以下の設定になっている。

接続するために絶対に必要なのが、

 config.vm.network "private_network", ip: "192.168.43.20"

となっている。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
   config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.43.20"

end

編集が終了したら、vagrant upをする。

vagrant up

vagrant upが完了したら、vagrant sshで接続して、

 vagrant ssh 

接続は完了となる。

apacheのインストールと表示の確認

まずは、apacheをインストールしていくことから始めます。

sudo yum install httpd

completeと表示されるとインストールが完了となる。

systemctl status httpd

この時点では、まだ起動していないので、inactive(dead)という表示になる。

● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)

apacheを起動させていく。

sudo systemctl start httpd

起動したことを確認するために、もう一度apacheのステータスを確認する。

systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2022-08-12 05:55:17 UTC; 4s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2443 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
tq2443 /usr/sbin/httpd -DFOREGROUND
tq2444 /usr/sbin/httpd -DFOREGROUND
tq2445 /usr/sbin/httpd -DFOREGROUND
tq2446 /usr/sbin/httpd -DFOREGROUND
tq2447 /usr/sbin/httpd -DFOREGROUND
mq2448 /usr/sbin/httpd -DFOREGROUND

active(running)という表示になっていることが確認できたら、apacheは動いている。

更に、apacheのtestページが表示されることも併せて確認していく。

ブラウザでhttp://localhost:8080と入力して、apacheのtest画面が表示されればオッケー。

Virtualhostを立てていこう

まず初めに、Virtualhost用にドキュメントルートを作成していく

mkdir -p /var/www/vhosts/example.com/public_html

を作成したら、public_htmlに移動する。

cd /var/www/vhosts/example.com/public_html

public_html以下に表示させるようのファイルを作成する

vi index.html

中身は任意のものを記載していく。今回はVirtualhost用のテストなので、以下のような文言にする。

This is a test for the basic document

書き込むことができたら、:wqで保存してページを閉じる。

次に今回作成するようのVirualhostのconfファイルを作成する。

confファイルを読み込むためには、/etc/httpd/conf.d以下におく必要があるので、conf以下のファイルに移動する。

cd /etc/httpd/conf.d/

confのディレクトリに移動することができたら、Virtualhost用のconfを作成する。

vi vhost.conf

vhost.confの中身にServerNameとDocumentRootを記述していく。

<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/vhosts/example.com/public_html
</VirtualHost>

設定を反映させるために、apacheを再起動する。

systemctl restart httpd

これだけでは反映されないので、windowsのローカルでの設定が必要となる。行っていくことは、hostsファイルを編集すること。hostファイルの位置は以下のディレクトリになっている。

C:\Windows\System32\drivers\etc\hosts

これをexplorerに入力して、hostファイルをメモ帳を管理者権限で開いて、以下の記述を付け加えていく。

192.168.43.20 example.com

これを書き加えて、保存する。

その後にもし設定がうまくできていれば、ブラウザでhttp://example.comと入力したときにindex.htmlで記載したサイトが表示される。

 

このように表示がされたら、VirtualHostの設定に成功したことになる。

次にこの設定したVirtualHostにbasic認証の設定をしていく。

basic認証の設定を行っていこう

今回basic認証をかけていくサイトは、

/var/www/vhosts/example.com/public_html

になっているので、上記のパスにあるサイトのみにbasic認証がかかるように設定していく。

sudo htpasswd -c /var/www/vhosts/example.com/.htpasswd ユーザ名

ユーザー名とパスワードは任意なものを設定する。上記のコマンドを打つとEnter passwordと聞かれるので、パスワードを設定していく。パスワードは確認のために2回聞かれるので、2回自分が設定したいパスワードを打っていく。それが完了したら、ユーザーとパスワードが設定されているかを確認するために、catコマンドを打つ。

cat /var/www/vhosts/example.com/.htpasswd

出力結果で、ユーザー名とハッシュ化されたパスワードが表示されたので、設定はちゃんとされていることを確認できた。

vagrant:$apr1$5XrvFjtv$45oq4gzLiu708WtVYuOtq0

権限周りの設定が変更できたので、basic認証がされるように設定を行っていく。今回は、.htaccessを使って、basic認証を行っていく。

.htaccessのファイルをドキュメントルート配下に作成する。

sudo vi /var/www/vhosts/example.com/public_html/.htaccess 

そして、basic認証がかかる設定を.htaccess内に入れていく。

  AuthType Basic
  AuthName basic auth test
  AuthUserFile /var/www/vhosts/example.com/.htpasswd
  require valid-user

書き込みが終わったら、:wqを押して、保存する。

そして、apacheはデフォルトで.htaccessの設定を許可するようにされていないので、許可するように設定をしていく。

具体的には/var/www/vhosts/example.com/public_html用にbasic認証の設定ができるようにvhost.confの中に設定を追記していく。追記していく内容は、以下の記述になっている。

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/vhosts/example.com/public_html
  <Directory /var/www/vhosts/example.com/public_html>
    AllowOverride AuthConfig
  </Directory>
</VirtualHost>

 

まずは、どのDirectoryに設定を反映させるかを決めるために、<Directory /var/www/vhosts/example.com/public_html></Directory>を記述していく。その中に、AllowOverrideの設定をAuthConfigにすることによって、basic認証などの設定を反映させることができる。この変更が記述できたら、今までの設定を反映させるために再起動する。

sudo systemctl restart httpd

これで設定が反映されたはずなので、もう一度http://example.comと検索して、表示を確認する。

basic認証の画像が表示がされた!!!念のために設定したユーザー名とパスワードを入力してみるとindex.htmlの画像がちゃんと表示された!!!

 

まとめ

basic認証の設定をする時に、設定したのに表示されないじゃないかとか焦るかもしれませんが、焦らずにググって調べることの大切さを学びました。これまではapacheさんメインに色々勉強してましたが、次からはnginxさんのbasic認証の仕方をブログに書いていきたいと思います。

次のブログのテーマを予告した所で、読んでくださってありがとうございます。よい夏をお過ごしください。

 

 

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
9
読み込み中...
9 票, 平均: 1.00 / 19
3,858
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Ken

Joined Beyond as a new graduate in 2022.Currently works
in the System Solutions Department.I
studied linguistics until graduate school and took on the challenge of becoming an infrastructure engineer.I
established an in-house club called the Beyond Cafe Club, and am passionate about making coffee and tea every day. ing