How to Create Apache Virtual Hosts

Apache Logo

I’ve changed the structure of my websites.
Here’s my DNS Record Settings.

For the domain xasiimov.com:
    @ and www -- My Homepage
    blog -- My WordPress Blog

For the domain 503craft.com:
    @ and www -- 503Craft Homepage

The coolest fact is that these three websites are simultaneously running on the same server since I’ve changed the config files of Apache and create Virtual Hosts.

The virtual host’s config files are stored in a particular directory in your Apache installation path.

 /usr/local/apache/conf/vhost/

For convenience, you could create configure files for each of your virtual websites. For example, I created xasiimov.com.conf, blog.xasiimov.com.conf, and 503craft.com.conf in this directory.

Here’s the basic structure of vhost config file.

<VirtualHost *:80>
    ServerAdmin <Your Email>
    DocumentRoot <Website Path>
    ServerName <Url>
    ServerAlias <Variant Url>
</VirtualHost>

Here’s an example.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /data/www/default
    ServerName xasiimov.com
    ServerAlias www.xasiimov.com
</VirtualHost>

If you want to enable SSL for your website, you need to change the vhost config files to let Apache know the location of SSL Certificates and Private Keys.

<VirtualHost *:443>
    ServerAdmin <Your Email>
    DocumentRoot <Website Path>
    ServerName <Url>
    ServerAlias <Variant Url>
    SSLEngine on
    SSLCertificateFile <Full Path to SSL Certificate File>
    SSLCertificateKeyFile <Full Path to SSL Private Key File>
</VirtualHost>

Here’s an example.

<VirtualHost *:443>
    ServerAdmin [email protected]
    DocumentRoot /data/www/default
    ServerName xasiimov.com
    ServerAlias www.xasiimov.com
    SSLEngine on
    SSLCertificateFile /usr/local/apache/conf/ssl/xasiimov.com.pem
    SSLCertificateKeyFile /usr/local/apache/conf/ssl/xasiimov.com.key
</VirtualHost>

Test Apache Configuration and restart Apache Server.

apachectl configtest
/etc/init.d/httpd restart

Finally, you should change DNS Settings for your domain to make sure all the subdomains are pointing to your original server.

For example, you should follow the format in the image below if you want to let “blog.yourdomain.com” point to 1.1.1.1.

Published by

Siujoeng Lau

Liberty will never perish.

Leave a Reply

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