The Internet is replete with guidelines for configuring virtual hosts in Apache. But, in most cases, the creation of such a subdomain is troublesome.
According to the "standard" instructions are invited to do the following:
- Create a folder for the site
- Create a configuration file with the name of the future domain
- Include site special option
- Reload Apache
- Register our domain in the hosts file
Some try to optimize this process with different scripts, but this, in fact, does not solve problems.
So, we will try to ensure that the process of creating a subdomain is limited only to creating a folder for the site. Is it possible? Check it out ...
I will not tell you how to install LAMP, as you most likely can do it with closed eyes (smile). We turn to the most interesting.
Configure vhost_alias
We
include the vhost_alias module. He then will be the main character.
sudo a2enmod vhost_alias
Enable, if necessary,
mod_rewrite .
sudo a2enmod rewrite
Open the file
httpd.conf and proceed to the direct configuration.
% -2 means that the host will be selected by the penultimate part of the domain name. In other words, by creating the directory
/ home /% username% / web / habrahabr , we can access it as
habrahabr.ru (or
habrahabr.com , or even
habrahabr.xxx ).
You can also set your own host name selection options:
- % 0 Full Name
- % 1 first part of the name
- % 2 second part of the name
- % 3 Third part of the name
- % -1 last part
- % -2 penultimate part
- % 2 + Second and all subsequent parts
- % -2 + 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 with some content, for example "
<? Php phpinfo ();?> ".
Oh yeah, you still need to register our domain in the file
/ etc / hosts .
127.0.0.1 test.loc
Everything, now it is possible to open a page in the browser.
Stop, we did not agree! Creating a site should be limited to creating a directory for it. Well, let's do ...
DNS server setup
For this we will use the
bind9 DNS server. All domains with the suffix
* .loc will look at our local machine.
Install the 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;
Create files for the domain zone.
cd /etc/bind/ sudo touch db.loc
Db.loc content
$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 file
named.conf.local and
append it there.
zone "loc" { type master; file "/etc/bind/db.loc"; allow-transfer { 127.0.0.1; }; notify no; };
Remain connected to our DNS server. Or through the
/etc/resolv.conf file, adding the line
nameserver 127.0.0.1
or through a standard network connection manager. In the connection properties, on the IPv4 Settings tab, add the address 127.0.0.1 in the DNS Servers field.

For convenience, create a local host for phpmyadmin
ln -s /usr/share/phpmyadmin/ /home/alex/web/phpmyadmin
Now it is available at
phpmyadmin.loc .
Some notes
There are a couple of comments on working with vhost_alias.
- Wrong data is given by the $ _SERVER ['DOCUMENT_ROOT'] variable, so you have to use either dirname (__ FILE__) or realpath () . It depends on what you need.
- If mod_rewrite stopped working, do not panic. In the .htaccess file after the line
RewriteEngine on
We insert
RewriteBase /
Related Links:
httpd.apache.org/docs/2.0/ru/vhosts/mass.htmlwww.softtime.ru/info/apache.php?id_article=103PS Thank you
Wott for the kindly provided bind configs.