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

วิธีรักษาความปลอดภัย Apache ด้วย SSL และเข้ารหัสใน FreeBSD


ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีรักษาความปลอดภัยเซิร์ฟเวอร์ Apache HTTP ด้วยใบรับรอง TLS/SSL ที่นำเสนอโดย Let's Encrypt ใน FreeBSD 11 x. นอกจากนี้เรายังจะกล่าวถึงวิธีทำให้กระบวนการต่ออายุใบรับรองสำหรับ Lets' Encrypt เป็นแบบอัตโนมัติ

อ่านเพิ่มเติม: ติดตั้ง Let's Encrypt สำหรับ Nginx บน FreeBSD

เว็บเซิร์ฟเวอร์ Apache จะใช้ใบรับรอง TLS/SSL เพื่อเข้ารหัสการสื่อสารระหว่างโหนดปลายทาง หรือแบบธรรมดาระหว่างเซิร์ฟเวอร์และไคลเอ็นต์เพื่อให้การรักษาความปลอดภัย Let’s Encrypt มียูทิลิตีบรรทัดคำสั่ง certbot ซึ่งเป็นแอปพลิเคชันที่สามารถอำนวยความสะดวกในการรับใบรับรองที่เชื่อถือได้ได้ฟรี

ความต้องการ:

  1. การติดตั้ง FreeBSD 11.x
  2. 10 สิ่งที่ต้องทำหลังจากติดตั้ง FreeBSD
  3. วิธีการติดตั้ง Apache, MariaDB และ PHP ใน FreeBSD

ขั้นตอนที่ 1: กำหนดค่า Apache SSL บน FreeBSD

1. ก่อนที่จะเริ่มติดตั้งยูทิลิตี้ certbot และสร้างไฟล์การกำหนดค่า TSL สำหรับ Apache ขั้นแรกให้สร้างไดเร็กทอรีที่แตกต่างกันสองไดเร็กทอรีชื่อ sites-available และ เปิดใช้งานไซต์ ในไดเร็กทอรีการกำหนดค่ารูทของ Apache โดยใช้คำสั่งด้านล่าง

วัตถุประสงค์ของทั้งสองไดเร็กทอรีนี้คือเพื่ออำนวยความสะดวกในการจัดการการกำหนดค่าโฮสติ้งเสมือนในระบบ โดยไม่ต้องแก้ไขไฟล์การกำหนดค่า Apache httpd.conf หลักทุกครั้งที่เราเพิ่มโฮสต์เสมือนใหม่

mkdir /usr/local/etc/apache24/sites-available
mkdir /usr/local/etc/apache24/sites-enabled

2. หลังจากที่คุณสร้างทั้งสองไดเร็กทอรีแล้ว ให้เปิดไฟล์ Apache httpd.conf ด้วยโปรแกรมแก้ไขข้อความ และเพิ่มบรรทัดต่อไปนี้ใกล้กับส่วนท้ายของไฟล์ดังภาพด้านล่าง

nano /usr/local/etc/apache24/httpd.conf

เพิ่มบรรทัดต่อไปนี้:

