このWebサイト ひつじ帳 は、管理者がWEB関連の技術をまとめた備忘録サイトです。 Linux,CentOS,PHP,MySQL,Blogger,JavaScript,CSS について記録しています。

CentOS-7/Apache/設定(httpd.conf,SSL,VirtualHost)

Apacheをインストールすれば、そのままでも動作しますが
ここでは、ユーザーのホームディレクトリ /home 配下で動作するように設定していきます。

その他、SSLやバーチャルドメインの設定についても追加設定していきます。

Apache 設定

Apache 設定ファイルを編集していきます。

ベースファイルを設定する

Apacheのベースとなるファイルを設定していきます。

設定ファイルをバックアップ

[root@vm ~]# cp -a /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
[root@vm ~]#

/etc/httpd/conf/httpd.conf 編集

#----- 設定ファイルを編集する
[root@vm ~]# vi /etc/httpd/conf/httpd.conf

#--------------------------------------------------
##★ サーバー名
#ServerName www.example.com:80
 ↓ デフォルトではコメントアウトされている為、コメントアウトを外して設定する
ServerName vm.lan:80


##★ ドキュメントルート
DocumentRoot "/var/www/html"
 ↓ 変更する
DocumentRoot "/home/admin/public_html"


##★ DirectoryIndex

    DirectoryIndex index.html
     ↓ 変更する
    DirectoryIndex index.cgi index.html



##★ LogFormat
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 ↓ 長すぎるURI(414エラー)はログに記録しない
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

##★ 外部公開しないのなら不要
## LogFormat の保存条件(LogFormatの下にでも追記する)
SetEnvIf Request_URI "^/_mem_bin/" nolog
SetEnvIf Request_URI "^/_vti_bin/" nolog
SetEnvIf Request_URI "^/c/" nolog
SetEnvIf Request_URI "^/d/" nolog
SetEnvIf Request_URI "^/msadc/" nolog
SetEnvIf Request_URI "^/MSADC/" nolog
SetEnvIf Request_URI "^/scripts/" nolog
SetEnvIf Request_URI "default\.ida" nolog
SetEnvIf Request_URI "cmd\.exe" nolog
SetEnvIf Request_URI "root\.exe" nolog
SetEnvIf Request_URI "Admin\.dll" nolog
SetEnvIf Request_URI "NULL\.IDA" nolog


##★ CustomLog
CustomLog "logs/access_log" combined
 ↓ 上記以外のアクセスをログに記録するように変更する
CustomLog "logs/access_log" combined env=!nolog


##★ AddHandler
#AddHandler cgi-script .cgi
 ↓ デフォルトではコメントアウトされている為、追記する
AddHandler cgi-script .cgi .pl


##★ AddDefaultCharset
AddDefaultCharset UTF-8
 ↓ コメントアウト化する
#AddDefaultCharset UTF-8


#----- 最下行あたりに追記する
##★ httpヘッダーにサーバ名のみを表示
ServerTokens ProductOnly

##★ Trace メソッドを無効にする
TraceEnable off
#--------------------------------------------------

#----- 編集内容を保存してエディタを閉じる
:wq
[root@vm ~]#

設定の文法をチェックする

[root@vm ~]# apachectl configtest
Syntax OK
[root@vm ~]#

httpdを再読み込みする

[root@vm ~]# systemctl reload httpd.service
[root@vm ~]#


ユーザーディレクトリを設定する

ユーザーディレクトリを設定します。

設定ファイルをバックアップ

[root@vm ~]# vi /etc/httpd/conf.d/userdir.conf

--------------------------------------------------
#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid.  This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    UserDir disabled
     ↓コメントアウトする
    #UserDir disabled

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    #
    #UserDir public_html
     ↓コメントアウトを外す
    UserDir public_html
</IfModule>

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
##★ <Directory> ~ </Directory> の範囲を変更
## ここから(変更前) ▼##
<Directory "/home/*/public_html">
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>
## ここまで(変更前) ▲##

  ## ↓ 変更する

## ここから(変更後) ▼##
<Directory "/home/*/public_html">
    AllowOverride All
    Options All -Indexes
    Require all granted
    AddHandler fcgid-script .php
</Directory>
## ここまで(変更後) ▲##
--------------------------------------------------

##■ 保存して編集終了
:wq
[root@vm ~]#

設定の文法をチェックする

[root@vm ~]# apachectl configtest
Syntax OK
[root@vm ~]#

httpdを再読み込みする

[root@vm ~]# systemctl reload httpd.service
[root@vm ~]#


SSLを設定する

サーバー用の自己署名証明書・秘密鍵を設定します。

設定ファイルをバックアップ

[root@vm ~]# cp -a /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org
[root@vm ~]#

/etc/httpd/conf.d/ssl.conf 編集

#----- 設定ファイルを編集する
[root@vm ~]# vi /etc/httpd/conf.d/ssl.conf

#--------------------------------------------------
##★ SNI の設定を追記する
SSLStrictSNIVHostCheck off


<VirtualHost _default_:443>

##★ サーバー名
#ServerName www.example.com:443
 ↓ 変更する
ServerName vm.lan:443


##★ サーバー証明書
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
 ↓ 変更する
SSLCertificateFile /etc/pki/tls/certs/server.crt


##★ 秘密鍵
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
 ↓ 変更する
