Hướng dẫn 2 cách tạo SSL trên localhost cho Ubuntu 22.04 hoặc 20.04 LTS chạy Nginx hoặc Apache giúp bạn chuyển đổi từ http sang https giống như trên server.
2 Cách tạo SSL trên Localhost Ubuntu 22.04 | 20.04 LTS
1. Tạo SSL certificate và cấu hình file hosts
Để chạy được SSL hay https trên localhost trước hết bạn cần phải có 1 domain (tên miền). Localhost thì bạn có thể tự tạo 1 domain bất kỳ rồi cấu hình nó trong /etc/hosts. Hãy mở file bằng nano và thêm 127.0.0.1 domain.com vào cuối file sau đó lưu lại. Ví dụ ở đây itsmeit sẽ tạo SSL certificate với domain dev.itsmeit.co.
sudo nano /etc/hosts

Khi bạn gán domain dev.itsmeit.co cho ip localhost là 127.0.0.1 thì bạn sẽ chạy trực tiếp website với domain dev.itsmeit.co thay vì http://localhost/project hoặc http://127.0.0.1/project.
Tiếp theo, chạy câu lệnh sau trên terminal để tạo 2 file ssl certificate có đuôi .key
và .crt
, nhớ thay đổi dev.itsmeit.co thành tên domain của bạn.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/dev.itsmeit.co.key -out /etc/ssl/certs/dev.itsmeit.co.crt

2. Cách cấu hình SSL certificate localhost với Apache2
Sau khi đã tạo ssl trên localhost để chạy tên miền với https (SSL), bạn cần thêm port 443 trong file config domain apache của bạn. Ví dụ như file config apache2 itsmeit cấu hình wordpress cho domain là : /etc/apache2/sites-available/dev.itsmeit.co.conf.
<VirtualHost *:80> ServerName dev.itsmeit.co ServerAlias www.dev.itsmeit.co Redirect permanent / https://dev.itsmeit.co/ </VirtualHost> <VirtualHost *:443> ServerName dev.itsmeit.co ServerAlias www.dev.itsmeit.co DocumentRoot /var/www/vhosts/dev.itsmeit.co/httpdocs Protocols h2 http:/1.1 <If "%{HTTP_HOST} == 'www.dev.itsmeit.co'"> Redirect permanent / https://dev.itsmeit.co/ </If> ErrorLog ${APACHE_LOG_DIR}/dev.itsmeit.co-error.log CustomLog ${APACHE_LOG_DIR}/dev.itsmeit.co-access.log combined SSLEngine On SSLCertificateFile /etc/ssl/certs/dev.itsmeit.co.crt SSLCertificateKeyFile /etc/ssl/private/dev.itsmeit.co.key SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 SSLProtocol All -TLSv1.2 -TLSv1.3 SSLCompression off #SSLUseStapling on Header always set Strict-Transport-Security "max-age=63072000" <Directory /var/www/vhosts/dev.itsmeit.co/httpdocs/> Options FollowSymlinks AllowOverride All Require all granted </Directory> </VirtualHost>
Sửa lại tên miền, đường dẫn đến source code, đường dẫn SSL và lưu lại file. Tiếp tục, để cấu hình trên hoạt động bạn cần enable/bật module SSL lên.
sudo a2enmod ssl
Bước tiếp sau khi tạo ssl trên localhost Ubuntu 22.04 | 20.04 là cấu hình Global
ServerName:
Mở file apache2.conf và thêm ServerName
127.0.0.1 vào cuối file.
sudo nano /etc/apache2/apache2.conf
. . . # Include the virtual host configurations: IncludeOptional sites-enabled/*.conf # vim: syntax=apache ts=4 sw=4 sts=4 sr noet ServerName 127.0.0.1
Cuối cùng kiểm tra lại cú pháp cấu hình file, nếu OK thì restart lại apache2 là được. Nếu có thông báo lỗi thì chạy lệnh sudo apachectl configtest
để xác định lỗi.
sudo service apache2 restart
3. Hướng dẫn cấu hình SSL certificate localhost với Nginx
Cũng tương tự như với apache, sau khi bạn config /etc/hosts
và tạo ssl trên localhost với 2 file có đuôi có đuôi .key
và .crt
bạn chỉ cần thêm config cho port 443 để chạy https. Ví dụ với config wordpress:
server { listen 443 ssl http2; root /var/www/vhosts/dev.itsmeit.co/httpdocs; index index.html index.htm; server_name dev.itsmeit.co; access_log /var/log/nginx/dev.itsmeit.co.vhost.log; error_log /var/log/nginx/dev.itsmeit.co.vhost.err; ssl_certificate /etc/ssl/certs/dev.itsmeit.co.crt; ssl_certificate_key /etc/ssl/private/dev.itsmeit.co.key; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384"; ssl_ecdh_curve secp384r1; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s; location / { try_files $uri $uri/ =404; } }
Giải thích
- root là đường dẫn đến thư mục chạy website
- server_name là domain trang web
- listen 80 là cổng mặc định (*:80) khi cài nginx
- listen 443 ssl http2 là cấu hình port chạy SSL (https)
- ssl_certificate và ssl_certificate_key là khóa key file xác thực
Hãy kiểm tra lại cấu hình và lưu file, reset lại nginx nếu không có lỗi gì là thành công. Nếu có lỗi thì chạy lệnh sudo nginx -t để xác định và sửa lỗi.
Cài đặt SSL trên localhost qua hướng dẫn trên sẽ chạy được domain với https, tuy nhiên SSL localhost không được xác thực cho nên bạn cần xác nhận thủ công như hình dưới nhé. Tạo SSL trên localhost Ubuntu 22.04 | 20.04 phục vụ cho việc test các plugin cần kết nối API ví dụ như facebook, google…

Trên đây là 2 cách hướng dẫn tạo SSL trên Localhost cho Ubuntu 22.04 hoặc 20.04 LTS chạy Nginx hoặc Apache giúp bạn chuyển đổi từ http sang https giống như trên Server.
– Advertising –