your-service.japaneast.cloudapp.azure.com을 certbot과 nginx로 부담없이 SSL화

9621 단어 Azurenginxletsencrypt
의외로 어색한 것을 쓰고 있는 Qiita 기사가 없었기 때문에, 메모해 둔다.
익숙해지면 2~3분 있으면 가능할 것 같다.

전제



①Debian 머신이 Virtual Machine에서 움직이고 있다.
 (Ubuntu에서도 CentOS에서도 아마 절차는 그렇게 변하지 않는다. 데비안인 것은 개인적인 취향)

②TCP 80번 포트와 443번 포트는 외부에서 액세스 가능


③your-service.japaneast.cloudapp.azure.com DNS 이름 라벨을 취득하고 있다


certbot에서 SSL 인증서를 받습니다.



certbot 설치



h tps : // / rt 보트 t. 엣 f. 오 rg / A l-in St ru c chion s에는 사세하게 설명이 있기 때문에, 그것에 따른다.
$ echo "deb http://deb.debian.org/debian stretch-backports main" | sudo tee /etc/apt/sources.list.d/certbot.list
$ sudo apt-get update && sudo apt-get install certbot

certbot에서 SSL 인증서 발급


certbot certonly --manual --manual-public-ip-logging-ok -d YOUR-DOMAIN.japaneast.cloudapp.azure.com -m まともなメールアドレス 를 root 실행한다.
$ sudo certbot certonly --manual --manual-public-ip-logging-ok -d certbot-playground.japaneast.cloudapp.azure.com -m [email protected]

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for certbot-playground.japaneast.cloudapp.azure.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:

SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E.au7pE6foVeeQ8XwYgWII-iA_LKiTAudzEYYfKZLaA4w

And make it available on your web server at this URL:

http://certbot-playground.japaneast.cloudapp.azure.com/.well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue


여기서, 「정말 당신의 서버군요? 그것을 증명하기 위해서, …

다른 창에서 SSH로 들어가,
$ mkdir -p .well-known/acme-challenge/
$ echo 'SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E.au7pE6foVeeQ8XwYgWII-iA_LKiTAudzEYYfKZLaA4w' > .well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E
$ sudo python3 -m http.server 80
Serving HTTP on 0.0.0.0 port 80 ...

이런 상태로 Python으로 HTTP 서버를 세우면 좋을 뿐. (Python3은 certbot을 넣을 때 마음대로 들어간다)

그래서, Press Enter to Continue 의 윈도우 쪽으로 Enter 키를 두드리면・・・
$ sudo python3 -m http.server 80
Serving HTTP on 0.0.0.0 port 80 ...
34.222.229.130 - - [08/Sep/2019 13:01:39] "GET /.well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E HTTP/1.1" 200 -
3.14.255.131 - - [08/Sep/2019 13:01:39] "GET /.well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E HTTP/1.1" 200 -
64.78.149.164 - - [08/Sep/2019 13:01:39] "GET /.well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E HTTP/1.1" 200 -
18.197.227.110 - - [08/Sep/2019 13:01:40] "GET /.well-known/acme-challenge/SVxxY7APvlHPcNkBeGZA5VdrwEg1eBhNABVRkh2Go5E HTTP/1.1" 200 -

4 발 정도 HTTP 통신이 오고,
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/privkey.pem
   Your cert will expire on 2019-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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


눈에 띄는 증명서를 받을 수 있다.
  • /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/fullchain.pem
  • /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/privkey.pem

  • 인증서를 받으면 Python의 HTTP 서버를 삭제해도 괜찮습니다.

    nginx에서 SSL 오프로드



    localhost : 8080 라든지에서 HTTP 서버가 일어나고있는 서비스에 밖에서 SSL로 넣고 싶은 같은 용도라면 nginx를 전단에 걸리는 것이 가장 빠르다.

    nginx 설치


    $ sudo apt-get install nginx
    

    그렇다면 오래된 버전의 nginx가 들어간다. 낡아도 아마 움직이는 것이겠지만, nginx.conf의 디폴트가 상당히 복잡하고 알기 어려우므로, h tp : // 근진 x. rg/엔/ぃぬ x_ぱcかげ s. HTML # 데비안 에 따라 최신의 것을 넣자.
    $ sudo apt install curl gnupg2 ca-certificates lsb-release
    $ echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
    $ curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
    OK
    $ sudo apt update && sudo apt install nginx
    

    nginx.conf 확인


    $ sudo cat /etc/nginx/nginx.conf 
    
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    
    $ sudo cat /etc/nginx/conf.d/default.conf 
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    

    이런 간지의, 알기 쉬운 nginx.conf가 있다. 이번에는 conf.d/default를 다시 씁니다.

    nginx/conf.d/default.conf 재작성


    server {
        listen      80;
    
        return 301 https://$host$request_uri;
    }
    server {
        listen              443 ssl;
        server_name         certbot-playground.japaneast.cloudapp.azure.com;
        ssl_certificate     /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/certbot-playground.japaneast.cloudapp.azure.com/privkey.pem;  
    
        location / {
            proxy_pass http://localhost:8080;
        }
    }
    

    그래서, sudo service nginx restart 한다.

    동작 확인.



    여기까지 오면,

    브라우저에서 certbot-playground.japaneast.cloudapp.azure.com으로 이동하면,



    \(^o^)/

    좋은 웹페이지 즐겨찾기