Apache設定 チートシート
Apacheディレクティブのクイックリファレンス。基本設定、バーチャルホスト、リライトルール、認証、SSL、モジュール、.htaccess、パフォーマンスチューニングを網羅
58 件のコマンド
ServerNameサーバーのホスト名を設定
ServerName www.example.comServerAliasサーバーの別名を設定
ServerAlias example.com *.example.comDocumentRootドキュメントルートを設定
DocumentRoot /var/www/htmlListenリッスンするポートを設定
Listen 80ServerRootサーバーのルートディレクトリを設定
ServerRoot /etc/apache2DirectoryIndexデフォルトのインデックスファイルを設定
DirectoryIndex index.html index.phpErrorDocumentカスタムエラーページを設定
ErrorDocument 404 /error/404.htmlServerAdmin管理者のメールアドレスを設定
ServerAdmin admin@example.comapachectl configtest設定ファイルの構文チェック
apachectl configtestapachectl gracefulApacheをグレースフルリスタート
apachectl graceful<VirtualHost>バーチャルホストブロックを定義
<VirtualHost *:80> ... </VirtualHost><VirtualHost *:443>SSL用バーチャルホストを定義
<VirtualHost *:443> SSLEngine on ... </VirtualHost>NameVirtualHost名前ベースのバーチャルホストを有効化
NameVirtualHost *:80<Directory>ディレクトリ別の設定を定義
<Directory /var/www/html> Options Indexes FollowSymLinks </Directory>Optionsディレクトリのオプションを設定
Options -Indexes +FollowSymLinksAllowOverride.htaccessによる上書きを制御
AllowOverride AllRequireアクセス制御を設定
Require all grantedRewriteEngineURL書き換えエンジンを有効化
RewriteEngine OnRewriteRuleURL書き換えルールを定義
RewriteRule ^old/(.*)$ /new/$1 [R=301,L]RewriteCond書き換えの条件を指定
RewriteCond %{HTTPS} offRewriteCond %{HTTP_HOST}ホスト名による条件分岐
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]RewriteCond %{REQUEST_URI}リクエストURIによる条件分岐
RewriteCond %{REQUEST_URI} !\.(jpg|png|gif)$ [NC]RedirectURLリダイレクトを設定
Redirect 301 /old-page https://example.com/new-pageRedirectMatch正規表現でリダイレクトを設定
RedirectMatch 301 ^/blog/(.*)$ https://blog.example.com/$1AuthType認証タイプを設定
AuthType BasicAuthName認証領域名を設定
AuthName "Restricted Area"AuthUserFileパスワードファイルのパスを指定
AuthUserFile /etc/apache2/.htpasswdRequire valid-user認証済みユーザーのみアクセス許可
Require valid-userRequire user特定ユーザーのみアクセス許可
Require user admin editorhtpasswdパスワードファイルを作成・更新
htpasswd -c /etc/apache2/.htpasswd adminRequire ip特定IPからのみアクセス許可
Require ip 192.168.1.0/24SSLEngineSSLエンジンを有効化
SSLEngine onSSLCertificateFileSSL証明書ファイルを指定
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pemSSLCertificateKeyFileSSL秘密鍵ファイルを指定
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pemSSLProtocol許可するSSLプロトコルを設定
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1SSLCipherSuite暗号スイートを設定
SSLCipherSuite HIGH:!aNULL:!MD5SSLHonorCipherOrderサーバーの暗号スイート順序を優先
SSLHonorCipherOrder onHeader set Strict-Transport-SecurityHSTSヘッダーを設定
Header always set Strict-Transport-Security "max-age=31536000"LoadModuleモジュールをロード
LoadModule rewrite_module modules/mod_rewrite.soa2enmodモジュールを有効化(Debian系)
a2enmod rewritea2dismodモジュールを無効化(Debian系)
a2dismod statusa2ensiteサイト設定を有効化(Debian系)
a2ensite example.com.confa2dissiteサイト設定を無効化(Debian系)
a2dissite 000-default.conf<IfModule>モジュールの存在を条件に設定
<IfModule mod_rewrite.c> RewriteEngine On </IfModule>.htaccess RewriteEngine.htaccessでURL書き換えを有効化
RewriteEngine On.htaccess HTTPS強制HTTPSへのリダイレクトを強制
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301].htaccess www強制wwwありにリダイレクト
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L].htaccess キャッシュブラウザキャッシュを設定
ExpiresByType image/jpeg "access plus 1 year".htaccess IP制限IPアドレスでアクセスを制限
Require ip 192.168.1.0/24.htaccess ディレクトリ一覧禁止ディレクトリ一覧表示を禁止
Options -Indexes.htaccess カスタムエラーページカスタムエラーページを設定
ErrorDocument 404 /errors/404.htmlmod_deflategzip圧縮を有効化
AddOutputFilterByType DEFLATE text/html text/css application/javascriptmod_expiresブラウザキャッシュの有効期限を設定
ExpiresActive On
ExpiresDefault "access plus 1 month"KeepAliveKeep-Alive接続を設定
KeepAlive OnKeepAliveTimeoutKeep-Aliveタイムアウトを設定
KeepAliveTimeout 5MaxKeepAliveRequestsKeep-Alive最大リクエスト数
MaxKeepAliveRequests 100MaxRequestWorkers最大同時リクエスト数を設定
MaxRequestWorkers 150Timeoutリクエストタイムアウトを設定
Timeout 60