Android

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

पृथà¥?वी पर सà¥?थित à¤à¤¯à¤¾à¤¨à¤• नरक मंदिर | Amazing H

पृथà¥?वी पर सà¥?थित à¤à¤¯à¤¾à¤¨à¤• नरक मंदिर | Amazing H

สารบัญ:

Anonim

Let's Encrypt เป็นหน่วยงานออกใบรับรองอัตโนมัติและเปิดฟรีที่พัฒนาโดย Internet Security Research Group (ISRG) ใบรับรองที่ออกโดย Let's Encrypt นั้นมีอายุ 90 วันนับจากวันที่ออกและได้รับความเชื่อถือจากเบราว์เซอร์หลักทุกวันนี้

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

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

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

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

ติดตั้งแพ็คเกจต่อไปนี้ซึ่งจำเป็นสำหรับเว็บเซิร์ฟเวอร์ที่เข้ารหัส SSL:

yum install mod_ssl openssl

ติดตั้ง Certbot

Certbot เป็นเครื่องมือที่ช่วยให้กระบวนการรับใบรับรอง SSL จาก Let's Encrypt และ HTTPS ที่เปิดใช้งานอัตโนมัติบนเซิร์ฟเวอร์ของคุณ

แพคเกจ certbot นั้นสามารถใช้ได้กับการติดตั้งจาก EPEL หากที่เก็บ EPEL ไม่ถูกติดตั้งบนระบบของคุณคุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้:

sudo yum install epel-release

เมื่อเปิดใช้งานที่เก็บ EPEL แล้วให้ติดตั้งแพคเกจ certbot โดยพิมพ์:

sudo yum 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 apache /var/lib/letsencrypt sudo chmod g+s /var/lib/letsencrypt

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

/etc/httpd/conf.d/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/httpd/conf.d/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

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

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

sudo systemctl reload

ตอนนี้เราสามารถเรียกใช้เครื่องมือ 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-12-07. 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:

CentOS 7 มาพร้อมกับ Apache เวอร์ชั่น 2.4.6 ซึ่งไม่รวมถึงคำสั่ง SSLOpenSSLConfCmd คำสั่งนี้ใช้ได้เฉพาะใน Apache 2.4.8 ในภายหลังและใช้สำหรับการกำหนดค่าพารามิเตอร์ OpenSSL เช่นการแลกเปลี่ยนคีย์ Diffie – Hellman (DH)

เราจะต้องสร้างไฟล์รวมใหม่โดยใช้ใบรับรอง Let's Encrypt SSL และไฟล์ DH ที่สร้างขึ้น หากต้องการทำสิ่งนี้ให้พิมพ์:

cat /etc/letsencrypt/live/example.com/cert.pem /etc/ssl/certs/dhparam.pem >/etc/letsencrypt/live/example.com/cert.dh.pem

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

/etc/httpd/conf.d/example.com.conf

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration

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

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

sudo systemctl restart

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

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

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

รันคำสั่ง crontab เพื่อสร้าง cronjob ใหม่ซึ่งจะต่ออายุใบรับรองสร้างไฟล์รวมใหม่รวมถึงคีย์ DH และรีสตาร์ท apache:

sudo crontab -e

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

บันทึกและปิดไฟล์

เพื่อทดสอบกระบวนการต่ออายุคุณสามารถใช้คำสั่ง certbot ตามด้วย --dry-run :

sudo certbot renew --dry-run

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

ข้อสรุป

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

apache centos ลองเข้ารหัส certbot ssl

โพสต์นี้เป็นส่วนหนึ่งของชุดติดตั้ง LAMP Stack บน CentOS 7 ซีรี่ย์

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

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