Apache に静的ファイルをアップロードする - AlmaLinux

Apache をウェブサーバーとして利用する

Apache を利用してウェブページを表示するには、HTML ファイルをサーバーにアップロードする必要があります。

ファイル保存場所は「 /var/www/html 」です。

しかし、「 /var/www/html 」のパーミッションの設定が環境によって異なります。

そのままではパーミッションエラーになり、ファイルを設置することができません。まず、「 所有者の変更 」と「 パーミッションの変更 」が必要です。

一般ユーザーでもファイルの書き換えができるように「 /var/www/html 」の所有者を「 apache 」に変更します。

chown コマンドに -R オプションをつけることで配下のファイルやディレクトリも一括で変更できます。

sudo chown -R apache:apache /var/www/html/

「 /var/www/html 」のバーミッションを所有者の「 apache ユーザー 」と「 apache グループ 」を「 775 」に変更します。

sudo chmod 775 -R /var/www/html/

一般ユーザー(user)を書き込み権限のある apache グループへ追加します。

sudo usermod -aG apache user

オプションの「 -a 」はユーザーをグループに追加し、「 -G 」は所属グループを変更します。「 -a 」と「 -G 」はセットで使います。

グループの設定を反映させるには、シェルを再読み込みさせる必要があります。一度、ログアウトして、ログインします。

これで「 /var/www/html 」に HTML ファイルを配置することができるようになります。

ユーザーごとの公開ディレクトリの設定

Apache の設定を変更します。

sudo vi /etc/httpd/conf.d/userdir.conf

<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

    #
    # 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>

<Directory "/home/*/public_html">
    #AllowOverride FileInfo AuthConfig Limit Indexes
    #Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

    AllowOverride None
    Options SymLinksIfOwnerMatch
    Require method GET POST OPTIONS
</Directory>

「 UserDir disabled 」をコメントにして、「 UserDir public_html 」のコメントを外します。

「 AllowOverride FileInfo AuthConfig Limit Indexes 」、「 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec 」をコメントアウトします。

ユーザーディレクトリに「 public_html 」というディレクトリを作成します。このディレクトリに公開する HTML ファイルをアップロードします。

作成した 「 index.html 」を「 public_html 」ディレクトリにアップロードしてください。

次に各ディレクトリのパーミッションを変更します。

sudo chmod 755 -R public_html
sudo chmod 755 ./

パーミッションを「 755 」に変更した場合、書き込み権限がないためアップロードすることができなくなります。その場合は、「 775 」などに変更します。

SELinux を有効にしている場合、SELinux の設定も変更しておきます。

restorecon -R public_html

ブラウザで「 サーバーのIPアドレスもしくはドメイン名/~ユーザー名 」と入力しページが表示されれば成功です。

Document ルートの変更

Document ルートを変更するには、Apache の設定を変更します。

ここでは、ユーザーディレクトリの「 public_html 」というディレクトリに変更してみます。

ユーザーディレクトリ名は「 user 」です。

/etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

まず、「 DocumentRoot "/var/www/html" 」を「 DocumentRoot "home/user/public_html 」に変更します。

# Relax access to content within /var/www. のところの <Directory "/var/www"> を <Directory "/home/user/public_html"> に変更します。

# Further relax access to the default document root: のところも <Directory "/var/www/html"> を <Directory "/home/user/public_html"> に変更します。

ディレクトリのパーミッションを変更していない場合、以下のように変更します。

sudo chmod 755 -R /home/user/public_html
sudo chmod 755 ./

パーミッションを「 755 」に変更した場合、書き込み権限がないためアップロードすることができなくなります。その場合は、「 775 」などに変更します。

ブラウザで「 サーバーのアドレス 」を入力して画面が表示されれば成功です。