Report this

What is the reason for this report?

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

Posted on July 25, 2014

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)



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.