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?

ubuntu20.04Virtual Host設定及びCertbotで無料で SSL 証明書を発行

Posted at

Apacheのインストール

apt update
apt install apache2

まずはVirtual Host設定

cd /var/www/
mkdir example
cd /etc/apache2/sites-available
vi www.example.com.conf
下記のように編集保存。
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
</VirtualHost>

設定ファイルwww.example.com.confを下記コマンドで有効にします。
a2ensite www.example.com.conf
service apache2 restart

Certbotで無料で SSL

apt-get install certbot python3-certbot-apache
certbot --apache -d www.example.com

Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2を選択

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

cd /etc/apache2/sites-available
vi www.example.com-le-ssl.conf
下記のように生成されます。

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName www.example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/linevetssmessenger
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =www.vetssmessenger.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

更に最初に作成したwww.example.com.confに以下のような変更が生成されます。

vi www.example.com-le-ssl.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/linevetssmessenger
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

systemctl reload apache2 すると自動的にhttpsへリダイレクトされます。
最新の certbot には自動更新の機能がありますので、特別何かを設定する必要はありません。正しく動作しているかsystemctl status certbot.timerで確認してみましょう。

systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Tue 2024-11-19 21:34:29 JST; 1 day 22h ago
    Trigger: Fri 2024-11-22 07:30:13 JST; 11h left
   Triggers: ● certbot.service

11月 19 21:34:29 v133-18-209-152 systemd[1]: Started Run certbot twice daily.

参考サイト:https://tks2.co.jp/2021/10/07/ubuntu-certbot-2021/

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?