Question

Add VirtualServer automatically to Apache

  • Posted May 16, 2014

Hi,

Currently, I add a VirtualServer to Apache manually for each site I want to host.

Is there any way to add it programmatically? Means create a VirtualServer, Create a directory in /var/www/ and copy some files into it.

Is there any API to do so?

I’m running VirtualMin/Webmin but that is also a manual process. Anything to automate all this?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@Andrew, thank you for the code.

There isn’t any API, but it is certainly something you could script. Something like this would handle the basic use case: <br> <br><pre> <br>#!/bin/bash <br> <br>if [ ! -n “$1” ]; then <br> echo “No host set.” <br> exit <br>fi <br> <br>echo “Creating new VirtualHost /var/www/$1/” <br> <br>touch /etc/apache2/sites-available/$1 <br> <br>mkdir /var/www/$1 <br> <br>cat << EOF > /etc/apache2/sites-available/$1 <br><VirtualHost *:80> <br> ServerAdmin webmaster@localhost <br> ServerName $1 <br> <br> DocumentRoot /var/www/$1/ <br> <Directory /> <br> Options FollowSymLinks <br> AllowOverride None <br> </Directory> <br> <Directory /var/www/> <br> Options Indexes FollowSymLinks MultiViews <br> AllowOverride None <br> Order allow,deny <br> allow from all <br> </Directory> <br> <br> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <br> <Directory “/usr/lib/cgi-bin”> <br> AllowOverride None <br> Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch <br> Order allow,deny <br> Allow from all <br> </Directory> <br> <br> ErrorLog ${APACHE_LOG_DIR}/error.log <br> LogLevel warn <br> CustomLog ${APACHE_LOG_DIR}/access.log combined <br></VirtualHost> <br>EOF <br> <br>a2ensite $1 <br>service apache2 reload <br></pre>