This script is useful for web developers and administrators whom use Apache name based virtual hosts to host multiple websites on the same server and would like to have the Listen and NameVirtualHost directives automatically updated when they add or remove virtual hosts.
Even though these are usually easy to configure it can be tedious to keep these values updated on some setups where hosts are being added and removed frequently. In these setups you could just add these directives to your vhost configuration but, in the case of the Listen directive, you'll need to ensure that they do not conflict so it is more handy to have them all listed in the same place. This is why the default Apache setup breaks these out into a separate file called ports.conf
on most setups. Using this script you'll have the best of both worlds by being able to configure only the vhost definition while still having a central definition to avoid conflicts.
It works by scanning a list of configuration files for virtual host definitions and using those to create a list of Listen and NameVirtualHost directives in a ports.conf
style Apache configuration file. Depending on how Apache is configured on your system this will mean either just scanning your http.conf
file or it may mean scanning your sites-enabled
directory on more advanced setups.
You can either configure the script to overwrite your current ports.conf
file (if you have one) or you can modify your http.conf
file to just include the file that this script generates.
- Get the latest version of this file from the Gist website.
- Put the script files anywhere you like. They'll be slightly easier to use if you put them in a path without spaces though.
- Edit the script configuration at the top of the script to you preferences. It is heavily commented to assist you.
- (Optional) Edit the template file to your needs. This will typically just mean adding any static Listen or NameVirtualHost directives you want.
The default configuration for this script will prevent it from actually doing anything because there are no search paths defined. You must either uncomment one of the paths provided or provide your own.
It is suggested that you first test this script from the command line until you understand exactly how it works.
NOTE: The script currently takes no arguments because all of the options are in the
$cfg
array at the top of the file. This may change in future versions but for now you just need to make copies of this script if you want to run multiple setups.
By default this script will save the output file to the same directory as the script. If your file system supports linking, it is suggested that you simply create a link to that file in your Apache configuration directory and then include
that link from your http.conf
file. If not then you'll want to modify the output_file
configuration option to output the file directly to the Apache configuration directory.