65

Is it possible? Someone told me it is but I'm not sure.

If its possible, how should I do it? I have one /www folder where my website lies. How can I configure 2 different sites?

Kara
  • 6,115
  • 16
  • 50
  • 57
Artemix
  • 8,497
  • 14
  • 48
  • 75
  • 5
    Not really a programming question, belongs on webmasters site. But, in short, yes, you can, unless your VPS contract forbids it. – Marc B Jan 26 '11 at 19:41
  • 1
    Yes, is some kind of greay area, – Artemix Jan 26 '11 at 21:13
  • 3
    I voted to close this question because it is not a programming question and it is off-topic on Stack Overflow. Non-programming questions about your website should be asked on [webmasters.se]. In the future, please ask questions like this there. – Stephen Ostermiller Jan 25 '22 at 18:39

3 Answers3

72

As complete beginner, I have been trying to host multiple domains on one Apache VPS. Tutorials had too much information that lead me to confusion.

Below I describe, for complete begginers, how to host multiple domains on one VPS server with Ubuntu and Apache.

IMPORTANT! You need to use root account to execute most operations.

IMPORTANT! If you have been trying to make some changes to apache configuration before, undo them.

Creating VirtualHosts

Create folders for your domains on server. For example:

/home/apache/domain1

/home/apache/domain2

Put index.html file in each folder with any text.

This is domain1
This is domain2

Go to /etc/apache2/sites-available folder.

/etc/apache2/sites-available

Create file domain1

sudo nano domain1

<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>

Create file domain2

sudo nano domain2

<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>

You can create subdomains same way.

sudo nano blog

<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>

Enable created sites

sudo a2ensite domain1
sudo a2ensite domain2

Restart apache

sudo service apache2 reload

Redirecting domain to server

Created VirtualHosts will work only if you redirect your domain name to server IP. Domains are just names that can be translated to IP numbers.

Local computer

To test your configuration on local machine, you need to edit hosts file.

sudo nano /etc/hosts

It should look like this.

127.0.0.1       localhost domain1.com domain2.com

Hosts file tells your computer that domain needs to be redirected to local machine.

IMPORTANT! If you create entry in hosts file for existing domain, for example

127.0.0.1       stackoverflow.com

you will lose access to this website.

Server

In order to redirect domain to you web server, you need to create or modify "A"-type DNS record for given domain to IP address of your server. You can do it by panel control provided by your domain registrar.

If you do not know IP address of your server, log in to that server and type in command line:

ifconfig
Community
  • 1
  • 1
Rafal
  • 864
  • 10
  • 21
  • 8
    When creating in sites-available, they have the extension .conf so instead of sudo nano domain1 it should be sudo nano domain1.conf .. or rename the file if you've already created it using mv – george rizk Mar 09 '16 at 13:19
  • 1
    also, when specifying the DocumentRoot, the path should be a string. "/home/apache/blog" instead of /home/apache/blog – george rizk Mar 09 '16 at 19:28
  • for extensions and "Site X does not exist" errors, check: https://stackoverflow.com/a/20592059/1259865 – Asqan Dec 14 '19 at 01:44
  • for 403 forbidden error, check: https://www.digitalocean.com/community/questions/forbidden-you-don-t-have-permissions-to-access-on-this-server – Asqan Dec 14 '19 at 02:40
  • also see https://stackoverflow.com/questions/21812360/what-is-the-difference-between-the-sites-enabled-and-sites-available-directo – Caleb Jay Mar 01 '22 at 02:42
59

The procedure is:

  1. Point both domains to the ip of the VPS.

  2. Configure the webserver you have installed on your VPS to answer to those two domains.

In the case of Nginx with Passenger, it is a matter of adding entries to you nginx.conf file. Like this:

server {
      listen 80;
      server_name domain1;
      root /path/to/your/project;
      passenger_enabled on;
   } 
server {
      listen 80;
      server_name domain2;
      root /path/to/your/project;
      passenger_enabled on;
   }

Check your webserver documentation to do a similar thing.

Nerian
  • 15,901
  • 13
  • 66
  • 96
  • 1
    Thx for your answer, Im using Apache as web server, any ideas how can I accomplish that?, if not, any links?, I googled but no luck so far. – Artemix Jan 26 '11 at 21:14
  • 3
    I never used Apache in production, but I found this link: http://httpd.apache.org/docs/2.0/vhosts/examples.html – Nerian Jan 26 '11 at 23:41
  • If DNS resolution for both the hostnames results in the same IP address and if the requests arrive at the same IP address and port number, how does the web server serve the correct sites for the two domains? The answer is in the HTTP "Host:" header. Based on the "Host:" header from the request, the webserver will choose the correct website to serve. – MediumOne Dec 25 '20 at 16:49
15

This is always possible ... You can always configure name servers using one or two ip addresses on your VPS. Once configured, you can start pointing multiple domains using those name servers.

On my VPS there is only one IP address, but I am hosting 35+ sites there ....

AccuWebHosting.Com
  • 1,159
  • 1
  • 7
  • 17