Tạo SSL Certificate trên nginx cho CentOS 6

0

Chứng chỉ SSL (Secure Sockets Layer) là công nghệ mã hóa trao đổi dữ liệu giữa máy chủ web và máy tính cá nhân, đảm bảo việc trao đổi thông tin luôn luôn được bảo mật và an toàn, không thể bị khai thác và giải mã bởi đối tượng thứ ba. Ngoài ra SSL còn có nhiệm vụ như chứng thực Website, bảo mật FTP, Mail Service, VPN…. và rất nhiều ứng dụng khác.

Trong bài viết này, mình sẽ hướng dẫn các bạn tạo SSL Certificate trên nginx cho CentOS.

1. Tạo thư mục cho Certificate

sudo mkdir /etc/nginx/ssl

Di chuyển đến folder

cd /etc/nginx/ssl

2. Tạo Server Key và Certificate Signing Request

Tạo private server key. Chú ý ghi nhớ pass khi đăng ký nhé.

sudo openssl genrsa -des3 -out server.key 1024

Tạo certificate signing request

sudo openssl req -new -key server.key -out server.csr

Tiếp theo điền các thông tin như mẫu bên dưới. Chú ý cái Common name cần nhập chính xác domain của bạn hoặc IP nếu ko có. Phần challenge password và optional company name để trống.

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:VI
State or Province Name (full name) [Some-State]:Ha Noi
Locality Name (eg, city) []:HN
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Awesome Inc
Organizational Unit Name (eg, section) []:Dept of Merriment
Common Name (e.g. server FQDN or YOUR name) []:example.com                  
Email Address []:webmaster@awesomeinc.com

3. Xóa Passphrase vì thông tin này không cần thiết

sudo cp server.key server.key.org
sudo openssl rsa -in server.key.org -out server.key

4. Sign SSL Certificate

Mình sẽ đăng ký chứng chỉ hết hạn sau 1 năm nữa (365):

 sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

5. Cài đặt Certificate

Mở SSL config

nano /etc/nginx/conf.d/ssl.conf

Chỉnh lại phần dưới HTTPS Server như thông tin sau (chú ý thay example.com):

# HTTPS server

server {
    listen       443;
    server_name example.com;

    ssl on;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key; 
}

Khởi động lại nginx

 /etc/init.d/nginx restart

Truy cập vào https://youraddress, bạn sẽ thấy website hoạt động bình thường.

Leave a Reply

Your email address will not be published. Required fields are marked *