IncludeOptional etc/apache24/sites-enabled/*.conf

3. ถัดไป เปิดใช้งานโมดูล TLS สำหรับ Apache โดยสร้างไฟล์ใหม่ต่อไปนี้ชื่อ 020_mod_ssl.conf ใน modules.d< ไดเร็กทอรีที่มีเนื้อหาดังต่อไปนี้

nano /usr/local/etc/apache24/modules.d/020_mod_ssl.conf

เพิ่มบรรทัดต่อไปนี้ในไฟล์ 020_mod_ssl.conf

Listen 443
SSLProtocol ALL -SSLv2 -SSLv3
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLPassPhraseDialog  builtin
SSLSessionCacheTimeout  300

4. ตอนนี้ ยกเลิกหมายเหตุโมดูล SSL จากไฟล์ /usr/local/etc/apache24/httpd.conf โดยลบแฮชแท็กออกจากจุดเริ่มต้น ของบรรทัดต่อไปนี้ดังภาพด้านล่าง:

LoadModule ssl_module libexec/apache24/mod_ssl.so

5. จากนั้น สร้างไฟล์การกำหนดค่า TLS สำหรับโดเมนของคุณในไดเรกทอรี ไซต์ที่มีอยู่ โดยควรใช้ชื่อโดเมนของคุณตามที่แสดงใน ข้อความที่ตัดตอนมาด้านล่าง:

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

เพิ่มการกำหนดค่าโฮสต์เสมือนต่อไปนี้ลงในไฟล์ bsd.lan-ssl.conf

<VirtualHost *:443>
    ServerName www.yourdomain.com
	ServerAlias yourdomain.com
                DocumentRoot "/usr/local/www/apache24/data/"
	SSLEngine on

	SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>

<Directory "/usr/local/www/apache24/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

	BrowserMatch "MSIE [2-5]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0

	CustomLog "/var/log/apache/httpd-ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

	<Directory "/usr/local/www/apache24/data/">
            Options Indexes FollowSymLinks MultiViews
        #AllowOverride controls what directives may be placed in .htaccess files.       
                        AllowOverride All
        #Controls who can get stuff from this server file
                        Require all granted
        </Directory>
       
    ErrorLog "/var/log/apache/yourdomain.ssl-error.log"
    CustomLog "/var/log/apache/yourdomain.ssl-access_log" combined

</VirtualHost>

ตรวจสอบให้แน่ใจว่าคุณได้แทนที่ตัวแปรชื่อโดเมนจากคำสั่ง ServerName, ServerAlias, ErrorLog, CustomLog ตามลำดับ

ขั้นตอนที่ 2: ติดตั้ง Lets'Encrypt บน FreeBSD

6. ในขั้นตอนถัดไป ให้ออกคำสั่งต่อไปนี้เพื่อติดตั้งยูทิลิตี้ certbot ที่ได้รับจาก Let's Encrypt ซึ่งจะใช้ในการรับ Apache TSL ใบรับรองฟรีสำหรับโดเมนของคุณ

ขณะติดตั้ง certbot ชุดข้อความแจ้งจะปรากฏขึ้นบนหน้าจอของคุณ ใช้ภาพหน้าจอด้านล่างเพื่อกำหนดค่ายูทิลิตี certbot นอกจากนี้ การคอมไพล์และติดตั้งยูทิลิตี้ certbot อาจใช้เวลาสักครู่ ขึ้นอยู่กับทรัพยากรเครื่องของคุณ

cd /usr/ports/security/py-certbot
make install clean

7. หลังจากกระบวนการคอมไพล์เสร็จสิ้น ให้ออกคำสั่งด้านล่างเพื่ออัปเดตยูทิลิตี้ certbot และการอ้างอิงที่จำเป็น certbot

pkg install py27-certbot
pkg install py27-acme

8. ในการสร้างใบรับรองสำหรับโดเมนของคุณ ให้ออกคำสั่งตามที่แสดงด้านล่าง ตรวจสอบให้แน่ใจว่าคุณระบุตำแหน่งเว็บรูทที่ถูกต้องซึ่งไฟล์เว็บไซต์ของคุณถูกจัดเก็บอยู่ในระบบไฟล์ (คำสั่ง DocumentRoot จากไฟล์การกำหนดค่าโดเมนของคุณ) โดยใช้แฟล็ก -w หากคุณมีโดเมนย่อยหลายรายการ ให้เพิ่มทั้งหมดโดยใช้แฟล็ก -d

certbot certonly --webroot -w /usr/local/www/apache24/data/ -d yourdomain.com -d www.yourdomain.com

ขณะรับใบรับรอง ให้ระบุที่อยู่อีเมลสำหรับการต่ออายุใบรับรอง กด a เพื่อยอมรับข้อกำหนดและเงื่อนไขของ Let's Encrypt และ n ที่จะไม่เปิดเผยที่อยู่อีเมลของพันธมิตร Let's Encrypt

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):[email 
There seem to be problems with that address. Enter email address (used for
urgent renewal and security notices)  If you really want to skip this, you can
run the client with --register-unsafely-without-email but make sure you then
backup your account key from /etc/letsencrypt/accounts   (Enter 'c' to cancel):[email 

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a ------------------------------------------------------------------------------- Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: n Obtaining a new certificate Performing the following challenges: http-01 challenge for www.domain.com Using the webroot path /usr/local/www/apache24/data for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem. Your cert will expire on 2017-11-15. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /usr/local/etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

9. หลังจากที่คุณได้รับใบรับรองสำหรับโดเมนของคุณแล้ว คุณสามารถเรียกใช้คำสั่ง ls เพื่อแสดงรายการส่วนประกอบของใบรับรองทั้งหมด (เชน คีย์ส่วนตัว ใบรับรอง) ตามที่แสดงในตัวอย่างด้านล่าง

ls -al /usr/local/etc/letsencrypt/live/www.yourdomain.com/

ขั้นตอนที่ 3: อัปเดตใบรับรอง Apache TLS บน FreeBSD

10. ในการเพิ่มใบรับรอง Let's Encrypt ไปยังเว็บไซต์ของคุณ ให้เปิดไฟล์การกำหนดค่า apache สำหรับโดเมนของคุณและอัปเดตบรรทัดต่อไปนี้เพื่อแสดงเส้นทางของใบรับรองที่ออก

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

เพิ่มบรรทัดใบรับรอง TLS เหล่านี้:

SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

11. สุดท้าย ให้เปิดใช้งานไฟล์การกำหนดค่า TLS โดยการสร้าง symlink สำหรับไฟล์การกำหนดค่า TLS ของโดเมนของคุณไปยังไดเร็กทอรี ที่เปิดใช้งานไซต์ ให้ตรวจสอบการกำหนดค่า Apache เพื่อดูข้อผิดพลาดทางไวยากรณ์ที่เป็นไปได้ และหาก ไวยากรณ์ก็โอเค ให้รีสตาร์ท Apache daemon โดยใช้คำสั่งด้านล่าง

ln -sf /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf /usr/local/etc/apache24/sites-enabled/
apachectl -t
service apache24 restart

12. เพื่อตรวจสอบว่าบริการ Apache กำลังรับฟังบนพอร์ต HTTPS 443 หรือไม่ ให้ใช้คำสั่งต่อไปนี้เพื่อแสดงรายการซ็อกเก็ตเครือข่าย httpd

sockstat -4 | grep httpd

13. คุณสามารถนำทางไปยังที่อยู่โดเมนของคุณจากเบราว์เซอร์ผ่านโปรโตคอล HTTPS เพื่อยืนยันว่ามีการใช้ใบรับรอง Let’s Encrypt สำเร็จแล้ว

https://www.yourdomain.com

14. หากต้องการรับข้อมูลเพิ่มเติมเกี่ยวกับใบรับรอง Let's Encrypt ที่ออกให้จากบรรทัดคำสั่ง ให้ใช้คำสั่ง openssl ดังต่อไปนี้

openssl s_client -connect www.yourdomain.com:443

15. คุณยังสามารถตรวจสอบได้ว่าการรับส่งข้อมูลได้รับการเข้ารหัสด้วยใบรับรองที่ถูกต้องที่ มาเข้ารหัส CA จากอุปกรณ์มือถือดังที่แสดงในภาพหน้าจอมือถือด้านล่าง

นั่นคือทั้งหมด! ขณะนี้ลูกค้าสามารถเยี่ยมชมเว็บไซต์ของคุณได้อย่างปลอดภัย เนื่องจากการรับส่งข้อมูลที่ไหลระหว่างเซิร์ฟเวอร์และเบราว์เซอร์ของลูกค้าได้รับการเข้ารหัส สำหรับงานที่ซับซ้อนมากขึ้นเกี่ยวกับยูทิลิตี้ certbot โปรดไปที่ลิงก์ต่อไปนี้: https://certbot.eff.org/