Linux 技术手札

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”.

Exit mobile version