N
N
Nikita Kolosov2014-06-03 16:29:25
Apache HTTP Server
Nikita Kolosov, 2014-06-03 16:29:25

How to create subdomain on apache server?

There is a server, for example, test.com
Apache is running there, etc., it is written in /sites-avaliable/default

ServerName test.com
DocumentRoot /var/www/site/

I would like /var/www/test/ to open at test.test.com, and /var/www/test/ for all other *.test.com.
Just adding another virtual host with the contents
<VirtualHost *:80>
        ServerName test.test.com
        DocumentRoot /var/www/test
</VirtualHost>

did not help.
Actually, the question is: what to do?
//Output of the commands that were asked in the comments
cat /etc/apache2/sites-available/default
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName www.test.com

        DocumentRoot /var/www/site/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                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 All
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
$ cat /etc/apache2/sites-available/test   
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName test.test.com

        DocumentRoot /var/www/test/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                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 All
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
$ host www.test.com
www.test.com has address XX.247.172.108
$ host test.test.com
test.test.com has address XX.247.172.108

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2014-06-03
@merryjane

apachectl -S shows what?

H
hodakovskiy, 2015-09-09
@hodakovskiy

Setting up Linux* The
Internet is full of tutorials on how to set up Apache virtual hosts. But, in most cases, creating such a subdomain seems to be troublesome.
According to the "standard" instructions, it is proposed to do the following:
Create a folder for the site
Create a configuration file with the name of the future domain
Enable the site with a special option
Restart Apache
Register our domain in the hosts file
Some people try to optimize this process with various scripts, but this, in fact, does not solve the problem.
So, let's try to ensure that the process of creating a subdomain is reduced only to creating a folder for the site. Is it possible? Let's check...
I will not tell you how to install LAMP, since you can most likely do it with your eyes closed (smile). Let's move on to the most interesting.
Configuring vhost_alias Enable the vhost_alias
module. He will be the main character.
sudo a2enmod vhost_alias
Turn on mod_rewrite if needed.
sudo a2enmod rewrite
Open the httpd.conf file and proceed to the direct configuration.
#We substitute the server name from the user request header
UseCanonicalName Off
# Form the logs so that they indicate the name of the virtual host
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /home/ %username%/web/access_log vcommon
# Required for mod_rewrite to work
Options FollowSymLinks
AllowOverride All
# Actually, the rule by which the site we need will be searched
VirtualDocumentRoot /home/%username%/web/%-2
%-2 means that the host will be selected by the penultimate part of the domain name. In other words, by creating the /home/%username%/web/habrahabr directory, we can refer to it as habrahabr.ru (or habrahabr.com, or even habrahabr.xxx).
You can also set your own hostname selection options:
%0 Full name
%1 First part of name
%2 Second part of name
%3 Third part of name
%-1 Last part
%-2 Penultimate part
%2+ Second and all subsequent parts
%-2+ The penultimate and all subsequent parts
Restart Apache.
sudo service apache2 restart
Our server is already running. We can verify this by creating a folder with the desired name, for example test and placing index.php there with some content, for example "<?php phpinfo(); ?>".
Oh yes, we still need to register our domain in the /etc/hosts file.
127.0.0.1 test.loc
That's it, now you can open the page in the browser.
Wait, we didn't agree! Creating a site should be reduced to creating a directory for it. Well, let's do it...
Setting up the DNS server
For this we will use the bind9 DNS server. All domains with *.loc suffix will look at our local machine.
Install DNS server
sudo apt-get install bind9
Open the configuration file named.conf.options and add
acl "home" {192.168.1.0/24; 127.0.0.1;};
options {
directory "/var/cache/bind";
auth-nxdomain no;
listen-on-v6 { none; };
listen-on { 127.0.0.1; };
allow-transfer {none; };
allow-query {"home";};
forward first;
# Specify provider DNS addresses
forwarders {
192.168.1.2;
8.8.8.8;
};
};
We create files for the domain zone.
cd /etc/bind/
sudo touch db.loc
Contents of db.loc
$TTL 86400
$ORIGIN loc.
@ IN SOA skywrtr.loc. admin.skywrtr.loc. (
2010050100; Serial
14400; Refresh
7200; Retry
3600000; Expire
86400); Minimum
@ IN NS localhost.
* IN A 127.0.0.1
Finally, open the named.conf.local file and add
zone "loc" {
type master;
file "/etc/bind/db.loc";
allow-transfer { 127.0.0.1; };
notify no;
};
Just connect to our DNS server. Or through the file /etc/resolv.conf by adding the line
nameserver 127.0.0.1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question