This is gonna be a quick one, but I think you’ll find this quite useful … if you don’t already know it by heart ;)
As I promised in my initial Dev-Box post, I promised to give further useful instructions/tips for your development VM, so here we go: mod_rewrite and Virtual Hosts for Apache. Two basics that every coder should know by heart and uses every day (even though he might not be aware of it). I, for example, create a virtual host for every new project I start. That way I can access them easily via their own URL and don’t have to remember the exact folders they reside in.
This is a module for Apache that “uses a rule-based rewriting engine to rewrite requested URLs on the fly” (quoted from the Apache Docs). It allows you to use .htaccess files to redirect/restrict access to your website very easily. Installing is quite easy and the only thing you need to do, no configuration needed:
That’s it, now you can use .htaccess just like you are used to.
Virtual Hosts basically let you maintain more than one server/website on one machine and allow you to use a simple url like http://my_project.vm instead of something like http://url-to-my-vm/folder1/folder2/folder3. They are a bit more tricky but nothing too hard and once you got the first one working it’s mostly copy & paste. We’ll start with editing the Apache config and then we’ll continue and finish with editing your hosts file.
For each virtual host (=website) you want to run on your server you’ll have to create a file. In this example we’ll setup a virtual host for local website, e.g. dev-box.vm:
Now we’ve navigated to the folder where all virtual hosts are stored and opened a new file called “dev-box” in our well-known editor nano. I’ve accustomed myself to naming the files the same as the website (in this case dev-box), but you can actually name them however you want. Anway, the contents of a virtual host file looks always the same, except three lines that are unique for each website you set up:
Options Indexes FollowSymLinks MultiViews
allow from all
CustomLog /var/log/apache2/access.log combined
Place the above in the nano editor and replace the placeholders with the following:
url-to-website: This should be the actual url that you’ll use to access this website, in our case dev-box.vm
path-to-website-folder: This should be the path to the root folder of your website, basically where the index.php will be. In this case something like /var/www/dev-box/
After you save the file you’ll have to enable it and that’s it for the linux part:
For future virtual hosts you can just copy your existing one to a new file and edit that one:
cp dev-box new-project
Now go to your hosts file on your system (C:\Windows\System32\drivers\etc for Windows), open the host file and add the following line:
Save the file and that’s it. From now on when you go to dev-box.vm in your browser it will use your VM’s IP to look up the IP and the server will know that you are requesting dev-box.vm and that it should look for the files in the website folder you entered in the virtual hosts file.