How to set up a new virtualhost domain in Apache 2
This guide will show you how to quickly set up a new website in Apache 2 using virtualhosts. Follow these instructions exactly as written, down to the character: trailing slashes, case, line breaks, etc.
Start by firing up a terminal, and creating a folder for the new website:
mkdir /var/www/my-new-website.com
I like to name Apache folders after the actual website URL, to decrease potential confusion down the road.
Next, we will copy the default Apache2 virtualhost configuration file, renaming it to your domain:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/my-new-website.com
And open that file in your favorite text editor:
sudo nano /etc/apache2/sites-available/my-new-website.com
Make sure your email address is correct:
ServerAdmin youraddress@gmail.com
Below the ServerAdmin directive, add two lines. Notice the www before the ServerAlias directive:
ServerName my-new-website.com
ServerAlias www.my-new-website.com
Now change the DocumentRoot:
DocumentRoot /var/www/my-new-website.com/
Also change the Directory directive’s path:
<Directory /var/www/my-new-website.com/>
You can now save this file and exit it.
Back in the terminal, type:
sudo a2ensite my-new-website.com
sudo service apache2 restart
Upload your website files into the /var/www/my-new-website.com folder, using your favorite FTP client.
Then go to www.my-new-website.com and your website will be online.
It’s that simple!
N.B. For this to work best, follow my minimal Apache2 guide.