Ubuntu LinuxにNginx Free HTTPS証明書をインストールする

このチュートリアルでは、Ubuntu LinuxでNginxサーバーブロックを設定するために必要なすべての手順を説明します。

Nginx Server BlocksはApache仮想ホスト機能とほとんど同じです。

このチュートリアルはUbuntu 18.04でテストされました。

1. Ubuntu LinuxにNginxをインストールする

Nginxサーバーをインストールするには、Ubuntu APTコマンドを使用します。

# apt-get update
# apt-get install nginx

Nginx Webサーバーを手動で再起動します。

# service nginx restart
# service nginx status

Nginxサービスの状態を確認してください。

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-12-29 04:29:22 UTC; 1h 17min ago
Docs: man:nginx(8)
Process: 2233 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status
Process: 2221 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exite
Main PID: 2238 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─2238 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2239 nginx: worker process

Ubuntu LinuxへのNginx Webサーバーのインストールは完了です。

2. NginxにPHPサポートを追加する

NginxはPHPサポートを追加するために外部プログラムを必要とします。

Ubuntu APTコマンドを使用してPHPの必須パッケージをインストールします。

# apt-get update
# apt-get install php7.2-fpm

オプションです。 最も使用されているPHPモジュールをインストールするには、次のコマンドを使用します。

# apt-get install php7.2-xml php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql
# apt-get install php7.2-bz2 php7.2-zip php7.2-json php7.2-readline

システム上のPHP設定ファイルの場所を見つけます。

php.ini設定ファイルを編集します。

# updatedb
# locate php.ini
# vi /etc/php/7.2/fpm/php.ini

あなたのPHPのバージョンは私たちのものと同じではないかもしれません。

あなたのPHP設定ファイルの場所は、私たちのものと同じではないかもしれません。

これが私たちの設定ファイルです。

file_uploads = On
max_execution_time = 300
memory_limit = 256M
post_max_size = 32M
max_input_time = 60
max_input_vars = 4440
upload_max_filesize = 32M

NginxのデフォルトWebサイト設定ファイルを編集します。

# vi /etc/nginx/sites-available/default

これが私たちの設定の前の、元のファイルです。

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

これが私たちの設定による新しいファイルです。

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

Nginxの設定ファイルにエラーがないか確認してください。

# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

PHPサービスを再起動します。
Nginxサービスを再起動します。

# service php7.2-fpm restart
# service nginx restart

Ubuntu Linux上でNginxとPHPの統合は完了です。

3. Nginxサーバーブロックを設定する

サーバーブロックは、1台のNginxサーバーが同じIPアドレスを使用して複数のWebサイトを提供できるようにする機能です。

Nginx Serverのブロック機能を使用するために必要なインフラストラクチャを作成しましょう。

# mkdir /websites/mining-pool -p
# cd /websites/mining-pool
# mkdir www
# chown www-data.www-data /websites -R

私たちのウェブサイトはmining-pool.ninjaという名前になります。

mining-pool.ninja Webサイトファイルは、/ website / mining-pool / wwwディレクトリ内にあります。

Alert!

Mining-pool.ninja was used as an example!

You need to change your configuration files to reflect your website name.

あなたのウェブサイトにNginx Virtualhost設定ファイルを作成してください。

# vi /etc/nginx/sites-available/mining-pool.conf

これが私たちの設定ファイルです。

