Apache - user000422/0 GitHub Wiki

ノウハウ

# httpd.conf

# セキュリティ的にこれは確定でよい
<Directory />
    AllowOverride none # すべて拒否
</Directory>

導入

RHEL6 RHEL8 RHEL9
yum install httpd dnf install httpd

操作

バーチャルホスト一覧を確認

/usr/sbin/httpd -t -D DUMP_VHOSTS

# バージョン確認
httpd -v

# 色んな情報(VirtualHostの確認に使用した)
httpd -S

# 再起動
service httpd restart
# RHEL

# 状態確認
systemctl status httpd
service httpd status

# 設定反映
systemctl reload httpd

# 起動
systemctl start httpd
service httpd start

# 停止
systemctl stop httpd

httpd.conf

# エラーログに記録されるレベルを設定
LogLevel notice

<Directory />
  Require all denied  # 全て拒否
  Require all granted # 全て許可

  Deny from all  # 全て拒否
  Allow from all # 全て許可

  AllowOverride All  # .htaccessを全て有効
  AllowOverride None # .htaccessを全て無効
</Directory>


.htaccess

HTTP(HTTPS)接続を制御する(SSHなどは制御しない)
修正後サーバの再起動などは必要ない

# 接続許可(指定IP)
Require ip 000.000.000.000

URLリダイレクト
confファイルで「mod_rewrite.so」を読み込ませる必要がある

RewriteEngine on
RewriteRule from.html to.html
# 特定のIP以外は404にリダイレクト
RewriteEngine on

# このIPのみ404にはリダイレクトしない
RewriteCond %{REMOTE_ADDR} !^111.1111.111.1111$
RewriteRule ^.*$ - [NC,R=404,L]

logrotate.d

ログローテーション設定 /etc/logrotate.d


(応用)自動起動

# RHEL7
systemctl enable httpd.service

(応用)ベーシック認証

ドキュメントルートに .htaccess.htpasswd を作成する
.htaccess に下記を追記する。

AuthUserFile /var/www/html/.htpasswd # .htpasswdを作成したパス
AuthName “Please enter your ID and password”
AuthType Basic
require valid-user 

.htpasswd に下記を追記する。
パスワードは http://www.htaccesseditor.com/#a_basic で生成したものを記述。

# ユーザー:パスワード
test:pass

(応用)Digest認証

.htaccess に下記を追記する。(要検証 検証したのはxxx.conf)

AuthType Digest
AuthUserFile /etc/httpd/.htdigest # .htdigestを作成する予定のパス
AuthName “sample”
AuthDigestProvider file
require sample-user

記載後、下記コマンド(htdigest -c [パス] [AuthName] [requireユーザ])
htdigest -c /etc/httpd/.digest "sample" sample-user

⚠️ **GitHub.com Fallback** ⚠️