SSLCertificateKeyFile /etc/pki/tls/private/server.key

</VirtualHost>
#--------------------------------------------------

##■ 保存して編集終了
:wq
[root@vm ~]#

設定の文法をチェックする

[root@vm ~]# apachectl configtest
Syntax OK
[root@vm ~]#

httpdを再読み込みする

[root@vm ~]# systemctl reload httpd.service
[root@vm ~]#


デフォルトページの設定

デフォルトで表示されるページを設定します。

設定ファイルをバックアップ

[root@vm ~]# cp -a /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org
[root@vm ~]#

/etc/httpd/conf.d/welcome.conf 編集

[root@vm ~]# vi /etc/httpd/conf.d/welcome.conf

#--------------------------------------------------
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

  ## ↓ コメントアウトにする

#<LocationMatch "^/+$">
#    Options -Indexes
#    ErrorDocument 403 /.noindex.html
#</LocationMatch>
#--------------------------------------------------
[root@vm ~]#

設定の文法をチェックする

[root@vm ~]# apachectl configtest
Syntax OK
[root@vm ~]#

httpdを再読み込みする

[root@vm ~]# systemctl reload httpd.service
[root@vm ~]#


バーチャルホストを設定する

バーチャルホストを設定します。
admin ユーザーに対して設定しています。
ご自身の環境に合わせて、admin のところを読み替えて設定してください。

/etc/httpd/conf.d/vhost.conf 新規作成

[root@vm ~]# vi /etc/httpd/conf.d/vhost.conf

#--------------------------------------------------
#----- http の設定
<VirtualHost _default_:80>
  ## vm.lan
  SuexecUserGroup admin admin
  ServerName vm.lan
  ServerAlias www.vm.lan
  DocumentRoot /home/admin/public_html
  #FcgidWrapper /home/admin/.fast-cgi-bin/php.cgi .php
</VirtualHost>

#----- https の設定
<VirtualHost _default_:443>
  ## https://vm.lan
  SuexecUserGroup admin admin
  ServerName vm.lan
  ServerAlias www.vm.lan
  DocumentRoot /home/admin/public_html
  #FcgidWrapper /home/admin/.fast-cgi-bin/php.cgi .php
  SSLEngine on
  SSLCertificateFile "/etc/pki/tls/certs/server.crt"
  SSLCertificateKeyFile "/etc/pki/tls/private/server.key"
</VirtualHost>
#--------------------------------------------------

#----- 編集内容を保存してエディタを閉じる
:wq
[root@vm ~]#

設定の文法をチェックする

[root@vm ~]# apachectl configtest
Syntax OK
[root@vm ~]#


ユーザー別のバーチャルホストの設定

ユーザー別のバーチャルホストを新規に作成しておきます。

/etc/httpd/conf.d/vhost_user.conf 新規作成

[root@vm ~]# touch /etc/httpd/conf.d/vhost_user.conf
[root@vm ~]#


FastCGI「mod_fcgid」の設定

FastCGIとして稼働させている「mod_fcgid」の設定です。

設定ファイルをバックアップ

[root@vm ~]# cp -a /etc/httpd/conf.d/fcgid.conf /etc/httpd/conf.d/fcgid.conf.org
[root@vm ~]#

fcgid.conf 編集

#----- fcgid.conf を編集する
# vi /etc/httpd/conf.d/fcgid.conf

#--------------------------------------------------
#★ 追記する (FCGIサーバへのリクエストがあってから、待機をしている時間を "3600秒" に設定)
FcgidIOTimeout 3600

#★ 追記する (アイドルプロセスのタイムアウト時間を "600秒" に設定)
FcgidIdleTimeout 600

#★ 追記する (FCGIアプリケーションのタイムアウトを "600秒" に設定)
FcgidBusyTimeout 600

#★ 追記する (アイドルプロセスの寿命を "3600秒" に設定)
FcgidProcessLifeTime 3600

#★ 追記する (FcgidBusyTimeoutを監視する間隔を "120秒" に設定)
FcgidBusyScanInterval 120

#★ 追記する (FastCGIアプリケーションプロセスの最大数を "100" に設定)
FcgidMaxProcesses 100

#★ 追記する (FastCGIアプリケーションクラスにおける最大プロセス数を "10" に設定)
FcgidMaxProcessesPerClass 10

#★ 追記する (各FastCGIアプリケーションが処理する最大リクエストを "0" に設定)
FcgidMaxRequestsPerProcess 0

#★ 追記する (HTTPのリクエストの長さの上限を "1GB" に設定)
FcgidMaxRequestLen 1073741824

#★ CGI 出力バッファサイズを "0" に設定
FcgidOutputBufferSize 0
#--------------------------------------------------


#----- 編集内容を保存してエディタを閉じる
:wq
[root@vm ~]#


Apacheの設定を再読込または再起動する

変更した設定を反映するには、Apacheの再起動が必要ですので、
httpd のサービスを再起動します。

Apache リロードまたは再起動

#----- httpdを再読み込みします
[root@vm ~]# systemctl reload httpd.service
[root@vm ~]#

#----- httpdの再起動でもOK
[root@vm ~]# systemctl restart httpd.service
[root@vm ~]#


以上で、Apacheの設定は完了です。

0 件のコメント:

人気記事

このブログを検索