UserDir 模組可以讓伺服器的帳號, 擁有自己的網頁, 即在伺服器的主機名稱後面加上 “~username” 作為個人網頁。以下是在 RHEL 及 CentOS 開啟 UserDir 個人網頁模組, 以及設定 SELinux 的方法..
在 CentOS 只要用 yum 安裝了 Apache (httpd), 預設已經安裝了 UserDir 模組, 只要在 httpd.conf 或 /etc/httpd/conf.d/ 下面建立設定檔便可以啟用。可以用以下指令確認是否有 UserDir 模組:
-
# grep userdir /etc/httpd/conf.modules.d/00-base.conf
LoadModule userdir_module modules/mod_userdir.so
如果看到如上面的輸出, 即已經安裝了 UserDir.
第一步是建立 UserDir 的設定檔:
- # vi /etc/httpd/conf.d/userdir.conf
加入以下內容, 以下假設開啟的使用者名稱是 testuser:
<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 enabled testuser # # 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=""> ## Apache 2.4 users use following ## AllowOverride FileInfo AuthConfig Limit Indexes Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS ## Apache 2.2 users use following ## Options Indexes Includes FollowSymLinks AllowOverride All Allow from all Order deny,allow </directory>
儲存檔案後離開 vi.
如果需要開啟個帳號的個人網頁, 但其他帳號不開啟, 用以下語法:
UserDir enabled testuser1 testuser2 testuser3
如果所有帳號開啟個人網頁, 但個別幾個帳號不開啟, 用以下語法:
To allow most users to have UserDir directories, but deny this to a few, use the following:
UserDir disabled testuser4 testuser5 testuser6
設定後重新啟動 Apache:
- # systemctl restart httpd
建立使用者帳號的網頁目錄, 根據上面設定在 testuser 帳號家目錄下的 public_html, 並設定正確權限:
-
# mkdir /home/testuser/public_html
# chmod 711 /home/testuser
# chown testuser:testuser /home/testuser/public_html
# chmod 755 /home/testuser/public_html
建立測試頁面:
- # echo ‘userdir test’ >> /home/testuser/public_htmlindex.html
最後開啟 SELinux 的 httpd_enable_homedirs 及設定 httpd_sys_content_t:
-
# 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”.
The post RHEL / CentOS 7 開啟 UserDir 個人網頁模組 appeared first on Linux 技術手札.