-2

On my Ubuntu 16.04 system, I want to set up several websites that are served from several directories, that is, same domain, same port, same IP address. However, I don't know how to specify them. All instructions I could find cover setting up virtualhosts for different domains.

That's how far I am right now:

/etc/apache2/sites/sites-enabled/site1.conf:

<VirtualHost *:80>
  ServerName test.test
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site1/
  <Directory /var/www/vhosts/site1/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>

/var/www/vhosts/site1/index.html

<h1>site 1</h1>

/etc/apache2/sites/sites-enabled/site2.conf:

<VirtualHost *:80>
  ServerName test.test
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site2/
  <Directory /var/www/vhosts/site2/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>

/var/www/vhosts/site2/index.html

<h1>site 2</h1>

When I point my web browser to http://test.test,

site 1

is showing up in the browser.

How/where can I specify that hxxp://test.test/site1 shall point to /var/www/vhosts/site1/(index.html) and hxxp://test.test/site2 shall point to /var/www/vhosts/site2/(index.html)?

Bonus: hxxp://test.test and probably hxxp://test.test/site3 should point to somewhere else.

2

I don't think you can do what you want to do. Apache httpd will server the first vhost that has a SeverName or ServerAlias that matches the host header in your request. If no vhost is explicitly matched then the first vhost defined will be served.

With the above in mind, the host header for all of your requests will be test.test so that matches your first vhost and apache httpd will attempt to serve the rest of the url from the relevant Documentroot.

You could try setting the document root for the first vhost one level higher. You could try to do something with mod_rewrite or even reverse proxies.

0

You cannot have several virtualhost as you describe because they all have the same name and port.

Why not set your DocumentRoot and Directory to /var/www/vhosts/ ?

Here having several virtualhost is not relevant.

0

It seems that you don't set your VirtualHost properly.

If you want to have site1 redirecting to /var/www/vhosts/site2 your virtualhosts should look like this:

<VirtualHost *hostnamesite1*:80>
  ServerName test.test
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site1/
  <Directory /var/www/vhosts/site1/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>

<VirtualHost *hostnamesite2*:80>
  ServerName test.test
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site2/
  <Directory /var/www/vhosts/site2/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>
0

You can't do it whith same domain name.

VirtualHost *:80

this part is for interface that will be used for test.test. for example if you have two interface 10.0.0.2 and 10.0.0.3

Virtualhost 10.0.0.2:80

point apache to listen test.test. only from 10.0.0.2.

ServerName test.test

this point your apache to get hostname. so if you put in browser test.test an apache get request for it he will give your right answer from your DocumentRoot

So i think you need sub-domains like

site1.yourdomain.com

and

site2.yourdomain.com

then your conf files must bee like this

<VirtualHost *:80>
  ServerName site1.yourdomain.com
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site1/
  <Directory /var/www/vhosts/site1/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>

and

<VirtualHost *:80>
  ServerName site2.yourdomain.com
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/vhosts/site2/
  <Directory /var/www/vhosts/site2/>
    AllowOverride None
  </Directory>
  ErrorLog /var/log/apache2/error.log
</VirtualHost>

UPDATE

Didn't see this part

site 1

hxxp://test.test/site2 shall point to /var/www/vhosts/site2/(index.html)

hxxp://test.test/site3 should point to somewhere else.

So you need Alias and define location for this alias in your case Alias is /site2 and DocumentRoot is /var/www/vhosts/site2/

Best regards and sorry for mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.