WP63
  • Web Development
  • WordPress Development
  • Plugins
  • WP63
  • Web Development

ตั้งค่า SSL สำหรับเว็บที่รันอยู่บน VVV

Published January 4, 2018

Updated March 7, 2019

ตั้งค่า SSL สำหรับเว็บที่รันอยู่บน VVV
Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

บทความเขียนขึ้นตอนที่ยังใช้ VVV 2.1.0 ที่ยังต้องตั้งค่าเพิ่มเติมเพื่อเปิดใช้ SSL แต่ใน VVV 2.5.1 ตัวล่าสุด ณ ตอนนี้รองรับการเปิดใช้ SSL ในตัวแล้ว สามารถเปิดเว็บด้วย https ได้ทันที

ต่อจากบทความที่แล้วที่เราพาไปรู้จักกับการติดตั้งและใช้งานเวิร์ดเพรสบน VVV อีกเรื่องหนึ่งที่เรายังติดค้างกันเอาไว้นั่นคือการตั้งค่า SSL เพื่อให้เว็บทดสอบของเราสามารถใช้งาน HTTPS ได้

  • ติดตั้ง VVV สำหรับพัฒนา WordPress บน Isolated Development Environment

ปัจจุบันบริการหลายๆ อย่าง  รวมถึงฟีเจอร์หลายตัวใน HTML5/ES8 นั้นจำเป็นต้องใช้งานผ่าน HTTPS เท่านั้น  อย่างเช่น Geolocation API ทำให้หาเราไม่มีการตั้งค่า HTTPS จะทำให้ไม่สามารถทดสอบฟีเจอร์เหล่านี้บน localhost ได้

ขั้นแรกให้เริ่มการทำงานของ VVV ก่อน  โดยเปิด Command Prompt/Terminal แล้ว cd เข้าไปยังโฟลเดอร์ VVV

cd C:\vvv
cd ~/vvv

จากนั้นสั่ง vagrant up เพื่อเริ่มการทำงาน

หลังจาก VVV เริ่มทำงานแล้ว  แล้วสั่ง vagrant ssh เพื่อเชื่อมต่อ SSH เข้าไปใน Virtual Machine

สร้าง SSL Certification ด้วย OpenSSL

เมื่อเชื่อมต่อ SSH เข้ามาแล้ว  ให้เปิดไปยังโฟลเดอร์ provision ของเว็บของเราที่เราตั้งไว้ (ถ้าจำกันได้  มันคือโฟลเดอร์ vm_dir ที่เรากำหนดไว้ใน vvv-custom.yml ซึ่งจากบทความที่แล้วคือ /srv/www/myweb ดังนั้นเราจะเข้าไปที่ /srv/www/myweb/provision)

cd /srv/www/myweb/provision

จากนั้นสร้างโฟลเดอร์ชื่อ ssl เพิ่มขึ้นมาอีกโฟลเดอร์หนึ่ง

mkdir ssl

และเปิดเข้าไปในโฟลเดอร์ดังกล่าว

cd ssl

ถึงตอนนี้เราจะสร้างไฟล์ SSL Certificate กัน  โดยใช้คำสั่งต่อไปนี้ (เปลี่ยน myweb.test เป็นโฮสต์เนมที่เราตั้งเอาไว้ด้วยนะ)

openssl req -newkey rsa:2048 -x509 -nodes -keyout myweb.test.key -new -out myweb.test.cert -subj /CN=myweb.test -reqexts SAN -extensions SAN -config <(cat /etc/ssl/openssl.cnf <(printf '[SAN]\nsubjectAltName=DNS:myweb.test')) -sha256 -days 3650

จากนั้นตอนนี้เราจะได้ไฟล์ออกมา 2 ไฟล์คือ myweb.test.cert และ myweb.test.key ก็ถือว่าการสร้าง Certificate นั้นเสร็จเรียบร้อย  เหลือเพียงขั้นตอนการติดตั้ง Certificate นี้ลงไป

ติดตั้ง SSL Certificate ให้กับเว็บไซต์

ในโฟลเดอร์เก็บเว็บของเรา  ให้เปิดไฟล์ provision/vvv-nginx.conf ขึ้นมา  จากนั้นเพิ่มคำสั่งเกี่ยวกับ SSL ต่อท้ายลงไปก่อนที่จะปิดวงเล็บปีกกา  และเปลี่ยนชื่อไฟล์ให้ตรงกับไฟล์ที่เราสร้างขึ้น (myweb.test.cert กับ myweb.test.key)

    ssl    on;
    ssl_certificate    {vvv_path_to_folder}/ssl/myweb.test.cert;
    ssl_certificate_key    {vvv_path_to_folder}/ssl/myweb.test.key;

และไฟล์ vvv-nginx.conf โดยรวมของเราจะมีหน้าตาแบบนี้

