วิธีติดตั้ง SSL Certificate กับ Apache 2 สำหรับ CentoS, Ubuntu, Linux Server
เตรียม SSL Certificate ก่อนติดตั้ง ให้กับ Apache
ขั้นตอนเตรียมการก่อนติดตั้ง SSL Certificate ให้กับ Apache ของท่าน
- ทำการสั่งซื้อ SSL Certificate ผ่านระบบออนไลน์ทาง
https://www.ireallyhost.com/cart/ssl
- ทำการเปิดใช้งาน SSL Certificate
https://www.ireallyhost.com/client/service/ssl/
1. เริ่มต้นติดตั้ง SSL กับ Apache 2 กับ Linux Server
ทำการติดตั้ง mod_ssl และ ตรวจสอบว่าติดตั้ง mod_ssl เรียบร้อยแล้ว
#สำหรับ Ubuntu สั่ง
sudo a2enmod ssl
sudo a2enmod vhost_alias
#สำหรับ CentOS สั่ง
yum -y install mod_ssl
#ตรวจสอบผลการติดตั้ง mod_ssl
apache2ctl -M | grep ssl
หรือ
httpd -M | grep ssl
ก่อนติดตั้ง ทำการตรวจสอบให้แน่ชัด ว่า httpd หรือ apache ติดตั้งอยู่ที่ใด
โดยมากหากติดตั้ง apache2 ที่ ubuntu จะอยู่ที่ /etc/apache2 หรือ หากติดตั้งบน centos มักอยู่ที่ /etc/httpd
ตัวอย่างด้านล่างนี้จะถือว่า path ของ apache อยู่ที่ /etc/httpd
หาก web server ของท่านติดตั้งอยู่ที่ /etc/apache2 ให้ทำการแก้ไข path จากให้ถูกต้องต้องด้วย
พิมพ์คำสั่งนี้เพื่อตรวจสอบว่าติดตั้งอยู่ที่ใด
whereis apache2 | grep /etc/apache* && whereis httpd | grep /etc/httpd
ทำการแก้ไขไฟล์ config หลักของ httpd.conf
- vi /etc/httpd/conf/httpd.conf
- หรือ /etc/apache2/httpd.conf
- หรือ /etc/apache2/apache2.conf
ค้นหาส่วนที่ 1
#LoadModule ssl_module modules/mod_ssl.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
แก้ไขนำ # Comment ด้านหน้าออก
LoadModule ssl_module modules/mod_ssl.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
ค้นหาส่วนที่ 2
#Include conf/extra/httpd-ssl.conf
แก้ไขนำ # Comment ด้านหน้าออก
Include conf/extra/httpd-ssl.conf
ทำการ Save ไฟล์
3. วิธีการติดตั้ง SSL Certificate ให้กับ Apache หรือ Apache2
* ทำการเปลี่ยนชื่อ domain_com เป็นชื่อโดเมนเนมของท่าน
- สร้างโฟลเดอร์สำหรับเก็บไฟล์ Certificate เช่น
สามารถทำการเปลี่ยน domain_com เป็นชื่อโดเมนของท่าน- mkdir -p /etc/httpd/conf/ssl/domain_com_ชื่อโดมเนของท่าน
- mkdir -p /etc/httpd/conf/ssl/domain_com_ชื่อโดมเนของท่าน
- นำไฟล์ Certificate ต่างๆ ใส่ไว้ยังโฟลเดอร์
- ใส่ Private Key ในไฟล์
/etc/httpd/conf/ssl/domain_com/private.key
- ใส่ SSL Domain Certificate ในไฟล์
/etc/httpd/conf/ssl/domain_com/domain_name{ชื่อโดเมนของท่าน}.crt
- ใส่ CA Root ในไฟล์
/etc/httpd/conf/ssl/domain_com/CARootCertificate-ca.crt
** โปรดตรวจสอบชื่อ และนามสกุลไฟล์ ต้องตรงกัน ระหว่างไฟล์ กับ ค่า config ใน .conf ไฟล์
หากไม่ตรง สามารถทำการ rename เปลี่ยนชื่อ และนามสกุลของไฟล์ cert
หรือตั้งชื่อตามที่ต้องการได้
- ใส่ Private Key ในไฟล์
- ทำการสร้างไฟล์ httpd config สำหรับ Website ของท่าน หรือ ทำการแก้ไข เช่น
หรือvi /etc/httpd/sites-enabled/your-domain_com.conf
vi /etc/apache2/sites-available/domain.com.conf a2ensite domain.com.conf
ตัวอย่าง Config สำหรับ SSL สำหรับ 1 โดเมนเนม<VirtualHost *:443> DocumentRoot "/var/www/html/" ServerName your-domain.com ServerAlias www.your-domain.com #ErrorLog ${APACHE_LOG_DIR}/error.log #CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile "/etc/httpd/conf/ssl/domain_com/domain_name.crt" SSLCertificateKeyFile "/etc/httpd/conf/ssl/domain_com/private.key" SSLCertificateChainFile "/etc/httpd/conf/ssl/domain_com/CARootCertificate-ca.crt" </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/html/" ServerName your-domain.com ServerAlias www.your-domain.com #ErrorLog ${APACHE_LOG_DIR}/error.log #CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
ตัวอย่าง Config สำหรับ Sub domain Wildcard SSL<VirtualHost *:443> DocumentRoot "/var/www/html/" ServerAlias *.your-domain.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile "/etc/httpd/conf/ssl/domain_com/domain_name.crt" SSLCertificateKeyFile "/etc/httpd/conf/ssl/domain_com/private.key" SSLCertificateChainFile "/etc/httpd/conf/ssl/domain_com/CARootCertificate-ca.crt" </VirtualHost>
4. ทดสอบความถูกต้อง การตั้งค่า Config และ สั่ง restart service apache
- apache2ctl configtest
- /etc/init.d/apache2 restart
วิธีแก้ไขปัญหา error ต่างๆ
Enable the SSL module
AH00526: Syntax error on line ... of /etc/apache2/apache2.conf:
Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server config
วิธีการแก้ไข ให้ทำการ enable the SSL module
สำหรับ Ubuntu สั่ง sudo a2enmod ssl
สำหรับ CentOS สั่ง yum -y install mod_ssl
============================================
หากพบปัญหาติด Firewall
- ทำการ add port 443 (โดยปกติเปิดโดย default)
sudo ufw allow 443
- ตรวจสอบสถานะ firewall
ufw status
============================================
โหลด mod_ssl.so และ mod_vhost_alias.so แล้ว
ตรวจสอบการเรียก mod_ssl จาก httpd.conf หรือ apache2.conf
ตรวจสอบไฟล์ /etc/httpd/mods-available/ssl.load
- LoadModule ssl_module modules/mod_ssl.so
ตรวจสอบไฟล์ /etc/httpd/mods-available/vhost_alias.load
- LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so
ตรวจสอบ Apache 2 มี mod_ssl แล้วหรือไม่
- apache2ctl -M | grep ssl
อ้างอิงอื่นๆ
https://www.ireallyhost.com/kb/ssl/414
https://www.debuntu.org/how-to-enable-apache-modules-under-debian-based-system-page-2/
https://medium.com/@rachatatongpagdee/%E0%B8%97%E0%B8%B3-firewall-%E0%B8%9A%E0%B8%99-ubuntu-server-16-04-lts-8880e59729ed
** บทความนี้มีลิขสิทธิ์ ไม่อนุญาติให้คัดลอก ทำซ้ำ ดัดแปลงก่อนได้รับอนุญาต **
โปรดระบุแหล่งที่มา บริษัท เอ็กซ์ตร้า คอร์ปอเรชั่น จำกัด / https://www.ireallyhost.com