Android

การรักษาความปลอดภัย Apache ด้วย Let's เข้ารหัสบน Ubuntu 18.04

विदà¥?र: ये ६ लोग जीवनà¤à¤° दà¥?ःख और दरà¥?द हà¥

विदà¥?र: ये ६ लोग जीवनà¤à¤° दà¥?ःख और दरà¥?द हà¥

สารบัญ:

Anonim

Let's Encrypt เป็นผู้ออกใบรับรองที่สร้างขึ้นโดย Internet Security Research Group (ISRG) มันมอบใบรับรอง SSL ฟรีผ่านกระบวนการอัตโนมัติที่ออกแบบมาเพื่อกำจัดการสร้างใบรับรองด้วยตนเองการตรวจสอบความถูกต้องการติดตั้งและการต่ออายุ

ใบรับรองที่ออกโดย Let's Encrypt นั้นได้รับความไว้วางใจจากเบราว์เซอร์หลักทุกวันนี้

ในบทช่วยสอนนี้เราจะให้คำแนะนำทีละขั้นตอนเกี่ยวกับการรักษาความปลอดภัย Apache ของคุณด้วย Let's Encrypt โดยใช้เครื่องมือ certbot บน Ubuntu 18.04

ข้อกำหนดเบื้องต้น

ตรวจสอบให้แน่ใจว่าคุณได้ทำตามข้อกำหนดเบื้องต้นต่อไปนี้ก่อนที่จะดำเนินการกับบทช่วยสอนนี้:

  • ชื่อโดเมนที่ชี้ไปที่ IP เซิร์ฟเวอร์สาธารณะของคุณ เราจะใช้ example.com คุณมี Apache ติดตั้งกับโฮสต์เสมือน apache สำหรับโดเมนของคุณ

ติดตั้ง Certbot

Certbot เป็นเครื่องมือที่มีคุณลักษณะครบถ้วนและใช้งานง่ายซึ่งสามารถทำงานอัตโนมัติเพื่อรับและต่ออายุ Let's Encrypt SSL certificate และการกำหนดค่าเว็บเซิร์ฟเวอร์ แพคเกจ certbot รวมอยู่ในที่เก็บเริ่มต้นของ Ubuntu

อัพเดตรายการแพ็กเกจและติดตั้งแพ็กเกจ certbot:

sudo apt update sudo apt install certbot

สร้างกลุ่ม Strong Dh (Diffie-Hellman)

การแลกเปลี่ยนคีย์ Diffie – Hellman (DH) เป็นวิธีการแลกเปลี่ยนคีย์เข้ารหัสลับอย่างปลอดภัยผ่านช่องทางการสื่อสารที่ไม่ปลอดภัย เราจะสร้างพารามิเตอร์ DH 2048 บิตใหม่เพื่อเสริมความปลอดภัย:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 หากคุณต้องการคุณสามารถเปลี่ยนขนาดได้สูงสุด 4096 บิต แต่ในกรณีนั้นการสร้างอาจใช้เวลานานกว่า 30 นาทีขึ้นอยู่กับเอนโทรปีของระบบ

รับใบรับรอง Let's Encrypt SSL

ในการรับใบรับรอง SSL สำหรับโดเมนเราจะใช้ปลั๊กอิน Webroot ที่ทำงานโดยการสร้างไฟล์ชั่วคราวสำหรับการตรวจสอบโดเมนที่ร้องขอในไดเรกทอรี ${webroot-path}/.well-known/acme-challenge เซิร์ฟเวอร์ Let's Encrypt ส่งคำขอ HTTP ไปยังไฟล์ชั่วคราวเพื่อตรวจสอบว่าโดเมนที่ร้องขอแก้ไขไปยังเซิร์ฟเวอร์ที่ Certbot ทำงานอยู่

เพื่อให้ง่ายยิ่งขึ้นเราจะทำแผนที่คำขอ HTTP ทั้งหมดสำหรับ .well-known/acme-challenge ไปยังไดเรกทอรีเดียว /var/lib/letsencrypt

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

sudo mkdir -p /var/lib/letsencrypt/.well-known sudo chgrp www-data /var/lib/letsencrypt sudo chmod g+s /var/lib/letsencrypt

หากต้องการหลีกเลี่ยงการทำรหัสซ้ำให้สร้างตัวอย่างข้อมูลการกำหนดค่าสองรายการต่อไปนี้:

