LoginSignup
6

【Apache】Aliasを使ってみよう

Last updated at Posted at 2020-06-08

はじめに

過去記事【Apache】DocumentRootを変更しようではDocumentRootを変更してみましたが、
Alias(エイリアス)を使用することで、
DocumentRootを変更することなく、まったく別のディレクトリにあるファイルを
DocumentRootの配下に配置されているように見せかけることができるようです。

前提

下記記事で構築した環境を前提とします。

  • Windows10にVagrantをを入れてCentOS7をインストールしよう(123456)
  • ローカルでLAMP環境を構築しよう(014

使用ツール

  • Tera Term

手順

1. 設定ファイル書き換え
2. 動作確認用のファイルを作成
3. DocumentRootに指定したフォルダにパーミッション付与

やってみよう

1. 設定ファイル書き換え

設定ファイル/etc/httpd/conf/httpd.conf内を"Alias"で検索すると次のようなコメントが見つかります。

httpd.conf(変更前)
    #
    # 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.

そのコメントの下に、下記を追記します。

httpd.conf(変更後)
    #
    # 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 /hoge "/hoge/hoge"
    <Directory "/hoge/hoge">
        Require all granted
    </Directory>

追記をしたらApacheを再起動します。

systemctl restart httpd

2. 動作確認用のファイルを作成

ルートに下記のような要領で、ファイルを作成してください。

/hoge/hoge/index.html
.
├── hoge
│   └── hoge
│       └── index.html

index.htmlに適当な文字列を入力して保存してください。

vi /hoge/hoge/index.html

##3. DocumentRootに指定したフォルダにパーミッション付与
下記のコマンドで、作成したフォルダ・ファイルの所有者をapacheにします。

chown apache:apache /hoge/hoge/index.html

下記のコマンドで権限を付与します。

chmod 755 /hoge/hoge/index.html

Vagrantfileのconfig.vm.network “private_networkに設定したIPアドレスを確認します。
(ここでは"192.168.33.10"としましょう)
ブラウザに以下のアドレスを入力します。

http://192.168.33.10/hoge/index.html
('index.html'は省略可)

index.htmlに保存した文字列がブラウザで確認できたら完了です。

ちなみに

Aliasは複数設定可能です。

httpd.conf
    #
    # 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 /hoge "/hoge/hoge"
    <Directory "/hoge/hoge">
        Require all granted
    </Directory>
    Alias /foo "/foo/foo"
    <Directory "/foo/foo">
        Require all granted
    </Directory>

下記のように、フォルダ・ファイルを作成して、

/foo/foo/index.html

以下のURLにアクセスしてみてください。

http://192.168.33.10/hoge/index.html
http://192.168.33.10/foo/index.html

参考サイト

エイリアス(Alias)
Apacheでドキュメントルート以外のディレクトリを公開する方法
Apacheで403 Forbiddenが表示された時のチェックポイント5選

関連ページ

Windows10にVagrantをを入れてCentOS7をインストールしよう

1. VagrantインストールからVagrantfileを設置まで
2. 仮想マシンの操作
3. WinSCP、Tera Termに秘密鍵でログイン
4. WinSCP、Tera Termにrootユーザーでパスワードログイン
5. zip/unzipをインストール
6. Vagrantにて仮想環境を配布

ローカルでLAMP環境を構築しよう

0. 事前準備
1. Apacheをインストール
4. ファイアウォールとか停止する

Apache

DocumentRootを変更しよう
Aliasを使ってみよう
VirtualHostを使ってみよう

おすすめ参考書

サーバ構築の実際がわかる Apache[実践]運用/管理 (Software Design plus)

閑話休題

最近のマイブームはスプラトゥーンです。
ハマりすぎて専用のSwitchまで購入してしました。。。
みなさんでもぜひ!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6