Automate virtual host creation

Good day!

In the development process, you always want to automate routine operations (or it seems to me that way) and proceed directly to really interesting things. Since I am a web developer, deployment of an environment has always been such an operation for me. Today I will try to simplify the task of creating a new host for a new project on the local machine.

If you were tortured by the manual creation of hosts for Apache, and there is no desire / opportunity / your option to use a ready-made package like XAMPP or Denwer, I ask for a cat.


Of course we need Apache. Download and install. Next up is some magic:

Mass virtual hosts


First of all, we select the folder where all our projects will live and make it available for Apache. Let it be d: / sites (I use Windows, so all the examples will be for it, but it should work on Linux too):


  Options Indexes FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all


Now we automate the process of creating new hosts. For this purpose, you need to enable the mod_vhost_alias module for Apache and add only 2 lines to the configuration file:

  NameVirtualHost *:80
  VirtualDocumentRoot d:/sites/%-2


The key % -2 tells Apache to take the penultimate part of the request URL and direct the request to a subfolder in d: / sites . How subdomains will work as a bonus. A few examples:

  http://test.local -> d:/sites/test
  http://qwerty.local -> d:/sites/qwerty
  http://a.domain.local -> d:/sites/domain


More about possible keys can be found on the documentation page .

Comment. With this host configuration, mod_rewrite will not work. The problem is very simple to fix: you just need to specify "RewriteBase /". The result should be something like this:

  RewriteEngine on
  RewriteBase /
  # Your rewrite rules go next


DNS auto configuration



The best solution would be to register a new * .local zone in the router (if any), because the hosts files do not support wildcard (*). If there is no router, then the problem can be circumvented with the help of additional software.

Windows

In order not to register a new host manually each time, you can set a local DNS proxy. I am using Acrylic DNS Proxy . The program is tiny and extremely easy to use.
  1. In the properties of the Internet connection, change the DNS address to 127.0.0.1
  2. We edit the Acrylic settings file: Programs \ Acrylic DNS Proxy \ Config \ Edit Configuration File . You must specify the DNS server of your provider or any other available (for example, Google DNS 8.8.8.8) that Acrylic will use when the domain name is not in the cache.
  3. Editing Programs \ Acrylic DNS Proxy \ Config \ Edit Custom Hosts File . Acrylic understands an asterisk. Hurrah! Personally, I use *. local
  4. We clear the cache and restart Acrylic: Programs \ Acrylic DNS Proxy \ Config \ Purge Acrylic Cache Data


Linux

For the same purpose there is Dnsmasq (thanks Anonym ). Editing /etc/dnsmasq.conf . For the * .local zone you need to add:

address=/local/127.0.0.1
listen-address=127.0.0.1

Save the changes and restart Dnsmasq.

Bonus: email setup


Windows does not have built-in sendmail. I use the tiny Test Mail Server Tool , which simply puts letters in a folder.

Also popular now: