Share This

Wednesday, October 12, 2016

How To Set Up Subdomain or multi-domains and Apache Virtual Hosts on Ubuntu 14.04 LTS

This post is about creating multiple domains/sub-domains or virtual hosts on a single VPS running on Ubuntu 14.04 LTS with LAMP stack. (for LEMP stack, see another good example here.)

Summary

1) Create an A record for your sub-domain that points to your droplet

2) Create a .config file for your sub-domain site in /etc/apache2/sites-available

3) Create the directory for your site in /var/www (or wherever you defined it was in the config file)

4) Enable the site in apache with: sudo a2ensite sub.yourdomain.com (or whatever you named your site in the config)

5) reload apache: sudo service apache2 reload

NOTE: the DNS record you create might take awhile to propagate





Step (1).

In domain's DNS settings (e.g. digitalocean)
A--->demo.example.com.--->IPAddress (NOTE: there's a "." at the end of demo.example.com)

If using CDN (e.g. cloudflare)
A--->demo---------------->IPAddress [DNS & HTTP proxy(CDN)]


Step (2).

/* Copy the default Virtual Host and Customize for Second(sub/new) Domain */
clone existing 000-default.conf under /etc/apache2/sites-available/
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-demo.conf

/* IMPORTANT:: The original or the first Virtual Host must be modifed as below */
E.g. if 000-joomla.conf is the first Virtual Host, then:
        ServerName example.com
        ServerAlias www.example.com
        
NOTE: forgetting to do the step above will cause database or cache conflict.        

E.g. virtualhost for demo.example.com
<VirtualHost *:80>
        <Directory /var/www/demo>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        ServerName demo.example.com
        ServerAlias www.demo.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/demo
        ...
</VirtualHost>


Setp (3).

/* make directory for a new subdomain or domain under the same directory as the original website or create one if not exists. */
Examples:
sudo mkdir -p /var/www/example.com/
sudo mkdir -p /var/www/demo/ (would become demo.example.com)
sudo mkdir -p /var/www/example2.com/

/* grant permission to the new directory: */
sudo chown -R $USER:$USER /var/www/example.com/

/* modify permissions to ensure that read access is permitted: */
sudo chmod -R 755 /var/www


Step (4) & (5).

after that, enable the New Virtual Host Files as below:
E.g:
sudo a2ensite example.com.conf
sudo a2ensite 000-demo.conf

then, reload apache
sudo service apache2 reload


NOTE:  
The config file's name (e.g. 000-demo.conf), directory name (e.g. /var/www/demo/) and the domain name (e.g. demo.example.com) don't have to be given the same name,

for e.g. we can have (demo.000.conf or demo-123.conf ...) and document root as (/var/www/demo.example/ or /var/www/demo123/ ...) since the ServerName and DocumentRoot being specified inside <VirtualHost> Directive in the config file will take care of it.



Ref: How to Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

1 comment:

  1. Hi , I have question related to the Joomla 4 which you were answered for Joomla 3 https://joomla.stackexchange.com/questions/13930/how-to-get-categories-of-custom-extension-or-third-party-component on the Joomla stack overflow, If you have any idea can you please help

    ReplyDelete