Virtual Hosting with Apache http server - an overall scheme, and avoiding common pitfalls
Archive - Originally posted on "The Horse's Mouth" - 2011-01-14 14:04:05 - Graham EllisDuring many of our courses, I write code snippet examples and add them to our website so that the delegates can refer back to them later - and they also form an ongoing resource for others who happen to be looking for examples. But on our Linux Web Server (LAMP) courses, there's far less original code to be written, so fewer opportunities for me to talk about (and document) things which will be important to newcomers to the subject. Yesterday, though ... brought a couple of exceptional new examples:
a) Setting up Virtual Hosting for a whole lot of different domains
There are some "Gotcha"s if you're new to Virtual Hosting ...
i) You need to consider (and code for) the default case of what happens to requests for domains which do not have their own VirtualHost blocks - that includes any requests to the server by IP address number, and any requests which are made using the older HTTP/1.0 standard without any host name specified at all. Solution shown in the example - an empty VirtualHost block which ensures that any such requests use purely the defaults from the main configuration file
ii) You should remember that you're likely to want to serve requests with AND without a leading www. ... Solution shown in example - uses a ServerName and a ServerAlias directive as well
iii) The document root needs to be moved to its own area ... you'll be serving different content on different sites. You need to ensure that the permissions are going to work for every site too. Solution shown in example - add a Directory block in (each) VirtualHost
iv) When you come to look back at how busy your web sites have been, you'll want to know which visitors have come to which site, and in an easier way than trying to filter them from a single log file. Solution shown in example - add a CustomLog to each VirtualHost; while we were at it, we added a separate ErrorLog too, and specified that the logging was to be "combined" rather than "common" so we can look back at our visitors and know where they arrived from, and which browser they're using (or if they're robotic).
Sample - [here].
Virtual Hosting may often be set up through tools such as Webmin, Plesk or cPanel. You should be aware that if you alter the files directly, your changes may get overwritten later by the tool, or even cause the tool to malfunction.
b) Testing the MySQL and PHP are installed, running and talking to each other
An example PHP program - in which we read data from a database through the PHP program, and display it onto the screen, was written ... see [source] and [run it]. This tests the mysql rather than the mysqli drivers, and of course you'll need to change the database password and login to your own and populate the database first (the commands for this are in the course file!)