Avatar of rafaelrgl
rafaelrgl
 asked on

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

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.
Installation* virtual hostWordPressApache Web Server

Avatar of undefined
Last Comment
skullnobrains

8/22/2022 - Mon
Julian Hansen

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

skullnobrains

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.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
skullnobrains

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Julian Hansen

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

skullnobrains

@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