page counter next up previous contents
Next: Virtual Host 虛擬主機 Up: Apache 2.4 HTTP Server Previous: 防火牆設定   Contents

UserDir 用戶個人網頁

  1. UserDir 模組:可以讓伺服器中的用戶,擁有自己的網頁。例如:http://kvm5.deyu.wang/~deyu1,查看模組有沒有載入,沒有的話要自行載入。
    [root@kvm5 ~]# grep userdir /etc/httpd/conf.modules.d/00-base.conf
    LoadModule userdir_module modules/mod_userdir.so
    
  2. 開啟 Userdir,並設定用戶個人網頁的根目錄。
    [root@kvm5 ~]# vim /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  <= 取消註解這行,個人網頁根目錄為 public_html
    
    </IfModule>
    
  3. 查看用戶個人網頁根目錄的存取設定是否符合要求,不符合的話就要修改。
    [root@kvm5 ~]# tail /etc/httpd/conf.d/userdir.conf 
    #
    # 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 FileInfo AuthConfig Limit Indexes
        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        Require method GET POST OPTIONS
    </Directory>
    
  4. 重新啟動 httpd。
    [root@kvm5 ~]# systemctl enable httpd.service 
    ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
    [root@kvm5 ~]# systemctl reload httpd.service
    
  5. 增加一般用戶 deyu1,並設定其密碼。
    [root@kvm5 ~]# useradd deyu1
    [root@kvm5 ~]# echo '123qwe' | passwd --stdin deyu1
    Changing password for user deyu1.
    passwd: all authentication tokens updated successfully.
    
  6. 改變身份為 deyu1,滅號為使用 deyu1 用戶的環境變數,不要漏了。
    [root@kvm5 ~]# su - deyu1
    
  7. 建立個人網頁根目錄,並產生一個內容為 userdir test 的 index.html 檔案。
    [deyu1@kvm5 ~]$ mkdir public_html
    [deyu1@kvm5 ~]$ echo 'userdir test' > public_html/index.html
    
  8. 退出用戶 deyu1。
    [deyu1@kvm5 ~]$ exit
    logout
    
  9. 連線 deyu1 的個人網頁,發現無權限存取。
    [root@kvm5 ~]# curl http://kvm5.deyu.wang/~deyu1/
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don't have permission to access /~deyu1
    on this server.</p>
    </body></html>
    
  10. 查看 deyu1 的家目錄權限為 700,並不開放其他人讀取。
    [root@kvm5 ~]# ll /home/deyu1/ -d
    drwx------. 3 deyu1 deyu1 1024 May 22 21:04 /home/deyu1/
    
  11. 將 deyu1 的家目錄權限改為 755,開放其他人讀取。
    [root@kvm5 ~]# chmod 755 /home/deyu1/
    
  12. 連線成功。
    [root@kvm5 ~]# curl http://kvm5.deyu.wang/~deyu1/
    userdir test
    



2015-12-04