server {
listen 80;
listen [::]:80;
root /websites/mining-pool/www;
index index.php index.html index.htm;
server_name mining-pool.ninja;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

Nginx仮想ホスト構成を有効にするためのシンボリックリンクを作成します。

Nginxサービスを再起動します。

# ln -s /etc/nginx/sites-available/mining-pool.conf /etc/nginx/sites-enabled/
# service nginx restart

これでNginx Serverのブロック構成は完了です。

4. DNSドメインを設定する

にアクセスする GODADDY DNSドメインを購入する。

この例では、MINING-POOL.NINJAというドメインを購入します。

あなたはDNSドメインを購入するために任意のウェブサイトを使用することができます、GoDaddyはちょうど私の個人的な選択です。

あなたのウェブサイトがNginxを実行しているコンピュータを指すDNSエントリを作成します。

この例では、ドメインMINING-POOL.NINJAを指すDNSエントリをIPアドレス35.163.100.49に作成しました。

Godaddy Apache DNS Configuration

DNS設定をテストするには、NSLOOKUPコマンドを使用します。

# apt-get update
# apt-get install dnsutils
# nslookup mining-pool.ninja

Non-authoritative answer:
Name: mining-pool.ninja
Address: 35.163.100.49

DNSドメインの設定が完了しました。

設定をテストするために、基本的なPHPテストページを作成しましょう。

# vi /websites/mining-pool/www/index.php

これはindex.phpファイルの内容です。

<?php phpinfo(); ?>

ブラウザを開き、WebサイトのHTTPバージョンにアクセスしてみます。

この例では、ブラウザに次のURLが入力されています。

•http://mining-pool.ninja

PHP情報ページが表示されるはずです。

PHPInfo Nginx

5. Nginxで無料のHTTPS証明書を設定する

Ubuntu LinuxでLET'S ENCRYPT無料SSL / TLS証明書を使用するために必要なパッケージをインストールします。

# apt-get install software-properties-common
# add-apt-repository universe
# add-apt-repository ppa:certbot/certbot
# apt-get update
# apt-get install python-certbot-nginx

Nginxの無料HTTPS証明書を要求してインストールします。

certbot --nginx -d mining-pool.ninja

•(A)を押して利用規約に同意します。
•電子メールを共有してニュースレターを受信するには、(Y)を押します。
•(2)を押すと、HTTP Webサイトが自動的にHTTPSバージョンにリダイレクトされます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): techexpert.tips@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 mining-pool.ninja
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/mining-pool.conf

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/mining-pool.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://mining-pool.ninja

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

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mining-pool.ninja/privkey.pem
Your cert will expire on 2019-03-31. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. 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

システムは自動的に無料の証明書を要求します。

また、すべてのHTTPアクセスをHTTPSバージョンのWebサイトにリダイレクトするようにNginx Webサーバーを設定します。

この例では、システムはmining-pool.confという名前の仮想ホスト設定ファイルを変更しました。

これは、変更されたファイルmining-pool.confの内容です。

server {
root /websites/mining-pool/www;
index index.php index.html index.htm;
server_name mining-pool.ninja;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mining-pool.ninja/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mining-pool.ninja/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 = mining-pool.ninja) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
listen [::]:80;
server_name mining-pool.ninja;
return 404; # managed by Certbot
}

KEYファイルには証明書の秘密鍵が含まれており、常に安全な場所に保管する必要があります。

mining-pool.ninjaへの鍵ファイルは/etc/letsencrypt/live/mining-pool.ninja/privkey.pemに保存されていました。

6. Nginxで無料のHTTPS証明書をテストする

必要な設定はすべて完了しました。

設定をテストする時が来ました。

ブラウザを開き、WebサイトのHTTPバージョンにアクセスしてみます。

この例では、ブラウザに次のURLが入力されています。

•http://mining-pool.ninja

Nginxは自動的にHTTPリクエストをあなたのウェブサイトのHTTPSバージョンにリダイレクトします。

Lets Encrypt Nginx HTTPS

これでNginx HTTPSの無料証明書の設定は完了です。

7.無料のHTTPS証明書を更新する方法

LET'S ENCRYPT無料SSL / TLS証明書は90日間有効です。

システムは、有効期限が切れてから30日以内に証明書を自動的に更新するようにスケジュールされたタスクを作成します。

スケジュールされたタスク名はcertbotで、ディレクトリ/etc/cron.dの中にあります。

これは/etc/cron.d/certbotファイルの内容です。

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc. Renewal will only occur if expiration
# is within 30 days.
#
# Important Note! This cronjob will NOT be executed if you are
# running systemd as your init system. If you are running systemd,
# the cronjob.timer function takes precedence over this cronjob. For
# more details, see the systemd.timer manpage, or use systemctl show
# certbot.timer.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

証明書の更新プロセスをシミュレートするには、次のコマンドを使用します。

# certbot renew --dry-run

次のようなメッセージが表示されます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mining-pool.ninja.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 mining-pool.ninja
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/mining-pool.ninja/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/mining-pool.ninja/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -