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!
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:
a2ensite mysite.confmkdir /var/www/mysite
echo "this is it" > /var/www/mysite/index.html
service apache2 reload - you can also do a restart.It will take a while to update the DNS but it should be ready in ~30-40 mins
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.