RHEL / CentOS 7 開啟 UserDir 個人網頁模組


UserDir 模組可以讓伺服器的帳號, 擁有自己的網頁, 即在伺服器的主機名稱後面加上 “~username” 作為個人網頁。以下是在 RHEL 及 CentOS 開啟 UserDir 個人網頁模組, 以及設定 SELinux 的方法..

在 CentOS 只要用 yum 安裝了 Apache (httpd), 預設已經安裝了 UserDir 模組, 只要在 httpd.conf 或 /etc/httpd/conf.d/ 下面建立設定檔便可以啟用。可以用以下指令確認是否有 UserDir 模組:

/linux/httpd

  • # grep userdir /etc/httpd/conf.modules.d/00-base.conf
    LoadModule userdir_module modules/mod_userdir.so

如果看到如上面的輸出, 即已經安裝了 UserDir.

第一步是建立 UserDir 的設定檔:

/linux/httpd

  • # vi /etc/httpd/conf.d/userdir.conf

加入以下內容, 以下假設開啟的使用者名稱是 testuser:

儲存檔案後離開 vi.

如果需要開啟個帳號的個人網頁, 但其他帳號不開啟, 用以下語法:

UserDir disabled
UserDir enabled testuser1 testuser2 testuser3

如果所有帳號開啟個人網頁, 但個別幾個帳號不開啟, 用以下語法:
To allow most users to have UserDir directories, but deny this to a few, use the following:

UserDir enabled
UserDir disabled testuser4 testuser5 testuser6

設定後重新啟動 Apache:

/linux/httpd

  • # systemctl restart httpd

建立使用者帳號的網頁目錄, 根據上面設定在 testuser 帳號家目錄下的 public_html, 並設定正確權限:

/linux/httpd/userdir

  • # mkdir /home/testuser/public_html
    # chmod 711 /home/testuser
    # chown testuser:testuser /home/testuser/public_html
    # chmod 755 /home/testuser/public_html

建立測試頁面:

/linux/httpd/userdir

  • # echo ‘userdir test’ >> /home/testuser/public_htmlindex.html

最後開啟 SELinux 的 httpd_enable_homedirs 及設定 httpd_sys_content_t:

/linux/selinux

  • # setsebool -P httpd_enable_homedirs true
    # chcon -R -t httpd_sys_content_t /home/testuser/public_html

現在可以在伺服器的網址後面加上 “~testuser” 開啟使用者帳號的網頁目錄, 例如:

http://localhost/~testuser/

這時如果設定正確, 便可以看到上面建立的測試頁面 index.html, 輸出 “userdir test”.

Leave a Reply