server {
	listen 80;
	listen 443 ssl;
	server_name myweb.test;
	root {vvv_path_to_site}/public_html;

	error_log {vvv_path_to_site}/log/error.log;
	access_log {vvv_path_to_site}/log/access.log;

	set $upstream {upstream};

	include /etc/nginx/nginx-wp-common.conf;

	ssl    on;
	ssl_certificate    {vvv_path_to_folder}/ssl/myweb.test.cert;
	ssl_certificate_key    {vvv_path_to_folder}/ssl/myweb.test.key;
}

ติดตั้ง SSL Certificate ลงบนเครื่องของผู้ใช้

โดยปกตินั้นในระบบปฏิบัติการจะมีการระบุเอาไว้ว่า SSL จากผู้ออกใบรับรองรายได้บ้างที่จะไว้ใจ (เช่น COMODO หรือ Verisign) และ SSL จากผู้ออกใบรับรองรายอื่นที่ไม่อยู่ในรายชื่อนี้ (แน่นอนว่ารวมถึงการ self-signed ที่เราเพิ่มทำไปเมื่อสักครู่นี้ด้วย) จะไม่ได้รับการไว้ใจ  และจะไม่เชื่อมต่อเว็บให้ในทันที (ขึ้น Privacy Error)

ในขั้นตอนนี้เราจะใช้ไฟล์ .cert ที่ได้จากการสร้าง SSL เมื่อสักครู่นี้

macOS

สำหรับผู้ใช้ macOS ให้เปิดโปรแกรม Keychain Access ขึ้นมา  จากนั้นในช่อง Keychains เลือกไปที่ System และช่อง Category เลือกไปที่ Certificates

ในขั้นตอนนี้หากปุ่มแม่กุญแจด้านบนขึ้นเป็นแม่กุญแจล็อก  ให้กดปุ่มแม่กุญแจแล้วกรอกรหัสผ่านเพื่อปลดล็อคการแก้ไข

จากนั้นลากไฟล์ .cert ที่เราสร้างขึ้นมาใส่ในหน้าต่างนี้  จากนั้นให้ดับเบิลคลิกใบรับรองที่เราเพิ่งลากใส่เข้ามา  ตรงหัวข้อ Trust ให้เปลี่ยน When using this certificate เป็น Always trust

Windows

สำหรับบน Windows ให้เปลี่ยนนามสกุลไฟล์จาก .cert เป็น .cer (ตามมาตรฐาน  นามสกุลไฟล์บน Windows จะมี 3 ตัวอักษร) จากนั้นคลิกขวาที่ไฟล์ .cer แล้วเลือก Install Certificate

หลังจากนั้นหน้าต่าง Certificate Import Wizard จะเปิดขึ้นมา  ให้เลือก Store Location เป็น Current User แล้วกด Next

ในหน้า Certificate Store ให้เลือก Place all certificates in the following store

จากนั้นกด Browse แล้วเลือก Trusted Root Certification Authority

กด OK ออกมาแล้วกด Next แล้วกด Finish แล้ว Windows จะถามอีกครั้งว่าจะติดตั้งใบรับรองหรือไม่  ให้กดตกลง  ก็ถือว่าเสร็จสิ้นการติดตั้งใบรับรองบนวินโดวส์

จากนั้นให้กลับมาสั่ง reload VVV ใหม่  เพื่อให้ VVV นำการตั้งค่าใหม่ไปใช้งาน

vagrant reload --provision

หลังจากติดตั้งแล้ว (ทั้งบนแมคและวินโดวส์) ผู้ใช้ต้องปิดและเปิดเบราเซอร์ (หรือ restart เครื่อง) รอบหนึ่งเพื่อให้ใบรับรองเข้าสู่ระบบ  จึงจะสามารถใช้งาน https ได้ตามปกติ

Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

บทความอื่นๆ ที่น่าสนใจ

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2022 WP63

  • หน้าแรก
  • ติดต่อเรา
  • นโยบายความเป็นส่วนตัว
  • ข้อตกลงการใช้งาน
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
  • Web Development
  • WordPress Development
  • Plugins

Like us

Like us

Categories

  • Blog
  • Gutenberg
  • Plugins
  • Shortnotes
  • Snippets
  • Web Development
  • WordPress Development

Popular Posts

  • ส่งอีเมลด้วย SendGrid
    ส่งอีเมลด้วย SendGrid
  • array_map() ใน PHP
    array_map() ใน PHP
  • กำหนด URL structure ได้ตามใจด้วย AltoRouter
    กำหนด URL structure ได้ตามใจด้วย AltoRouter
  • การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
    การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
  • การทำ Routing ใน PHP ด้วย AltoRouter
    การทำ Routing ใน PHP ด้วย AltoRouter
  • ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ
    ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ

Archives

  • April 2022
  • January 2021
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • March 2019
  • February 2019
  • December 2018
  • September 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018