Ubuntu、apache2 ワイルドカード DNS からサブドメインへ

Ubuntu、apache2 ワイルドカード DNS からサブドメインへ

現在、私は自分の (ubuntu) サーバーをホストしており、次のサービスを使用しています: samba、ftp、およびウェブサーバー。ドメインを購入し、DNS A レコードを ISP の IP にリンクしました。これは正常に動作しています。次に、DNS ワイルドカード レコードを使用してサブドメインを作成したいと思います。DNS 変更が完了するまで 24 時間待つことは避けたいです。

これまでのところ、リダイレクトすることしかできませんでした全て同じディレクトリへの受信ワイルドカード:

test1.domain.com は /var/www にリダイレクトされます

test2.domain.com は /var/www にリダイレクトされます

私が取得したいのは:

test1.domain.com は /var/www/test1 にリダイレクトされます。

test2.domain.com は /var/www/test2 にリダイレクトされます。

私の推測では、/etc/apache2/sites-available/domain ファイルを変更することになります。

どのような助けやヒントでも歓迎します!

ありがとう、

マーク

編集:

私の /etc/apache2/sites-available/domain ファイルは次のようになります。

<VirtualHost *:80>
        ServerAdmin webmaster@domain

        DocumentRoot /var/www/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/webroot>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

答え1

を使用すると、目的の動作が得られるはずですVirtualDocumentRoot

内に を<VirtualHost>追加して、ServerAlias関心のあるすべてのドメインを取得します。

    ServerAlias *.example.com

...それらを目的のディレクトリにマップします。 を削除しDocumentRoot、代わりに以下を追加します。

    VirtualDocumentRoot /var/www/%1

アクセスを許可するブロックを用意する必要があります<Directory /var/www/>が、この vhost は動的に構成された vhost のサービスのみを処理する必要があることに注意してください。 とexample.comwww.example.com個別に処理したい場合は、それぞれに独自の を用意する必要があります<VirtualHost>

編集:

「ベース」ドメインを処理するには、別の仮想ホストを使用する必要があります。コメントの現在の構成を基に構築します。

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName catchall.example.com
    ServerAlias *.example.com
    # NOTE: this config will map test1.example.com to /var/www/test1
    VirtualDocumentRoot /var/www/%1
    # If you want that to map instead to /var/www/test1.example.com, then use %0:
    # VirtualDocumentRoot /var/www/%0
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# This next vhost should be in a different file in sites-available to
# fit with debian-style vhosts - but make sure it's alphabetically
# after the file that contains the first vhost - we want it to load first
# so that it's default.  It can also just go in the same file.
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/www.example.com
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

答え2

答えを見つけるのを手伝ってくれてありがとう。ここで私の問題の解決策が見つかりました。これは、私のような他の初心者が上記の問題の解決策を見つけるのに役立つと思います。

ステップ1: 以下のとおりにウェブサイトを設定します

vi /etc/apache2/sites-available/yoursite

<VirtualHost *:80> ServerAlias localhost *.yoursite #wildcard catch all VirtualDocumentRoot /path/to/your/workspace/%1/public UseCanonicalName Off <Directory "path/to/your/workspace"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

2)sudo a2ensite /etc/apache2/sites-available/yoursite

3) sudo サービス apache2 をリロードする

4)Dnsmasqをインストールします: sudo apt-get install dnsmasq

5) /etc/NetworkManager/NetworkManager.conf を開き、dns=dnsmasq の行をコメント アウトします。その後、NetworkManager を再起動します: sudo restart network-manager。

6)vi /etc/dnsmasq.conf の listen-address=127.0.0.1 の行。

7) /etc/dnsmasq.dに新しいファイルを作成し、ファイルを開いて次のように編集します。

address=/yourdomain/127.0.0.1

8)Dnsmasq を再起動します: sudo /etc/init.d/dnsmasq restart。

これは nginx でも実行できます。 この解決策は私にとってはうまくいきました。他の人にもうまくいくと思います。

関連情報