swetrix
nginx-letsencrypt

NGINX ve Let’s Encrypt kullanarak ücretsiz SSL Sertifikası oluşturmak ve otomatik yenilemek

Bu yazımda certbot ve nginx kullanarak ücretsiz SSL Sertifikaları üreteceğiz ve bu sertifikaları otomatik yenileyeceğiz. Ben Ubuntu 20.04 kullanacağım fakat Debian tabanlı bütün Linux dağıtımlarında uygulayabilirsiniz.

Gereksinimler

  • DNS Kayıtları doğru şekilde yapılandırılmış domain.
  • NGINX Web Server

Certbot ve NGINX plugin kurulumu

SSL sertifikası almak için Let’s Encrypt’i kullanmanın ilk adımı, sunucunuza Certbot yazılımını yüklemektir. Certbot’u ve NGINX pluginı kuralım:

sudo apt install certbot python3-certbot-nginx

NGINX yapılandırmaları kontrol et

Ben bu yazı için “dev.linuxpedi.com” domainini kullanacağım nginx’in varsayılan yapılandırma dosyasına sadece “server_name” değeri gireceğim. Örnek yapılandırma dosyası:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm;
    server_name dev.linuxpedi.com;
}

NGINX yapılandırma dosyasını kaydettikten sonra nginx ile test edelim:

nginx -t

Aşağıdaki çıktıyı benzer bir çıktı almanız gerekiyor.

root@linuxpedi:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

NGINX Restart et.

systemctl reload nginx.service

SSL Sertifikası oluşturmak

Serverımızın bütün yapılandırmalarını tamamladıktan sonra artık çok basit şekilde sertifika oluşturacağız ve certbot bizim için bu sertifikayı sürekli olarak yenileyecek.

Certbot ve nginx plugini kullanarak ssl sertifikasını oluşturalım.

certbot --nginx -d dev.linuxpedi.com #Kendi domaininize göre değiştirin

Bu komuttan sonra sizden ilk e-mail adresinizi ve sözleşme şartlarını kabul etmenizi isteyecek. Bu bölümde önemli olan bölüm ise http isteklerini https’e yönlendirmek isteyip istemediğinizi soracak.

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://dev.linuxpedi.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=dev.linuxpedi.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Özel bir nedeni yok ise kesinlikle yönlendirmenizi tavsiye ederim. Bu yüzden 2’yi seçtim. Gördüğünüz gibi hızlı bir şekilde sertifikamızı oluşturup gerekli bütün nginx ayarları yapıldı. Oluşturduğumuz nginx yapılandırma dosyanızı kontrol ederseniz certbot oraya ssl ile iligili bütün ayarları girmiş ve sertifika dizinlerini oluşturmuştur.

server {

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;
    server_name dev.linuxpedi.com; # managed by Certbot


	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/dev.linuxpedi.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/dev.linuxpedi.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = dev.linuxpedi.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

	listen 80 ;
	listen [::]:80 ;
    server_name dev.linuxpedi.com;
    return 404; # managed by Certbot
}

Otomatik olarak yenilemeyi kontrol etmek için certbot’un certbot.timer servisine bakmamız gerekiyor.

sudo systemctl status certbot.timer
root@linuxpedi:~# sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Sun 2022-02-13 23:19:34 UTC; 40min ago
    Trigger: Mon 2022-02-14 08:30:04 UTC; 8h left
   Triggers: ● certbot.service

Feb 13 23:19:34 linuxpedi systemd[1]: Started Run certbot twice daily.

İsterseniz certbot --dry-run parametresini kullanarak sertifikaların sorunsuz yenilendiğini test edebilirsiz.

sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/dev.linuxpedi.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for dev.linuxpedi.com
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/dev.linuxpedi.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/dev.linuxpedi.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Herhangi bir hata görmüyorsanız. Certbot, gerektiğinde sertifikalarınızı yenileyecek ve değişiklikleri yapmak için Nginx’i reload edecektir. Otomatik yenileme işlemi başarısız olursa, Let’s Encrypt belirttiğiniz e-postaya sertifikanızın süresi dolmak üzereyken sizi uyaran bir mail gönderir.

Serifikamızı https://www.ssllabs.com/ssltest test edelim.

ssl cert
Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Önceki Yazı
systemctl cover 1

Systemctl ile Linux Servislerini Listeleme

Sonraki Yazı
terraform-cover

Terraform Nedir ?

Benzer Yazılar