CentOS7にApacheのインストールを行う手順です。
CentOS6.5とは違い、yumで入れるとApache2.4が入ります。
ほとんどCentOS6.5と同じですが多少違う部分もあるかもしれません。
まずはyumでapacheのインストール
1 |
yum install httpd |
相変わらずyum先輩は便利です。
続いて、httpd.confを編集します
1 |
vi /etc/httpd/conf/httpd.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 一応サーバのバージョン情報等は隠す ServerTokens Prod ServerSignature Off # KeepAliveを有効に KeepAlive On # KeepAliveで接続を保持する件数を30、タイムアウトまでの時間は10秒に設定 MaxKeepAliveRequests 30 KeepAliveTimeout 10 # 逆引きは欲しい時に手動でやればいいので無効化 HostnameLookups Off # どうせPHPはインストールするのでDirectoryIndexにindex.phpを追記しておく DirectoryIndex index.html index.htm index.cgi index.php # なんやかんやで通信の圧縮とか設定していくとAddTypeしとかないと・・・ってなるので代表的なmimetypeは予め追加しちゃいます AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-gzip .gz .gzip AddType application/x-javascript .js AddType application/java .class AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx AddType application/x-shockwave-flash .swf AddType application/x-tar .tar AddType application/x-httpd-php .php AddType application/pdf .pdf AddType application/msword .doc .docx AddType application/x-msdownload .exe AddType text/css .css AddType text/html .html .htm AddType text/richtext .rtf .rtx AddType text/plain .txt AddType text/xml .xml AddType text/html .shtml AddType video/asf .asf .asx .wax .wmv .wmx AddType video/avi .avi AddType video/divx .divx AddType video/quicktime .mov .qt AddType video/mp4 .mp4 .m4v AddType video/mpeg .mpeg .mpg .mpe AddType image/bmp .bmp AddType image/svg+xml .svg .svgz AddType image/gif .gif AddType image/x-icon .ico AddType image/jpeg .jpg .jpeg .jpe AddType image/png .png AddType image/tiff .tif .tiff AddType audio/midi .mid .midi AddType audio/mpeg .mp3 .m4a AddType audio/x-realaudio .ra .ram AddType audio/wav .wav AddType audio/wma .wma |
こんな感じでhttpd.confはApacheWEBサーバ共通の設定だけ書いて、ホストの情報は別ファイルで管理することにします。
(バーチャルホストで運用します)
この中で注意していただきたいのは
MaxKeepAliveRequests
KeepAliveTimeout
の2つです。
MaxKeepAliveRequestsはKeepAlive(クライアントとのコネクションを保持)している間に転送するファイル数です。サイトの1ページを見るのに必要なファイル数より若干多いくらいを指定するべきなので、公開するサイトによって最適な値は変わってきます。
KeepAliveTimeoutはMaxKeepAliveRequestsに達していなくてもその秒数が経過したらコネクションを切るというものです。
この2つは最適な値を入れるようにしたほうが良いと思います。
以下がNameVirtualHostの設定
1 |
vi /etc/httpd/conf.d/virtual.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# jdbc.tokyoというホスト名はすべてblog.ryzen.tokyoにRewriteすることにしちゃいます。 # こうすることで、URLをblog.ryzen.tokyoに統一できてよかとです。 <VirtualHost *:80> ServerName jdbc.tokyo RewriteEngine On RewriteCond %{HTTP_HOST} jdbc.tokyo RewriteRule ^/(.*)$ http://blog.ryzen.tokyo/$1 [R=301,L] </VirtualHost> <VirtualHost *:80> ServerName blog.ryzen.tokyo ServerAdmin hoge@jdbc.tokyo DocumentRoot /var/www/html ErrorLog /var/log/httpd/www-error_log CustomLog /var/log/httpd/www-access_log combined env=!no_log <Directory "/var/www/html/blog.ryzen.tokyo"> Options Includes ExecCGI FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> |
DocumentRootは、ドキュメントルートまでの正しいパスを指定してください。
あと見慣れないのはRequire all grantedでしょうか。
これは以前のApacheでいう
Order allow,deny
Allow from all
と同じで、全部通すぜ!ワイルドだろ?ってやつです。
あとNameVirtualHostの宣言も不要になってます。
他にも細かいログのカスタマイズ等ゴニョゴニョ設定してはいますが、基本的なところではコレくらい設定できていればとりあえず動きます!
あと、Apacheのログは肥大化しやすいので、ログのローテーションも忘れないように行っておけば後から見やすく便利です。
たとえば、こんな感じで。。
1 |
vi /etc/logrotate.d/httpd |
1 2 3 4 5 6 7 8 9 10 11 |
/var/log/httpd/*log { missingok daily rotate 14 notifempty sharedscripts delaycompress postrotate /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true endscript } |
あとは自動起動の設定とfirewalldの設定、デーモンの起動を行えば、OKです!
1 2 3 4 5 |
systemctl enable httpd.service systemctl start httpd.service firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --reload |
あとはブラウザから表示されるかテストしてみてください。