📜 ⬆️ ⬇️

Multidomain in Apache without the hassle on the local host

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:
  1. Create a folder for the site
  2. Create a configuration file with the name of the future domain
  3. Include site special option
  4. Reload Apache
  5. 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.
 #       UseCanonicalName Off #   ,        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon CustomLog /home/%username%/web/access_log vcommon #    mod_rewrite <Directory /home/%username%/web> Options FollowSymLinks AllowOverride All </Directory> #  ,        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 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:

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; #  DNS-  forwarders { 192.168.1.2; 8.8.8.8; }; }; 

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.


Related Links:
httpd.apache.org/docs/2.0/ru/vhosts/mass.html
www.softtime.ru/info/apache.php?id_article=103

PS Thank you Wott for the kindly provided bind configs.

Source: https://habr.com/ru/post/129900/


All Articles