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.