การเลือกใช้ web server เป็นสิ่งสำคัญสำหรับประสิทธิภาพของเว็บไซต์ หากคุณต้องการได้ Nginx ที่ตรงกับความต้องการเฉพาะของคุณ วิธีที่ดีที่สุดคือการคอมไพล์ Nginx จาก source code ด้วยตัวเอง การทำเช่นนี้ให้คุณควบคุมได้อย่างเต็มที่ว่าจะติดตั้งโมดูลไหน เลือกเวอร์ชันไหน และปรับแต่งเพื่อให้ได้ประสิทธิภาพสูงสุด บทความนี้จะอธิบายขั้นตอนการ compile Nginx จาก source code อย่างละเอียด เพื่อให้คุณสามารถสร้าง Nginx ที่เหมาะสมกับเซิร์ฟเวอร์ของคุณได้
ทำไมต้องคอมไพล์ Nginx จาก Source Code
การติดตั้ง Nginx ผ่าน package manager เช่น apt หรือ yum นั้นง่ายดาย แต่มีข้อจำกัดบางประการ การคอมไพล์ Nginx เองมีข้อดีหลายประการ:
- เพิ่มโมดูลเพิ่มเติม: คุณสามารถเลือกโมดูลที่ต้องการ เช่น gzip compression, SSL/TLS support, หรือโมดูลของบุคคลที่สาม
- ใช้เวอร์ชันล่าสุด: ไม่ต้องรอให้ repository update เวอร์ชันใหม่ของ Nginx
- ปรับแต่งประสิทธิภาพ: คอมไพล์ด้วยแฟล็ก optimization เฉพาะเพื่อให้ได้ประสิทธิภาพสูงสุด
- ควบคุม dependencies: ติดตั้งเฉพาะไลบรารีที่จำเป็น ไม่มีส่วนที่ไม่ใช้
- ความเป็นอิสระ: ไม่ขึ้นอยู่กับการอัพเดตของ distribution นั้น ๆ
ข้อกำหนดเบื้องต้น (Prerequisites)
ก่อนที่จะ compile Nginx ให้แน่ใจว่าคุณมีเครื่องมือและไลบรารีที่จำเป็นติดตั้งบนเซิร์ฟเวอร์ของคุณ
โปรแกรมแปลภาษา (Compiler)
ติดตั้ง GCC (GNU Compiler Collection) ซึ่งเป็นโปรแกรมแปลภาษา C/C++ ที่ใช้ในการคอมไพล์ Nginx
# สำหรับ Debian/Ubuntu
sudo apt-get update
sudo apt-get install build-essential
# สำหรับ CentOS/RHEL
sudo yum groupinstall "Development Tools"
ไลบรารีที่จำเป็น (Required Libraries)
Nginx ต้องการไลบรารีสำคัญต่อไปนี้ ติดตั้งตามระบบปฏิบัติการของคุณ:
- PCRE (Perl Compatible Regular Expression): สำหรับรองรับ regular expressions ในการกำหนดค่า Nginx
- zlib: สำหรับ gzip compression
- OpenSSL: สำหรับ HTTPS/TLS support
# สำหรับ Debian/Ubuntu
sudo apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libgeoip-dev
# สำหรับ CentOS/RHEL
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel libgd-devel geoip-devel
ดาวน์โหลด Nginx Source Code
ต่อไป ให้ดาวน์โหลด source code ของ Nginx จากเว็บไซต์อย่างเป็นทางการ
# เข้าไปยังไดเรกทอรี source
cd /usr/local/src
# ดาวน์โหลด Nginx เวอร์ชันล่าสุด (แทน x.x.x ด้วยเวอร์ชันที่ต้องการ)
wget http://nginx.org/download/nginx-1.26.0.tar.gz
# แตกไฟล์ (decompress)
tar -xzf nginx-1.26.0.tar.gz
# เข้าไปยังไดเรกทอรี
cd nginx-1.26.0
ตรวจสอบเวอร์ชันล่าสุดได้ที่ http://nginx.org หรือดู stable version ที่มีการสนับสนุนระยะยาว
ตัวเลือก Configure ที่สำคัญ
ขั้นตอน configure นั้นมีความสำคัญที่สุดในการ compile Nginx ที่เหมาะสม คำสั่ง ./configure ใช้เพื่อกำหนดว่าจะสร้าง Nginx แบบไหน
ตัวเลือกหลักที่ใช้บ่อย
| ตัวเลือก | คำอธิบาย |
|---|---|
--prefix=PATH |
กำหนดตำแหน่ง root directory ของ Nginx (ค่าเริ่มต้น: /etc/nginx) |
--sbin-path=PATH |
กำหนดตำแหน่ง nginx binary executable |
--conf-path=PATH |
กำหนดตำแหน่งไฟล์ configuration (nginx.conf) |
--error-log-path=PATH |
กำหนดตำแหน่งไฟล์ error log |
--http-log-path=PATH |
กำหนดตำแหน่งไฟล์ access log |
--pid-path=PATH |
กำหนดตำแหน่งไฟล์ PID |
--lock-path=PATH |
กำหนดตำแหน่งไฟล์ lock |
โมดูลบาง (Module Options)
| ตัวเลือก | คำอธิบาย |
|---|---|
--with-http_ssl_module |
เพิ่ม HTTPS/SSL support (บ่อยครั้งที่ต้องใช้) |
--with-http_gzip_static_module |
เพิ่ม static gzip compression |
--with-http_realip_module |
เพิ่ม real IP module สำหรับ reverse proxy |
--with-http_v2_module |
เพิ่ม HTTP/2 support |
--with-http_geoip_module |
เพิ่ม GeoIP module สำหรับ geo-blocking |
--with-stream |
เพิ่ม TCP/UDP load balancing module |
--with-http_v3_module |
เพิ่ม QUIC/HTTP3 support (Nginx 1.25+) |
โมดูลที่ปิดใช้งาน (Disable Options)
หากต้องการลดขนาด Nginx หรือไม่ต้องใช้โมดูลบางตัว ใช้ --without-:
--without-http_charset_module
--without-http_ssi_module
--without-http_userid_module
--without-http_access_module
เพิ่มโมดูลของบุคคลที่สาม (Third-party Modules)
สามารถเพิ่มโมดูลเพิ่มเติมจากบุคคลที่สาม ด้วยตัวเลือก --add-module หรือ --add-dynamic-module:
# Statically compiled module
./configure --add-module=/path/to/module/source
# Dynamic module (can be loaded/unloaded at runtime)
./configure --add-dynamic-module=/path/to/module/source
ตัวอย่าง Configure ทั่วไป
ตัวอย่าง 1: Configure ขั้นต่ำ
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid
ตัวอย่าง 2: Configure สำหรับการใช้งานแบบ HTTPS พื้นฐาน
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module
ตัวอย่าง 3: Configure สำหรับการใช้งานขั้นสูงพร้อม HTTP/2 และ Stream
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_geoip_module \
--with-stream \
--with-stream_ssl_module
ตัวอย่าง 4: Configure ที่เหมาะสมสำหรับ Reverse Proxy และ Load Balancer
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-stream \
--with-stream_ssl_module \
--without-http_charset_module \
--without-http_ssi_module \
--without-http_userid_module
ขั้นตอน Make และ Make Install
หลังจากรัน configure แล้ว ต้องคอมไพล์ source code และติดตั้ง Nginx
# คอมไพล์ Nginx
make
# ติดตั้ง Nginx
sudo make install
กระบวนการ make อาจใช้เวลาสักครู่ ขึ้นอยู่กับความเร็วของเซิร์ฟเวอร์และโมดูลที่เลือก
การสร้างไฟล์ Systemd Service
เมื่อติดตั้ง Nginx เสร็จแล้ว สร้างไฟล์ systemd service เพื่อบริหาร Nginx ได้ง่ายขึ้น
sudo nano /etc/systemd/system/nginx.service
ใส่เนื้อหาต่อไปนี้:
[Unit]
Description=The NGINX HTTP and Reverse Proxy Server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
บันทึกและปิดไฟล์ จากนั้นเปิดใช้งาน Nginx service:
# อ่านไฟล์ systemd ใหม่
sudo systemctl daemon-reload
# เปิดใช้งาน Nginx เมื่อ boot
sudo systemctl enable nginx
# เริ่ม Nginx
sudo systemctl start nginx
# ตรวจสอบสถานะ
sudo systemctl status nginx
การอัพเกรด Nginx โดยไม่ระงับการทำงาน
หนึ่งในข้อดีของการคอมไพล์ Nginx เองคือสามารถอัพเกรดเป็นเวอร์ชันใหม่โดยไม่ต้องปิด Nginx ได้ วิธีนี้เรียกว่า “binary upgrade”
# 1. ดาวน์โหลดและแตกไฟล์เวอร์ชันใหม่
cd /usr/local/src
wget http://nginx.org/download/nginx-1.26.1.tar.gz
tar -xzf nginx-1.26.1.tar.gz
cd nginx-1.26.1
# 2. Configure ด้วยตัวเลือกเดียวกับเดิม (สำคัญมาก!)
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx \
[ใส่ตัวเลือก configure อื่น ๆ ที่ใช้ก่อนหน้านี้]
# 3. Compile (ไม่ต้อง make install)
make
# 4. Backup binary เดิม
sudo cp /usr/sbin/nginx /usr/sbin/nginx.old
# 5. Copy binary ใหม่
sudo cp objs/nginx /usr/sbin/nginx
# 6. ทดสอบ configuration
sudo /usr/sbin/nginx -t
# 7. ส่ง SIGUSR2 signal ให้ Nginx เดิมเพื่อให้ master process ทำงานคู่ขนาน
sudo kill -USR2 $(cat /var/run/nginx.pid)
# 8. ตรวจสอบว่า process ทำงานสองตัว
ps aux | grep nginx
# 9. ส่ง SIGWINCH signal ให้ process เดิมปิด worker processes
sudo kill -WINCH $(cat /var/run/nginx.pid.oldbin)
# 10. หากต้องการ rollback ส่ง SIGTERM ให้ master process เดิม
# sudo kill -TERM $(cat /var/run/nginx.pid.oldbin)
# 11. หากสำเร็จ ส่ง SIGTERM ให้ master process เดิมจบการทำงาน
sudo kill -TERM $(cat /var/run/nginx.pid.oldbin)
แฟล็ก Optimize สำหรับประสิทธิภาพ
หากต้องการให้ Nginx มีประสิทธิภาพสูงสุด สามารถเพิ่มแฟล็ก optimization ในระหว่าง configure ได้
./configure \
[ตัวเลือก configure ที่ต้องการ] \
--with-cc-opt='-O3 -march=native' \
--with-ld-opt='-Wl,-rpath,/usr/local/lib'
ตัวเลือก -O3 เป็น aggressive optimization ที่ให้ประสิทธิภาพดีที่สุด -march=native ใช้ instruction sets ที่เหมาะสมกับ CPU ของเซิร์ฟเวอร์
ตรวจสอบ Build Nginx
หลังติดตั้ง Nginx เสร็จ ตรวจสอบว่าติดตั้งถูกต้องและโมดูลที่ต้องการติดตั้งแล้ว
# ตรวจสอบเวอร์ชัน
nginx -v
# ดูข้อมูลโมดูลที่ติดตั้ง
nginx -V
# ตรวจสอบ configuration syntax
nginx -t
# ตรวจสอบ configuration พร้อมแสดงตำแหน่งไฟล์ที่ใช้
nginx -T
คำสั่ง nginx -V จะแสดง configure options ทั้งหมดที่ใช้ในการ compile Nginx และรายการโมดูลที่ติดตั้ง
การแก้ไขข้อผิดพลาดในการ Compile
หากเกิดข้อผิดพลาดในระหว่างการ compile Nginx สามารถทำการ troubleshoot ได้ดังนี้
ข้อผิดพลาด: Could not find PCRE library
# ติดตั้ง PCRE development files
sudo apt-get install libpcre3-dev # Debian/Ubuntu
sudo yum install pcre-devel # CentOS/RHEL
ข้อผิดพลาด: Could not find OpenSSL library
# ติดตั้ง OpenSSL development files
sudo apt-get install libssl-dev # Debian/Ubuntu
sudo yum install openssl-devel # CentOS/RHEL
ข้อผิดพลาด: C compiler “cc” not found
# ติดตั้ง build tools
sudo apt-get install build-essential # Debian/Ubuntu
sudo yum groupinstall "Development Tools" # CentOS/RHEL
ทำความสะอาด Previous Compile (Clean Build)
หากต้องการ compile ใหม่หลังจากข้อผิดพลาด ทำความสะอาดก่อน:
make clean
./configure [ตัวเลือก configure ใหม่]
make
sudo make install
Best Practices สำหรับการ Compile Nginx
- ตรวจสอบเวอร์ชัน: ดาวน์โหลด Nginx จากเว็บไซต์อย่างเป็นทางการ (nginx.org) เท่านั้น เพื่อความปลอดภัย
- ใช้ configure options เดียวกัน: เมื่อมีการอัพเกรด ตรวจสอบว่า configure options เหมือนเดิม จึงจะได้ Nginx ที่ compatible
- Backup binary เดิม: ก่อน make install ให้ backup binary เดิมไว้ เพื่อกรณี rollback
- ทดสอบ configuration: เสมอรัน
nginx -tก่อนที่จะ reload หรือ restart Nginx - ติดตั้ง modules ที่จำเป็น: เลือกเพิ่มเฉพาะโมดูลที่ใช้จริง เพื่อลดขนาดและเพิ่มความปลอดภัย
- จดบันทึก configure options: เก็บ configure command ไว้เพื่อง่ายต่อการอัพเกรดในอนาคต
- ทดสอบก่อน production: ทดสอบ Nginx ใหม่บนเซิร์ฟเวอร์ staging ก่อนที่จะใช้ใน production
สรุป
การ compile Nginx จาก source code อาจดูซับซ้อนในแวบแรก แต่เมื่อปฏิบัติตามขั้นตอนอย่างระมัดระวัง ก็ค่อนข้างตรงไปตรงมา ข้อดีของการ compile Nginx เองมีมากมาย ตั้งแต่ความเป็นอิสระในการเลือกโมดูล ความสามารถในการอัพเกรดโดยไม่ระงับการทำงาน จนถึงการปรับแต่งประสิทธิภาพ หากคุณต้องการควบคุม web server ของคุณแบบเต็มที่ การ compile Nginx เองเป็นทางเลือกที่ดีที่สุด
ขอให้ระวัง ตรวจสอบการกำหนดค่าอย่างรอบคอบ และอย่าลืมทำการ backup ก่อนปรับเปลี่ยน เพื่อให้เซิร์ฟเวอร์ของคุณปลอดภัยและทำงานได้อย่างเสถียร
แนะนำบริการ Dot Enterprise (DE) Cloud VPS
หากคุณต้องการเซิร์ฟเวอร์ที่มีความยืดหยุ่นและสามารถคอมไพล์ Nginx ตามต้องการได้ Dot Enterprise Cloud VPS เป็นตัวเลือกที่ดี ด้วย Dot Enterprise Cloud VPS คุณจะได้:
- การเข้าถึง Root: การเข้าถึง root ระดับเต็มเพื่อติดตั้งและ compile ซอฟต์แวร์ตามต้องการ
- ทรัพยากรที่ปรับเปลี่ยนได้: สามารถเพิ่ม CPU, RAM, และ storage ได้ตามความต้องการ
- การสนับสนุนที่ดี: ทีม support ของ DE พร้อมช่วยเหลือเมื่อพบปัญหา
- ราคาที่เข้าถึงได้: ราคาที่แข่งขันกับคุณภาพบริการที่ดี
ติดต่อ Dot Enterprise หรือเข้าดูข้อมูลเพิ่มเติม ที่ Cloud VPS เพื่อเริ่มต้นใช้งาน

