ApacheのWebサイトを無料のHTTP証明書を暗号化して安全に保護する方法を学びたいですか。 このチュートリアルでは、Ubuntu Linux上でLet's Encryptを使ってApacheをセキュアにするために必要なすべてのステップを紹介します。

•Ubuntuのバージョン:18.04

インストールを説明するために、MINING-POOL.NINJAという名前のドメインを使用します。

設定ファイルを変更し、ドメイン名MINING-POOL.NINJAをあなたのドメイン名に置き換える必要があります。

ハードウェアリスト:

次のセクションでは、このWordpressチュートリアルを作成するために使用された機器のリストを示します。

上記のすべてのハードウェアは、AmazonのWebサイトにあります。

Apache関連のチュートリアル:

このページでは、Apacheに関連したチュートリアルのリストにすばやくアクセスできます。

1.ドメイン名を購入する

まず、あなたがアクセスする必要があります GODADDY そしてアカウントを作成します。

このアカウントはDNSドメインを購入するために使用されます。

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

DNSドメインを購入するために任意のWebサイトを使用してください。

GoDaddyは私のお気に入りの選択肢です。

2. DNSエントリを設定します

今、あなたはあなたのApacheウェブサーバを指すDNSレジストリを設定する必要があります。

DNSドメインを購入した後、あなたはUbuntu Linuxを実行しているコンピュータにあなたのウェブサイトアドレスを指すDNSエントリを作成する必要があります。

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

HTTPS DNS Entry

以下のコマンドを使用して、Linuxを実行しているコンピューターでDNS構成をテストします。

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

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

上記のコマンドは、ドメインがIPアドレス35.163.100.49を指していることを確認しました。

3. Apache Webサーバーをインストールする

Let's Encrypt Certification認証局では、ユーザーは完全に機能するApache Webサーバーを所有している必要があります。

Apache Webサーバーと必要なすべてのソフトウェアをインストールする必要があります。

Linuxコンソールで、以下のコマンドを使用して必要なパッケージをインストールします。

# apt-get update
# apt-get install apache2 php7.2 php7.2-mysql libapache2-mod-php7.2
# apt-get install openssl

Apacheのmod_rewriteを有効にして、apache2.confファイルを編集します。

# a2enmod rewrite
# vi /etc/apache2/apache2.conf

ファイルの末尾に次の行を追加します。

<Directory /var/www/html>
AllowOverride All
</Directory>

WebサイトにApache Virtualhost設定ファイルを作成します。

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

あなたのVirtualhostのファイル名と場所は私のと同じではないかもしれないことを覚えておいてください。

例として、これが私たちのファイルの内容です。

<VirtualHost *:80>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error
</VirtualHost>

必要なフォルダのリストを作成してください。

すべてのフォルダに正しいファイル権限を設定してください。

あなたのフォルダの場所は私のと同じではないかもしれないことを覚えておいてください。

# mkdir /websites
# cd /websites
# mkdir www logs
# chown www-data.www-data /websites -R

Apache Virtualhost設定を有効にして、Apacheサービスを再起動します。

# a2ensite mining-pool.conf
# systemctl reload apache2

4. Apacheで無料のHTTPS証明書を設定する

Linuxコンソールで、次のコマンドを使用して必要なパッケージをインストールします。

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

Certbotスクリプトを使用するには、ユーザーは完全に機能するApache Webサーバーを持っている必要があります。

私たちはすでにApache Virtualhostのインストールと設定を終えました。

次のコマンド要求を使用して、Apacheの無料HTTPS証明書をインストールします。

あなたのドメイン名は私のものと同じではいけません。

# certbot --apache -d mining-pool.ninja

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

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
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
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/mining-pool-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/mining-pool-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/mining-pool-le-ssl.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
Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/mining-pool.conf to ssl vhost in /etc/apache2/sites-available/mining-pool-le-ssl.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-19. 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"
- 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サイトにリダイレクトするようにApache Webサーバーを設定します。

この例では、システムはmining-pool-le-ssl.confという名前のHTTPS仮想ホスト構成ファイルを作成しました。

HTTPS仮想ホスト構成ファイルmining-pool-le-ssl.confの内容は次のとおりです。

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error

SSLCertificateFile /etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mining-pool.ninja/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

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

KEYファイルの内容は次のとおりです。/etc/letsencrypt/live/mining-pool.ninja/privkey.pem

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwGYFx4kcQmYC5
gJftnjZhWNVZkIL7wSMTFtFfIY37PNbVNeJ11HoudX8HWout8qXOxBvHE8oUtzrO
/956cCVNsdY+eXZVVwUhjeSRT9wH2vUh3vVw6mi0TRy1KZJGo+hFeUS7zGb0/IwV
MofsEE3rAM1s9RoNOzH6Q/S7OiXFH4OhjbORcv0mBBDjbUg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UgK2nsdO2eQCXkJDhpAyPEIjK8RWu/10oaS4k4guXYW
IYut3+dZpCFAVVCMwvaSlSMZbkk4464VBM3zxgRvxyoW1DXuO2ihWiD/fNZ8opTf
l7EcqR2fjOFKhgUF7qaZYG4GDYxHVuNhSkzDsdvHAoGBAIfDk7fLsG/BYROxJpzN
b1CoG8DE+DKYTWwMWW45rDF0ImbC6H3fg876CeZ3YKGWqt9qkha7gnGGVRRufP15
O7ZEXebZXeDYafXSNcnqSe7FSNtVr1eFlTWmL8OiWQuaqxjs/VogdWfhkEGPISVF
AOM3I6XSGx4NxW2RgYmvQZ4O
-----END PRIVATE KEY-----

システムは、すべてのHTTP要求をHTTPSバージョンにリダイレクトするように、元のApache Virtualhost設定ファイルを自動的に変更しました。

更新されたオリジナルのHTTP仮想ホスト設定ファイルmining-pool.confの内容は次のとおりです。

<VirtualHost *:80>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error
RewriteEngine on
RewriteCond %{SERVER_NAME} =mining-pool.ninja
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>

5. Apache HTTPS Webサイトをテストします。

インストールをテストするには、ブラウザを開いてWebサイトのHTTPアドレスを入力します。

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

•http://mining-pool.ninja

HTTPバージョンの代わりに、WebサイトのHTTPSバージョンを提示する必要があります。

Lets Encrypt Apache

おめでとうございます、あなたのApache設定はLET'S ENCRYPTの無料HTTPS証明書を使用しています。

6. Apache HTTPS証明書を更新する

Let's Encryptの証明書は90日間有効であることに注意する必要があります。

Apache HTTPS Free Certificate

システムは、有効期限が切れてから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 apache, Installer apache
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 apache 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.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- 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.