LoginSignup
2
3

More than 5 years have passed since last update.

Nginxで簡単 Lets Encrypto !!な手順

Last updated at Posted at 2018-06-24

CentOs7 の場合 firewall設定でhttpsを開ける

現状の確認は、以下のコマンド。

$ firewall-cmd --list-services --zone=public --permanent

https追加方法は以下のコマンド。

$ firewall-cmd --add-service=https --permanent
$ firewall-cmd --reload

まず http でアクセスできるようnginxの設定をする

server {
    listen       80;
    server_name  {domain};
    root         /var/www/html/{application};

    location / {
    }

    location ~* \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/{application}$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_read_timeout 180;
    }

    error_page 404 /404.html;
        location = /40x.html {
     }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

必要であれば、nginxの設定ファイルのシンボリックリンクを張る

もし、1つのサーバーで複数サイトを運用している場合は作成した conf のシンボリックリンクをはります。

ln -s /etc/nginx/conf.d/sites-available/{service_name}.conf /etc/nginx/conf.d/sites-enable/

nginxをリスタートしてアクセス可能かチェック

$ service nginx restart

let's encrypt

certbotをインストール

$ cd ~
$ git clone https://github.com/certbot/certbot
$ cd certbot

証明書を発行

$ ./certbot-auto certonly

以下のように聞かれるんで、4を選択。

How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Apache Web Server plugin - Beta (apache)
2: Nginx Web Server plugin - Alpha (nginx)
3: Spin up a temporary webserver (standalone)
4: Place files in webroot directory (webroot)
-------------------------------------------------------------------------------
Select the appropriate number [1-4] then [enter] (press 'c' to cancel): 

次の質問はドメインについて。
対応させたいドメインを入力。

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel):

入力したらドメインのドキュメントルートを聞かれるので、入力する。

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for {ドメイン}
Input the webroot for {ドメイン}:

これで

/etc/letsencrypt/live/{ドメイン}/fullchain.pem
/etc/letsencrypt/live/{ドメイン}/privkey.pem

がつくられる。

httpsでアクセスできるよに nginx 設定を更新

server {
    listen       80;
    server_name  {domain};
    return 301 https://$host$request_uri;
}

server {
    listen       443;
    server_name  {domain};
    root         /var/www/html/{application};
    access_log   /var/log/nginx/{application}.access.log  main;

    ssl_certificate      /etc/letsencrypt/live/{domain}/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/{domain}/privkey.pem;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires 15d;
        log_not_found off;
    }

    location / {
    }

    location ~* \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/{application}$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_read_timeout 180;
    }

    error_page 404 /404.html;
        location = /40x.html {
     }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

nginxをリスタートする

$ service nginx restart
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3