About vhost files
Setting up a website has many steps. It’s important to know the rules and technologies that perform data transfer. The two most common servers are programs called Apache and NGINX. Either or both can be used to host a website by setting up rules on a host computer. These rules are text files, called virtual hosting or vhost files, that contain very specific language about domain names and files paths on the host computer. When a request is made to the internet about ‘some-website.com’, networks of processing devices link up to find a host computer associated with that name. The internet returns any content the host computer may have labeled under ‘some-website.com’.
Setting up a domain on a server your first time can be overwhelming. There are so many words to read and learn. That’s a task that requires time and repetition to understand. Once you understand that hosting and domains are like containers and labels you can begin to appreciate the subtlety of all the rules behind who can access them.
Three ways to config vhost
When it’s your job to set up many websites every day, you will quickly find yourself replicating the same setup over and over. Depending on your level of access and/or technical proficiency you will employ a range of solutions to do this.
- You could manually copy-paste-find-replace.
- You could use a command line tool to automate the steps.
- You could use a web interface to control hosting.
For newbie server admins, I strongly recommend manually creating host files before you use an automated tool. every time you need a new website you manually replicate the default host file or an existing host file. Do a global replace on the values that should change like ServerName, ServerAlias, DocumentRoot, ErrorLog, and CustomLog. When you’ve done it enough times that you can plan your own tool, start making one. While you’re doing that, look at the source code for tools other people have created.
Here’s an example apache vhost for ‘some-website.com’
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName some-website.com ServerAlias www.some-website.com DocumentRoot /var/www/some-website.com <Directory /> AllowOverride All </Directory> <Directory /var/www/some-website.com> Options Indexes FollowSymLinks MultiViews AllowOverride all Require all granted </Directory> ErrorLog /var/log/apache2/some-website.com-error.log LogLevel error CustomLog /var/log/apache2/some-website.com-access.log combined </VirtualHost>
Everywhere there is an instance of ‘some-website.com’ is something that needs to change if you wanted to host ‘some-other-website.com’ with the same config settings. Having to do so many text edits times however many new websites you need to configure can be a tedious task. When it’s your job, you’ll learn to set this up more and more efficiently over time. You can make, find, and/or extend existing tools. There are a few command line tools on github for creating vhosts.
I like the tool ‘virtualhost’ by user RoverWire. It is a bash script. It is very easy to implement for Apache and/or NGINX. The above vhost file was generated with that tool. You might notice that to really put the tool to use you would want to make some simple edits. If you open the script to edit it (make a backup and hack away), you will see that it anticipates some customization. You can edit the preferences with the variables at the top. If you’re comfortable editing bash you can venture past the warning and edit the script.
There are many great open source apps for maintaining web hosting on your server. Exploring these options will lead you to find that they often handle more than just config files. For example, user administration is a very common tool. Setting up this kind of software typically happens at the start of life for a server.
You also have the option to create your own app for managing vhosts. I have made a command line tool in python, and a web app in PHP that does this. However fancy you get about copying files and changing a few letters, remember to enable the new vhost file, and reload apache.