ask
By:
ask

WIKI: How to add a second domain in Ubuntu 14.04 Droplet?

July 25, 2014 4k views

This is a question with answer. As I have figured out the easiest solution.

Here is the description of the problem -

I have successfully install a Ubuntu 14.04 droplet and hosted a domain say 'myprimary.com' there, the default root is /var/www/html. Further, I have installed CMS and other scripts inside it, and they are working fine. Now, I have bought another domain 'mysecondary.com' and want to create an all new site inside the droplet, but without moving the files of 'myprimary.com' (because of any downtime of this domain)

4 comments
  • Dear @ask .

    Thanks for this instruction.

    I followed it but run into a couple of glitches. (please note that I am a complete newbie).

    I did Step 1 but the command line said that CD is not found.

    So what I did, i went via FTP and created the index file.

    For Step 2, I also made do with FTP by copying the conf file and editing it.

    Step 3> sudo a2ensite from command line said that website.com.ph.conf is not existing, so i tried to do it on the command line >step 2.

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysecondary.com.conf
    sudo nano /etc/apache2/sites-available/mysecondary.com.conf

    it seems that it did the files, but when i redid step 3, i still get the same error. on the ftp, i can see the file is there. i did try to restart apache.

    apache is restarted fine.

    did the htaccess.

    now, the site is redirecting to main site.

    (ps, i have already added the domain at digital ocean networking tab.)

    may i please have a comment on what i can do further to fix it?

    thank you in advance!

    Ps.

    i did the a2ensite command again and it said:

    root@xx:~# a2ensite website.com.ph.conf
    Site website.com.ph already enabled

  • by Justin Ellingwood
    The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 14.04 VPS.
  • Hi @april,
    I think your web root directory is /var/www/ (not /var/www/html), that may be the reason for the Step 1 error
    Further, if you have created the subfolder (where you'll store the files of second site) in your /var/www/ directory , it is fine.
    Let assume your subfolder location is /var/www/mysecondary
    And your second domain is mysecondary.com (it may be any domain you have purchased and DNS set to your digitalocean droplet IP)
    Open the following file

    sudo nano /etc/apache2/sites-available/mysecondary.com.conf
    

    Add/edit to these line

    ServerName mysecondary.com
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/mysecondary
            <Directory /var/www/mysecondary/>
                    AllowOverride All
            </Directory>
    

    Save the file by Ctrl + x
    Please NOTE: Replace mysecondary.com with your second domain name and /var/www/mysecondary with the subfolder you have created for storing the files of your second file.

    Now tell apache with reconfigure

    sudo a2ensite mysecondary.com
    

    Please NOTE: Replace mysecondary.com with your second domain name. New Apache server does NOT require full path of the file

    Then restart the server

    service apache2 reload
    

    Next, do the .htaccess modification as mentioned in the original answer.

  • I think it was my conf file that was the trouble.

    This helped me solve the problem.

    https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

    specifically the server alias and the document root. plus my server name was #commented. :P this might be because i just copied the original 000 conf instead of doing it via the command line as you instructed.

    <VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/publichtml
    ErrorLog ${APACHE
    LOGDIR}/error.log
    CustomLog ${APACHE
    LOG_DIR}/access.log combined
    </VirtualHost>

    Thank you again. Without this, i would never have found the courage to do it.

    by Justin Ellingwood
    The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 14.04 VPS.
2 Answers

The solution -

Step 1: Create a sub-directory inside /var/www/html and place an index.php there

sudo mkdir /var/www/html/mysecondary
sudo cd /var/www/html/mysecondary
sudo nano index.php

Add following lines in index.php file

<?php
phpinfo()
?>

Step 2: Copy 000-default.conf file and modify its content

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysecondary.com.conf 
sudo nano /etc/apache2/sites-available/mysecondary.com.conf

Modify following lines

ServerName myprimary.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        <Directory /var/www/html/>
                AllowOverride All
        </Directory>

to the following

ServerName mysecondary.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/mysecondary
        <Directory /var/www/html/mysecondary/>
                AllowOverride All
        </Directory>

Save the file.

Step 3: Tell apache to reconfigure

sudo a2ensite /etc/apache2/sites-available/mysecondary.com.conf

and restart apache

service apache2 reload

This will append the secondary site information to the default configuration with the primary site

Now if you have correctly set the A record of the secondary domain to your Droplet IP, the domain will be reachable and show the phpinfo() content in the browser. But the same content will be available on myprimary.com/mysecondary link to. Step 4 solves this problem.

Step 4: Open or create .htaccess file in your primary domain root (ie /var/www/html)

sudo nano /var/www/html/.htaccess

And the add the following lines:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myprimary\.com  [NC]
RewriteRule ^apples/(.*)  http://mysecondary.com/$1 [R=301,NC,L]

Do not add the first two lines if there are already there.

Now "myprimary.com/mysecondary" will be permanently redirected to "mysecondary.com"

And both the sites are working independently now, add scripts for the secondary domain into the mysecondary sub-directory

Caution: Create a backup of .htaccess file before modifying it, if you run into some trouble just restore it. You may have to clear cache in your browser (better open an incognito window) to access the secondary domain.

Or the simple way:

  1. Add the mysite.conf file to apache2/available . Note: the conf should include the url to your website & the folder where you store your website on the vps. In this case the folder will be "mysite"
  2. Run a2ensite mysite.conf
  3. Create the folder in /var/www/ and add a html file to it:
mkdir /var/www/mysite
echo "this is it" > /var/www/mysite/index.html
  1. Reload apache: service apache2 reload - you can also do a restart.
  2. Add the A record to your DNS section that will point to your website.

It will take a while to update the DNS but it should be ready in ~30-40 mins

  • You have written the very same way given above . I have shown how to add another domain to already running droplet with a domain. Your line "Note: the conf should include the url to your website & the folder where you store your website on the vps." summarizes the addition and modifications of lines in conf file.
    And the .htaccess part is absolutely necessary if you are already hosting files in root directory of the already existing site, and that is the basic requirement here.

Have another answer? Share your knowledge.