We help IT Professionals succeed at work.

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

197 Views
Last Modified: 2018-02-06
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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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

CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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

CERTIFIED EXPERT

Commented:
@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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions