How To Set Up Apache Virtual Hosts on Debian

Posted March 29, 2014 2.5k views

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers
You just need to define each virtual host in its own file.

Try this.

Running as root, or using sudo, type:

cd /etc/apache2/sites-available
nano yourdomainname.conf

Then copy into the blank text file that opens:


DirectoryIndex index.php
DocumentRoot /var/www/yourdomainname

You should obviously change "yourdomainname" to whatever your details are.

Also change the /var/www/yourdomainname to wherever you've stored the files for the site.

And finally, swap index.php to whatever your index document is (or leave it as it is if you actually have an index.php).

Then press ctrl+o to save the file.

Press ctrl+x to exit nano.

After this, at the command prompt, type:

a2ensite yourdomainname.conf
service apache2 restart

If you want to disable the virtual host in the future, type:

a2dissite yourdomainname.conf
service apache2 restart

Sorry, the text for the virtualhost file didn't render properly in my answer.

I've uploaded a text file for you to use here: