LoginSignup
17
15

More than 5 years have passed since last update.

apacheバーチャルホスト設定メモ(複数ユーザー対応)

Posted at

複数のサイトをホストするwebサーバを構築する際のメモ
ユーザーの作成・ホームディレクトリの設定・バーチャルホストの設定などごちゃごちゃしがちなのでまとめておく。

※ 注意事項
SELINUXが動作している場合は、ホームディレクトリを読み込めないので一旦OFFにしておく
SELINUX有効にしつつ動作させる方法は後述

setenforce 0  # ← 一時的に無効

目標

1. 複数のユーザー(デザイナ等)がそれぞれのサイトを個別に編集できる
2. バーチャルホストで複数サイトが運営できる

ユーザーの作成

1.ここではuser1という名前のユーザーを作成

sudo useradd user1
sudo passwd user1

2.ホームディレクトリの設定

作成したユーザーで作業

su - user1  ← user1にチェンジ

public_htmlディレクトリを作成して、自分以外が編集できないよう(chmod)
apacheが読み込みるようホームディレクトリとコンテンツディレクトリに実行許可

mkdir /home/user1/public_html
chmod 701 /home/user1/public_html
chmod +x /home/user1

3.ユーザーディレクトリにテスト用のindex.htmlファイルを作成

cd public_html
vi /home/user1/public_html/index.html

作業完了

exit

apacheの設定

ホームディレクトリを使用できるようuser.confファイルを編集

vi /etc/httpd/conf.d/user.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 enabled user1  # ← 追記 
    #
    # 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    # ← コメントを外す
</IfModule>

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*/public_html">
    AllowOverride All      # ← 個別設定の許可
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>

動作確認

http://(サーバ名)/~user1 にアクセスして確認

バーチャルホストを設定

1.バーチャルホスト設定ファイルvhost.confを作成

sudo vi /etc/httpd/conf.d/vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
        DocumentRoot    /var/www/html/
        ServerAdmin     root@localhost
        ServerName      www.servername.com
        ErrorLog        /var/log/httpd/servername-error_log
        CustomLog       /var/log/httpd/servername-access_log common
</VirtualHost>
<VirtualHost *:80>
        DocumentRoot    /home/user1/public_html
        ServerAdmin     user1@servername.com
        ServerName      vhost1.servername.com
        ErrorLog        /var/log/httpd/vhost1-error_log
        CustomLog       /var/log/httpd/vhost1-access_log common
</VirtualHost>
  1. apacheを再起動
systemstl restart httpd.service
  1. 動作確認

作業PCのHOSTSファイルを書き換えて、確認!

SELINUXの再有効化

作成したユーザーディレクトリに対してrootユーザーで下記のコマンドを実行

restorecon -R /home/user1/public_html
setenforce 1
17
15
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
17
15