Aliasは、指定のURLを実際のファイルパスにマップするディレクティブ。
mod_alias - Apache HTTP サーバ バージョン 2.4
これまでも、"/htmltest"なら"/var/www/htmltest"にマップしていたけど、本来は、"/"なら"/var/www/html"の下なので、"/htmltest"なら"/var/www/html/htmltest"となるはずだが、Aliasによって、違う場所"/var/www/htmltest"にマップできるわけである。
さて、これまでDocumentRootの下でこういうことをしてきたけど、全く違う場所にマップできるかというと、それも可能。
ただし、ちょっとハマったので注意。
最初は、
Alias /htmltest /var/www/htmltest/
<Directory "/var/www/htmltest/">
Order deny,allow
Deny from all
Allow from localhost
</Directory>
<Directory "/var/www/htmltest/sub_dir1">
Order allow,deny
Allow from all
Deny from localhost
</Directory>
Alias /www2 /var/www2/html
<Directory "/var/www2/html">
Order allow,deny
Allow from all
</Directory>/varの下にwww2というディレクトリを作り、これまでのAliasと同様にこのように定義した。
ところが、これだと403のForbeddenになってしまう。
親ディレクトリやindex.htmlのパーミッション、オーナー、グループなど確認したが、どうしても解決できない。
次に、
Alias /home /home/bg1/www
<Directory "/home/bg1/www">
Require all granted
</Directory>ホームディレクトリを公開する方法があったので、これも試したが同様に403のForbedden。
Alias で別 DocumentRoot を作り CGI, SSI, html を動かす
ただ、どうもApache2.4では、Allow、Denyを使う方法ではなく、Requireを使う方法じゃないと、ダメだというのはわかった。
Apache - apacheでDocumentRoot以外のアクセス方法について|teratail
結論として、/usr/localの下にディレクトリを作り、そこへのAliasとRequire all grantedを<IfModule alias_module>タグの中で定義すると、うまくいった。
[root@localhost conf]# cd /usr/local/www/ [root@localhost www]# ls index.html [root@localhost www]# ls -al 合計 12 drwxrwxr-x. 2 apache apache 4096 3月 17 16:41 . drwxr-xr-x. 13 root root 4096 3月 17 16:39 .. -rwxrwxr-x. 1 apache apache 96 3月 17 16:41 index.html [root@localhost www]# cd .. [root@localhost local]# ls bin etc games include lib lib64 libexec sbin share src www [root@localhost local]# ls -al 合計 52 drwxr-xr-x. 13 root root 4096 3月 17 16:39 . drwxr-xr-x. 12 root root 4096 10月 24 08:12 .. drwxr-xr-x. 2 root root 4096 7月 25 2019 bin drwxr-xr-x. 2 root root 4096 7月 25 2019 etc drwxr-xr-x. 2 root root 4096 7月 25 2019 games drwxr-xr-x. 2 root root 4096 7月 25 2019 include drwxr-xr-x. 2 root root 4096 7月 25 2019 lib drwxr-xr-x. 2 root root 4096 7月 25 2019 lib64 drwxr-xr-x. 2 root root 4096 7月 25 2019 libexec drwxr-xr-x. 2 root root 4096 7月 25 2019 sbin drwxr-xr-x. 5 root root 4096 10月 24 08:12 share drwxr-xr-x. 2 root root 4096 7月 25 2019 src drwxrwxr-x. 2 apache apache 4096 3月 17 16:41 www [root@localhost local]#
一応、apacheオーナーとグループにしたほうがいいらしいのでそうしてる。
パーミッションも775だけど、これもどこまで締めたり開けたりするかはわからない。
とにかく/usr/localにwwwフォルダを置いて、index.htmlは、
<html>
<head>
<title>UserLocal</title>
</head>
<body>
UserLocal
</body>
</html>UserLocal。
httpd.confは、
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.example.com/bar
#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.
Alias /local "/usr/local/www"
<Directory "/usr/local/www">
Require all granted
</Directory>
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>これまでIfModuleの中で定義してなかったけど、さらっとここに書けと書いてあったので。
外に書いた場合に上手くいくかはわかない。
これで、

/localで/usr/local/wwwの下にアクセス出来た。