/etc/apache2/conf-available/letsencrypt.conf

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS /etc/apache2/conf-available/ssl-params.conf

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 SSLHonorCipherOrder On Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff # Requires Apache >= 2.4 SSLCompression off SSLUseStapling on SSLStaplingCache "shmcb:logs/stapling-cache(150000)" # Requires Apache >= 2.4.11 SSLSessionTickets Off SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

ตัวอย่างข้างต้นใช้ chippers ที่ Cipherli.st แนะนำให้เปิดใช้งาน OCSP Stapling, HTTP Strict Transport Security (HSTS) และบังคับใช้ส่วนหัว HTTP HTTP ที่เน้นความปลอดภัยเล็กน้อย

ก่อนเปิดใช้งานไฟล์กำหนดค่าตรวจสอบให้แน่ใจว่าทั้ง mod_ssl และ mod_headers เปิดใช้งานโดยการออก:

sudo a2enmod ssl sudo a2enmod headers

ถัดไปเปิดใช้งานไฟล์คอนฟิกูเรชัน SSL โดยการรันคำสั่งต่อไปนี้:

sudo a2enconf letsencrypt sudo a2enconf ssl-params

เปิดใช้งานโมดูล HTTP / 2 ซึ่งจะทำให้ไซต์ของคุณเร็วขึ้นและมีประสิทธิภาพยิ่งขึ้น:

sudo a2enmod

โหลดการกำหนดค่า Apache ใหม่เพื่อให้การเปลี่ยนแปลงมีผล:

sudo systemctl reload apache2

ตอนนี้เราสามารถเรียกใช้เครื่องมือ Certbot ด้วยปลั๊กอิน webroot และรับไฟล์ใบรับรอง SSL โดยพิมพ์:

sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

หากได้รับใบรับรอง SSL เรียบร้อยแล้ว certbot จะพิมพ์ข้อความต่อไปนี้:

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2018-10-28. 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" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF:

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

/etc/apache2/sites-available/example.com.conf

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration

ด้วยการกำหนดค่าข้างต้นเรากำลังบังคับใช้ HTTPS และเปลี่ยนเส้นทางจาก www เป็นรุ่นที่ไม่ใช่ www ลดลงเพื่อปรับการกำหนดค่าตามความต้องการของคุณ

โหลดบริการ Apache อีกครั้งเพื่อให้การเปลี่ยนแปลงมีผล:

sudo systemctl reload apache2

ตอนนี้คุณสามารถเปิดเว็บไซต์ของคุณโดยใช้ https:// และคุณจะสังเกตเห็นไอคอนล็อคสีเขียว

ต่ออายุการเข้ารหัสโดยอัตโนมัติขอใบรับรอง SSL

ใบรับรองของ Encrypt นั้นมีอายุ 90 วัน หากต้องการต่ออายุใบรับรองโดยอัตโนมัติก่อนที่จะหมดอายุแพ็คเกจ certbot จะสร้าง cronjob ที่ทำงานสองครั้งต่อวันและต่ออายุใบรับรองใด ๆ โดยอัตโนมัติ 30 วันก่อนหมดอายุ

เมื่อต่ออายุใบรับรองแล้วเรายังต้องโหลดบริการ Apache อีกครั้ง ผนวก --renew-hook "systemctl reload apache2" ไปยังไฟล์ /etc/cron.d/certbot เพื่อให้มีลักษณะดังต่อไปนี้:

/etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload apache2"

ในการทดสอบกระบวนการต่ออายุคุณสามารถใช้สวิตช์ --dry-run :

sudo certbot renew --dry-run

หากไม่มีข้อผิดพลาดแสดงว่ากระบวนการต่ออายุสำเร็จ

ข้อสรุป

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

apache อูบุนตูลองเข้ารหัส certbot ssl

โพสต์นี้เป็นส่วนหนึ่งของซีรี่ส์ How-to-install-lamp-stack-on-ubuntu-18-04

โพสต์อื่น ๆ ในชุดนี้:

•วิธีการติดตั้ง Apache บน Ubuntu 18.04 •วิธีการตั้งค่า Apache โฮสต์เสมือนบน Ubuntu 18.04 •รักษาความปลอดภัย Apache ด้วย Let's Encrypt บน Ubuntu 18.04 •วิธีติดตั้ง MySQL บน Ubuntu 18.04 •วิธีติดตั้ง PHP บน Ubuntu 18.04