0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UbuntuでSSL対応

Posted at

前提

  • DNSが設定されていることが前提。
    ドメインを取得したサイトで、UbuntuサーバーのIPアドレスと設定したいドメインを紐づけておく。
  • ドキュメントルートは/var/www/htmlで、ここにindex.htmlなどサイトデータ一式が格納されている状態とする。

Apache2のインストール

Ubuntuではhttpdではなくapache2の名前で配布されている。

sudo apt-get install -y apache2 #インストール
sudo systemctl start apache2 #起動

confファイルの設定

/etc/apache2/sites-enabled/にApacheの設定ファイル000-default.confがあるので、そこにドメインを設定しておく。
(元データは000-default.conf.oldのように名前をつけてバックアップしておく)

sudo vi /etc/apache2/sites-enabled/000-default.conf
000-default.conf
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName ドメイン名を記載

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # 略
</VirtualHost>

80番・443番ポートを開放

HTTPプロトコル(80番)、HTTPSプロトコル(443番)を受け付けるように、該当のポートを開放

firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent
firewall-cmd --reload

この時点で、HTTPプロトコルによるアクセスは可能に。

Certbot のインストール

sudo apt install certbot python3-certbot-apache

Apache 用の SSL 証明書を取得

sudo certbot --apache
  1. 使用するドメインを番号で選択
  2. HTTP → HTTPS リダイレクト設定をどうするか設定

証明書が無事取得されると、下記のディレクトリにそれぞれ証明書・秘密鍵のファイルが保存される。

  • 証明書本体: /etc/letsencrypt/live/ドメイン名/fullchain.pem
  • 秘密鍵: /etc/letsencrypt/live/ドメイン名/privkey.pem

SSLモジュールのインストール

sudo apt-get install -y mod_ssl #インストール
sudo a2enmod ssl #有効化

mod_sslがインストールされると、/etc/apache2/sites-availableに設定ファイルdefault-ssl.confが作成されるので、vimなどでこのファイルを開いて先程の証明書と秘密鍵のパスを書き込む。
(元データはdefault-ssl.conf.oldのように名前をつけてバックアップしておく)

sudo vi /etc/apache2/sites-available/default-ssl.conf
default-ssl.conf
<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    SSLEngine on
    # 下記はテスト用の証明書・秘密鍵なのでコメントアウト
    # SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    # SSLCertificateKeyFile   /etc/ssl/private/ssl-cert-snakeoil.key

    # 先程取得したCertbotの証明書・秘密鍵のパスを追加
    SSLCertificateFile      /etc/letsencrypt/live/ドメイン名/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/ドメイン名/privkey.pem

  </VirtualHost>
</IfModule>

SSLの有効化・Apache2の再起動

sudo a2ensite default-ssl
sudo systemctl reload apache2

Apacheを再起動したら、https:://ドメイン名にアクセスして、HTTPSプロトコルでアクセスできるか確認。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?