📜 ⬆️ ⬇️

Using mod_macro to configure Apache virtual hosts

Good day,

I publish this topic based on the topic Programming in PHP for the command line , in which, in my opinion, the not very optimal process of adding virtual hosts using PHP in the command line mode is described.
For those of you who are familiar with mod_macro , this topic will seem uninteresting and you can skip it.

So, the next task is to easily add new virtual hosts to apache.
The first thing that comes to mind is to describe the virtual host pattern in the form of a macro that can be used more than once, in which the mod_macro module will help us.

  1. Install mod_macro (installation for debian / ubuntu, but for other systems there should be no difficulties) and turn it on
     sudo apt-get install libapache2-mod-macro
     sudo a2enmod macro
    

  2. Create a file in which the virtual host pattern will be located.
     sudo mkdir /etc/apache2/conf.d/custom
     sudo touch /etc/apache2/conf.d/custom/vhosts
    

  3. Add the macro code to the file:
     <Macro VHost $ name $ path>
         <VirtualHost *: 80>
             ServerName $ name
             DocumentRoot $ path
         </ Virtualhost>
     </ Macro>
    

  4. Let's write a simple script that will use our macro:
     touch addvhost
    
     #! / bin / bash
     echo "Use VHost $ 1 $ 2" >> /etc/apache2/conf.d/custom/vhosts
     apache2ctl configtest && apache2ctl reload
    
     chmod + x addvhost
    

  5. The use is rather trivial:
      ./addvhost <hostname> </ path / to / wwwroot> 


')
PS In no case do I want to provoke controversy "PHP vs bash vs Python vs ...".

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


All Articles