Ubuntuでバーチャルホストを設定する手順

スポンサーリンク

XserverVPSに入れたUbuntu 24.04にbbb.example.comを追加でアクセスできるようにする手順のメモ。

すでにApacheは入っていて、ドメインのDNSもサーバーに向けて設定されている前提。

バーチャルホストの設定

sudo nano /etc/apache2/sites-available/bbb.example.com

でファイルを作って、以下の内容を記述して保存。

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName bbb.example.com
    ServerAlias www.bbb.example.com
    DocumentRoot /var/www/bbb.example.com

    <Directory /var/www/bbb.example.com>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/bbb.example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/bbb.example.com_access.log combined
</VirtualHost>

↓公開フォルダを作っておく

sudo mkdir -p /var/www/bbb.example.com
sudo chown -R ユーザー名:グループ名 /var/www/bbb.example.com
sudo chmod -R 755 /var/www/bbb.example.com

↓設定を反映させる。

sudo a2ensite bbb.example.com
sudo systemctl reload apache2

あとは、/var/www/bbb.example.com/test.html など作って、ブラウザからアクセスできるか確認したらいい。でもSSLの設定してないと今のブラウザからは表示されない模様。強制的にhttpsになるので。

SSLの設定

Let’s Encryptで無料のSSLを設定する。CertBotを使って簡単に設定できる。

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d bbb.example.com

↓成功したらこんな感じになる

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for bbb.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/bbb.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/bbb.example.com/privkey.pem
This certificate expires on 2024-10-18.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Some rewrite rules copied from /etc/apache2/sites-enabled/bbb.example.com.conf were disabled in the vhost for your HTTPS site located at /etc/apache2/sites-available/bbb.example.com-le-ssl.conf because they have the potential to create redirection loops.
Successfully deployed certificate for bbb.example.com to /etc/apache2/sites-available/bbb.example.com-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://bbb.example.com

/etc/apache2/sites-available/bbb.example.com-le-ssl.conf が自動的に作成されていて、以下のような内容になっている。

<IfModule mod_ssl.c>
<VirtualHost *:443>
	ServerName bbb.example.com
	ServerAlias www.bbb.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/bbb.example.com

	ErrorLog ${APACHE_LOG_DIR}/bbb.example.com_error.log
	CustomLog ${APACHE_LOG_DIR}/bbb.example.com_access.log combined

	<Directory /var/www/bbb.example.com>
		AllowOverride All
		Require all granted
	</Directory>

	RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

# 	RewriteCond %{SERVER_NAME} =www.bbb.example.com [OR]
# 	RewriteCond %{SERVER_NAME} =bbb.example.com
# 	RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

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

↓で、またApache再起動して適用。

sudo systemctl restart apache2

以上でhttps://bbb.example.com/でアクセスできたらOK!

その他メモ

ちょっとやったことでメモ。

・UFWでファイヤーウォールの設定をしてみたが、これをやるとブラウザから参照ができなくなった。これはXserverVPSの管理画面でポートの開放設定があるので、そちらとバッティングしている可能性がある。管理画面での設定を優先して、UFWはデフォルトのままOFFとすることにした。

・XserverVPSの管理画面でDNS設定があるが、当然ドメイン管理会社のDNSでAレコードをサーバーIPに向けていればこの設定は不要。

以上。

コメント