ค้นหาเว็บไซต์

วิธีเปิดใช้งานโมดูล Apache Userdir บน RHEL/CentOS


User Directory หรือ Userdir คือโมดูล Apache ซึ่งอนุญาตให้เรียกข้อมูลไดเรกทอรีเฉพาะของผู้ใช้ผ่านทางเว็บเซิร์ฟเวอร์ Apache โดยใช้ http://example.com/ ~user/ ไวยากรณ์

ตัวอย่างเช่น เมื่อเปิดใช้งานโมดูล mod_userdir บัญชีผู้ใช้ในระบบจะสามารถเข้าถึงเนื้อหาในโฮมไดเร็กตอรี่ของตนกับโลกผ่านทางเว็บเซิร์ฟเวอร์ Apache

ในบทความนี้ เราจะแสดงวิธีเปิดใช้งาน Apache userdirs (mod_userdir) บน RHEL, CentOS และ เซิร์ฟเวอร์ Fedora ที่ใช้เว็บเซิร์ฟเวอร์ Apache

บทช่วยสอนนี้สันนิษฐานว่าคุณได้ติดตั้งเว็บเซิร์ฟเวอร์ Apache บนการกระจาย Linux ของคุณแล้ว หากคุณยังไม่มี คุณสามารถติดตั้งได้โดยใช้ขั้นตอนต่อไปนี้...

ขั้นตอนที่ 1: ติดตั้งเซิร์ฟเวอร์ Apache HTTP

หากต้องการติดตั้งเว็บเซิร์ฟเวอร์ Apache ให้ใช้คำสั่งต่อไปนี้บนการกระจาย Linux ของคุณ

yum install httpd           [On CentOS/RHEL]
dnf install httpd           [On Fedora]

ขั้นตอนที่ 2: เปิดใช้งาน Apache Userdirs

ตอนนี้ คุณต้องกำหนดค่าเว็บเซิร์ฟเวอร์ Apache ของคุณเพื่อใช้โมดูลนี้ในไฟล์การกำหนดค่า /etc/httpd/conf.d/userdir.conf ซึ่งได้รับการกำหนดค่าด้วยตัวเลือกที่ดีที่สุดแล้ว

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

จากนั้นตรวจสอบเนื้อหาด้านล่าง

directory if a ~user request is received.
#
The path to the end user account 'public_html' directory must be
accessible to the webserver userid.  This usually means that ~userid
must have permissions of 711, ~userid/public_html must have permissions
of 755, and documents contained therein must be world-readable.
Otherwise, the client will only receive a "403 Forbidden" message.
#
<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 tecmint

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

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

หากต้องการอนุญาตให้ผู้ใช้บางรายเข้าถึงไดเรกทอรี UserDir ได้ แต่ไม่ใช่บุคคลอื่น ให้ใช้การตั้งค่าต่อไปนี้ในไฟล์การกำหนดค่า

UserDir disabled
UserDir enabled testuser1 testuser2 testuser3

หากต้องการอนุญาตให้ผู้ใช้ทั้งหมดเข้าถึงไดเร็กทอรี UserDir แต่ปิดการใช้งานนี้สำหรับผู้ใช้บางราย ให้ใช้การตั้งค่าต่อไปนี้ในไฟล์การกำหนดค่า

UserDir enabled
UserDir disabled testuser4 testuser5 testuser6

เมื่อคุณทำการตั้งค่าตามความต้องการแล้ว คุณจะต้องรีสตาร์ทเว็บเซิร์ฟเวอร์ Apache เพื่อใช้การเปลี่ยนแปลงล่าสุด

systemctl restart httpd.service  [On SystemD]
service httpd restart            [On SysVInit]

ขั้นตอนที่ 3: การสร้างไดเรกทอรีผู้ใช้

ตอนนี้คุณต้องสร้างไดเร็กทอรี/ไดเร็กทอรี public_html ในโฮมไดเร็กทอรีของผู้ใช้/ผู้ใช้ ตัวอย่างเช่น ฉันกำลังสร้างไดเร็กทอรี public_html ภายใต้โฮมไดเร็กทอรีของผู้ใช้ tecmint

mkdir /home/tecmint/public_html

จากนั้น ใช้การอนุญาตที่ถูกต้องในไดเรกทอรี home และ public_html ของผู้ใช้

chmod 711 /home/tecmint
chown tecmint:tecmint /home/tecmint/public_html
chmod 755 /home/tecmint/public_html

นอกจากนี้ ให้ตั้งค่าบริบท SELinux ที่ถูกต้องสำหรับ Apache homedirs (httpd_enable_homedirs)

setsebool -P httpd_enable_homedirs true
chcon -R -t httpd_sys_content_t /home/tecmint/public_html

ขั้นตอนที่ 4: ทดสอบ Apache Userdir ที่เปิดใช้งาน

สุดท้าย ตรวจสอบ Userdir โดยชี้เบราว์เซอร์ของคุณไปยังชื่อโฮสต์ของเซิร์ฟเวอร์หรือที่อยู่ IP ตามด้วยชื่อผู้ใช้

http://example.com/~tecmint
OR
http://192.168.0.105/~tecmint

หากต้องการ คุณสามารถทดสอบหน้า HTML และข้อมูล PHP ได้ด้วยการสร้างไฟล์ต่อไปนี้

สร้างไฟล์ /home/tecmint/public_html/test.html โดยมีเนื้อหาดังต่อไปนี้

<html>
  <head>
    <title>TecMint is Best Site for Linux</title>
  </head>
  <body>
    <h1>TecMint is Best Site for Linux</h1>
  </body>
</html>

สร้างไฟล์ /home/tecmint/public_html/test.php โดยมีเนื้อหาดังต่อไปนี้

<?php
  phpinfo();
?>

นั่นคือทั้งหมด! ในบทความนี้ เราได้อธิบายวิธีเปิดใช้งานโมดูล Userdir เพื่อให้ผู้ใช้สามารถแบ่งปันเนื้อหาจากโฮมไดเร็กตอรี่ของตนได้ หากคุณมีข้อสงสัยเกี่ยวกับบทความนี้ โปรดถามในส่วนความคิดเห็นด้านล่าง