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

คำสั่งที่เป็นประโยชน์ในการจัดการ Apache Web Server ใน Linux


ในบทช่วยสอนนี้ เราจะอธิบายคำสั่งการจัดการบริการ Apache (HTTPD) ที่ใช้บ่อยที่สุดบางส่วนที่คุณควรรู้ในฐานะนักพัฒนาหรือผู้ดูแลระบบ และคุณควรเก็บคำสั่งเหล่านี้ไว้เพียงปลายนิ้วสัมผัส เราจะแสดงคำสั่งสำหรับทั้ง Systemd และ SysViit

อ่านเพิ่มเติม: 10 คำสั่ง Nginx ที่ใช้มากที่สุดที่ผู้ใช้ Linux ทุกคนต้องรู้จัก

ตรวจสอบให้แน่ใจว่าคำสั่งต่อไปนี้จะต้องดำเนินการในฐานะผู้ใช้ root หรือ sudo และควรทำงานบนการกระจาย Linux เช่น CentOS, RHEL, Fedora เดเบียน และ Ubuntu

ติดตั้งเซิร์ฟเวอร์อาปาเช่

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

sudo apt install apache2	    [On Debian/Ubuntu]
sudo yum install httpd	    [On RHEL/CentOS]
sudo dnf install httpd	    [On Fedora 22+]
sudo zypper install apache2	    [On openSUSE]

ตรวจสอบเวอร์ชันของ Apache

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

sudo httpd -v
OR
sudo apache2 -v
ผลลัพธ์ตัวอย่าง
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09

หากคุณต้องการแสดงหมายเลขเวอร์ชัน Apache และการตั้งค่าการคอมไพล์ ให้ใช้แฟล็ก -V ตามที่แสดง

sudo httpd -V
OR
sudo apache2 -V
ผลลัพธ์ตัวอย่าง
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

ตรวจสอบข้อผิดพลาดทางไวยากรณ์การกำหนดค่า Apache

หากต้องการตรวจสอบไฟล์การกำหนดค่า Apache ของคุณเพื่อหาข้อผิดพลาดทางไวยากรณ์ ให้รันคำสั่งต่อไปนี้ ซึ่งจะตรวจสอบความถูกต้องของไฟล์กำหนดค่า ก่อนที่จะรีสตาร์ทบริการ

sudo httpd -t
OR
sudo apache2ctl -t
ผลลัพธ์ตัวอย่าง
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using linux-console.net. 
Set the 'ServerName' directive globally to suppress this message
Syntax OK

เริ่มบริการอาปาเช่

หากต้องการเริ่มบริการ Apache ให้เรียกใช้คำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl start httpd     [On Systemd]
sudo service httpd start 	 [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl start apache2   [On Systemd]
sudo service apache2 start     [On SysVInit]

เปิดใช้บริการอาปาเช่

คำสั่งก่อนหน้านี้เริ่มบริการ Apache เท่านั้นในระหว่างนี้ หากต้องการเปิดใช้งานการเริ่มอัตโนมัติเมื่อบูตระบบ ให้รันคำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl enable httpd     [On Systemd]
sudo chkconfig httpd on 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl enable apache2   [On Systemd]
sudo chkconfig apache2 on       [On SysVInit]

เริ่มบริการ Apache ใหม่

หากต้องการรีสตาร์ท Apache (หยุด จากนั้น เริ่ม บริการ) ให้เรียกใช้คำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl restart httpd     [On Systemd]
sudo service httpd restart 	   [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl restart apache2   [On Systemd]
sudo service apache2 restart     [On SysVInit]

ดูสถานะบริการ Apache

หากต้องการตรวจสอบข้อมูลสถานะรันไทม์ของบริการ Apache ให้รันคำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl status httpd     [On Systemd]
sudo service httpd status 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl status apache2   [On Systemd]
sudo service apache2 status     [On SysVInit]

โหลดบริการ Apache อีกครั้ง

หากคุณได้ทำการเปลี่ยนแปลงการกำหนดค่าเซิร์ฟเวอร์ Apache คุณสามารถสั่งให้บริการโหลดการกำหนดค่าใหม่ได้โดยการรันคำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl reload httpd     [On Systemd]
sudo service httpd reload 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl reload apache2   [On Systemd]
sudo service apache2 reload     [On SysVInit]

หยุดบริการ Apache

หากต้องการหยุดบริการ Apache ให้ใช้คำสั่งต่อไปนี้

------------ On CentOS/RHEL ------------ 
sudo systemctl stop httpd       [On Systemd]
sudo service httpd stop 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl stop apache2     [On Systemd]
sudo service apache2 stop     [On SysVInit]

แสดงวิธีใช้คำสั่ง Apache

สุดท้ายแต่ไม่ท้ายสุด คุณสามารถรับความช่วยเหลือเกี่ยวกับคำสั่งบริการ Apache ภายใต้ systemd ได้โดยการเรียกใช้คำสั่งต่อไปนี้

sudo httpd -h
OR
sudo apache2 -h		
OR
systemctl -h apache2	
ผลลัพธ์ตัวอย่าง
Usage: httpd [-D name] [-d directory] [-f file]
             [-C "directive"] [-c "directive"]
             [-k start|restart|graceful|graceful-stop|stop]
             [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in  directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

คุณสามารถค้นหาข้อมูลเพิ่มเติมเกี่ยวกับ systemctl ได้โดยการปรึกษา: วิธีจัดการบริการและหน่วย "Systemd" โดยใช้ "Systemctl" ใน Linux

คุณอาจต้องการอ่านบทความที่เกี่ยวข้องกับ Apache ต่อไปนี้

  1. 5 เคล็ดลับในการเพิ่มประสิทธิภาพของเว็บเซิร์ฟเวอร์ Apache ของคุณ
  2. วิธีตรวจสอบการโหลดเว็บเซิร์ฟเวอร์ Apache และสถิติเพจ
  3. วิธีจัดการ Apache Web Server โดยใช้เครื่องมือ “Apache GUI”
  4. วิธีการเปลี่ยนพอร์ต Apache HTTP ใน Linux
  5. 13 เคล็ดลับความปลอดภัยและความแข็งแกร่งของ Apache Web Server
  6. ปกป้อง Apache จากการโจมตีแบบ Brute Force หรือ DDoS โดยใช้โมดูล Mod_Security และ Mod_evasive

นั่นคือทั้งหมดที่สำหรับตอนนี้! ในบทความนี้ เราได้อธิบายคำสั่งการจัดการบริการ Apache/HTTPD ที่ใช้บ่อยที่สุดที่คุณควรทราบ รวมถึงการเริ่ม เปิดใช้งาน การรีสตาร์ท และการหยุด Apache คุณสามารถติดต่อเราได้ตลอดเวลาผ่านแบบฟอร์มคำติชมด้านล่างหากมีคำถามหรือความคิดเห็น