Setup virtualhost for hosting multiple domains

Create a folder at your desired location to host your new domain files

mkdir -p /var/www/NewDomain.com

-p helps to create parent directories if they don’t already exist

Edit your hosts file

sudo nano /etc/hosts

Add entry to your hosts file

127.0.1.1 NewDomain.com

Create a new.domain.conf file for your new domain site in /etc/apache2/sites-available. I prefer to copy from 000-default.conf

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/NewDomain.conf
sudo nano /etc/apache2/sites-available/Newdomain.conf

Update your newDomain.com.conf configuration

<VirtualHost *:80>
    ServerName NewDomain.com
    DocumentRoot /var/www/NewDomain.com
ServerName NewDomain.com
ServerAlias www.NewDomain.com
    
    <Directory /var/www/NewDomain.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    
    ErrorLog ${APACHELOGDIR}/NewDomain.com.error.log
    CustomLog ${APACHELOGDIR}/NewDomain.com.access.log combined
</VirtualHost>

Enable new domain site in apache with

sudo a2ensite YourNewDomain.com.conf

Restart Apache2

sudo service apache2 restart

Ensure that you have already setup your public DNS pointing to your server.

Leave a Comment

Your email address will not be published. Required fields are marked *