LoginSignup
24
34

【Apache】DocumentRootを変更しよう

Last updated at Posted at 2020-06-08

はじめに

WebサーバーのApacheを導入当初は、DocumentRootに所定のパス\var\www\htmlが指定されていますが、
DocumentRootに任意のパスを指定することもできるようです。
その方法を調べてみました。

前提

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

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

使用ツール

  • Tera Term

手順

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

やってみよう

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

設定ファイル/etc/httpd/conf/httpd.confの該当部分を下記のように書き換えます。

  • DocumentRoot書き換え
httpd.conf(変更前)
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"
httpd.conf(変更後)
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/hoge/hoge"
  • Dirctory書き換え
    Directoryのパスを、編集したドキュメントルートに合わせて書き換えます。
httpd.conf(変更前)
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
httpd.conf(変更後)
#
# Relax access to content within /var/www.
#
<Directory "/hoge/hoge">

DocumentRoortと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に保存した文字列がブラウザで確認できたら完了です。

参考サイト

【環境構築】CentOS7.7 + Apache2.4.6でドキュメントルート変更手順

関連ページ

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まで購入してしました。。。
みなさんでもぜひ!

24
34
0

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
24
34