a2mksite: Apache 2 Virtual Host Generator Script
I'm not a big fan of using control panels like Plesk and cPanel to manage my Apache sites. But setting up a new site by hand can take minutes. I don't have that kind of time! :)
This script allows you to type one command to set up an entire site including:
- Creation of the root html directory (using an optional skeleton structure for a new site) for the site
- Creation of a site-specific Apache log directory with recommended permissions
- Creation of empty access.log and error.log files with recommended permissions
- Creation of an Apache configuration file in your 'sites-available' directory (using an optional user-template)
- Creation of a logrotate configuration file for rotating your site's Apache logs (using an optional user-template)
- Symlinking the Apache conf file into the 'sites-enabled' directory via a2ensite
- Gracefully restarting Apache
PLEASE NOTE: This script is intended to be a time saver. It's not a substitute for understanding your server's configuration. You should understand what these configuration files do before using this script.
This script works fine for me (using Ubuntu 8.04, Apache 2.2 and logrotate on Slicehost), but server setups vary greatly. USE THIS SCRIPT AT YOUR OWN RISK!! It mainly just creates some files and restarts Apache, but there is always risk involved with running a script (esp. using sudo). I AM NOT RESPONSIBLE FOR DAMAGE CAUSED BY THIS SCRIPT.
If you're a web server admin, you may very well know more about configuring Apache and logrotate than I do. If you find any flaws with this script or have any recommendations as to how this script can be improved, please fork it and send me a pull request.
This script was created for use with Apache 2.x on Ubuntu. It will probably work on most other Linux distros with a little tweaking of the configuration at the top of the a2mksite script. See "Affected Files and Directories" below for specifics.
- Place this a2mksite directory somewhere on your server (I like /usr/local)
- Make sure the a2mksite and a2rmsite scripts are executable by you:
chmod 744 a2mksite/a2mksite a2mksite/a2rmsite
- Optionally, create Apache and logrotate configuration templates and site skeleton (see below)
- You might also want to create an alias to the script.
A bash alias would look like this:
alias a2mksite='sudo /usr/local/a2mksite/a2mksite'
sudo a2mksite domain.com
To undo the things that a2mksite does, use a2rmsite similarly:
sudo a2rmsite domain.com
This script creates several files owned by root; so, sudo is necessary.
Affected Files and Directories
By default, these are the directories and files created. This is how I like my web server set up. You may prefer a different structure. The structure can be customized by opening the script and changing the configuration variables near the top of the script.
/var/www/sites/example.com/ /var/www/sites/example.com/public/ /var/www/sites/example.com/log/ /var/www/sites/example.com/log/access.log /var/www/sites/example.com/log/error.log /etc/apache2/sites-available/example.com /etc/apache2/sites-enabled/example.com /etc/logrotate.d/sites /etc/logrotate.d/sites.d/example.com.conf
Using Configuration File Templates
If you would prefer to customize the config files that are generated by this script, copy the 'templates' directory to your home directory and rename it to 'a2mksite_templates'. Then, open the conf files in your text editor and go to town.
Using a Custom Site Placeholder
Similarly, the contents of the public directory (your default web documents) can be customized by copying the 'templates' directory to your home directory, renaming it to 'a2mksite_templates', and editing the contents of the public directory within it.
Regarding Apache Config Differences
It seems that some Apache configurations include a port number (*:80) in the NameVirtualHost directive, and some don't. Either way, the VirtualHost directive in the site's config file needs to match. In the included site config template, I've set it up to include the port number. Check your NameVirtualHost directive (in either sites-available/default, apache2.conf or httpd.conf) to see if your VirtualHost conf needs to declare a port number. If it doesn't, you'll need to modify the template (see "Using Configuration File Templates" above).
This script creates the config files for logrotate, but you still need to make sure you have logrotate installed.