PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How to host multiple websites on different folders on a VPS?

 August 01, 2022     .htaccess, dns, virtualhost, vps     No comments   

Issue

I have a domain1.com pointing to my VPS server (I just specified the IP address in an A entry). More specifically the domain1.com is pointing to folder /var/www/html of my VPS.

I want to host another website on this VPS with domain name: domain2.com

My idea is to create a new directory: /var/www/html2, put the code of the new website in it, and then to redirect domain2.com to this folder.

But, I don't see how to do this by adding a A (DNS) entry. Also, is there not a solution to work with .htaccess ?


Solution

By the sounds of it you've probably configured your main server config for domain1.com. This is OK if you are only hosting 1 site on your server, but if you are hosting multiple websites (different domains) then you need to configure virtual hosts instead. One for domain1.com and another for domain2.com, etc.

You've probably put the necessary directives (ie. ServerName, DocumentRoot etc.) for domain1.com directly in the main server config, these need to be moved to a <VirtualHost> container instead.

For example:

<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Require all granted
        # Enable .htaccess overrides if required
        AllowOverride All
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/html2

    <Directory /var/www/html2>
        Require all granted
        # Enable .htaccess overrides if required
        AllowOverride All
    </Directory>
</VirtualHost>

Add the same A record for both domains. (You'll probably want to do the same for the www subdomain as well, or create a CNAME record.) So both domains point to the same IP address (ie. your server). Apache can then determine which virtual host to route the request to by comparing Host header on the request (HTTP 1.1) with the ServerName (or ServerAlias) directives inside the <VirtualHost> containers.

It's usual to have the <VirtualHost> containers in a separate .conf file that is included later in the main server config using the Include directive.

Reference:

  • https://httpd.apache.org/docs/2.4/vhosts/
  • https://httpd.apache.org/docs/2.4/mod/core.html#virtualhost
  • https://httpd.apache.org/docs/2.4/mod/core.html#include

UPDATE:

Also, is there not a solution to work with .htaccess ?

You don't implement virtual hosts using .htaccess. With .htaccess you can rewrite the URL so you can simulate something similar (with a single virtual host that is serving multiple domains) but that is not necessary here and not the way it should be done if you are hosting entirely separate sites.



Answered By - MrWhite
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing