Apache – Add domain


web-server-iconApache can easily host more than one domain. And here is how.

We assume the new domain is called example.com. First we create the needed directories.
mkdir /var/www/example.com
mkdir /var/www/example.com/logs

If you are going to run web applications like a forum script or even WordPress, apache will need write access to the directories.
chown -R www-data /var/www/example.com
If Apache don’t need write access, or is hosting a dumb html page. You should still give write access to the logs folder. This is convinient for fault finding and to keep track of script kiddies.
chown -R www-data /var/www/example.com/logs

Then we need to create the Apache site config file.
vim /etc/apache2/sites-available/example.com

And modify this to suit your own domain. You can skip the ErrorLog and CustomLog if you dont want to log your site.

<VirtualHost *:80>
ServerAdmin you@example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/example.com
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

<VirtualHost *:80>
ServerAdmin you@example.com
ServerName www.example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

Close vim, vi, nano or what ever text editor your using. And we have to add the domain with:
a2ensite example.com

Then we have to restart Apache to apply the changes.
service apache2 restart (or you can run /etc/init.d/apache2 restart)

You should now have example.com added to your apache server.

Remember to point your dns to your web server.

Happy hosting!

 

Have any Question or Comment?

One comment on “Apache – Add domain

Leave a Reply