How to configure virtual host in apache2 to have its own root folder

rafaelrgl
rafaelrgl used Ask the Experts™
on
Here is my question, I want to have 2 wordpress installation inside my VM.

folder structure:
/var/www/site1
/var/www/site2

url structure:
site1:    ipaddress/
site2:    ipaddress/site2

the problem I am facing is that I want site2 independent without access to site1, so when I put in on link inside site2 like this   /page1.html it should not go to ipaddress/page1.html, but it should go  to ipaddress/site2/page1.html So all resources would be block to current root of site2.

So how should be the config inside apache2. I am using alias, but it let all requests from site2 got to root of site 1. that can not happen.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If you are going to be using the IP address to access the sites then your only option is to use aliases i.e.

root
   /site1
     files
   /site2
     files

Open in new window

<IfModule alias_module>
  Alias /site1 "/home/site1"
  Alias /site2 "/home/site2"
  <Directory "/home">
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AllowOverride All
	Require all granted
  </Directory>  
</IfModule>

Open in new window


Alias one (site1) would be accessed by http://ipaddress/site1
Alias two (site2) would be accessed by http://ipaddress/site2

To have them on different urls you would need to setup different domain names for each - for instance you can do the following. Create the following DNS / Hosts entries
site1.local => ipaddress
site2.local => ipaddress 

Open in new window


Then setup your virtual hosts as

<VirtualHost *:80>
    DocumentRoot "/home/site1"
    ServerName site1.local
    ErrorLog "logs/site1.local-error.log"
    CustomLog "logs/site1.local-access.log" common
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/home/site2"
    ServerName site2.local
    ErrorLog "logs/site2.local-error.log"
    CustomLog "logs/site2.local-access.log" common
</VirtualHost>

Open in new window


Now you would access them as
http://site1.local
http://site2.local

Open in new window

and you'd need to set the base directory in your wordpress config to the proper one so you don't get redirected to the webserver's root. by default wordpress assumes it is installed at the webserver's root.

using named-based virtual hosting or different server ports for each site is much simpler.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
and you'd need to set the base directory in your wordpress config
We do that in the .htaccess
RewriteBase /site1
Or
RewriteBase /site2
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Should work as well providing wp uses the base html tag and relative links. Given that s you speaking i assume you checked or know
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
No base tag - links are absolute.
RewriteEngine On
RewriteBase /site1/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site1/index.php [L]

Open in new window

@jh
my bad. did not notice it acted at the rewrite level... interesting ( and slightly double-edged ) feature. thanks for pointing this out
best regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial