Apache virtualhost configuration - linux

I am tring to set two virtual host (example.com.conf and test.com.conf):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
and
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
The problem is that if I go on localhost in my browser it is shown the website www.example.com. If I type localhost/test.com there is error not found. My goal should be to address both website with localhost/example.com and localhost/test.com.

Sounds like you could use the ServerPath directive for apache, in your case something like this should work:
<VirtualHost 127.0.0.1>
# primary vhost
DocumentRoot "/var/www/example.com"
RewriteEngine On
RewriteRule "." "/var/www/example.com/public_html"
# ...
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "/var/www/example.com/public_html"
ServerName localhost
ServerPath "/example/"
RewriteEngine On
RewriteRule "^(/sub1/.*)" "/var/www/example$1"
# ...
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "/var/www/test.com/public_html"
ServerName localhost
ServerPath "/test/"
RewriteEngine On
RewriteRule "^(/sub2/.*)" "/var/www/test$1"
# ...
</VirtualHost>
The first Vhost would be so that localhost defaults to example.com page.

if you want to browse to these folders under any virtual host, like http://localhost/test.com, then you simply need an alias directive inside a location tag appended to the end of any active virtual host
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Location "/example.com">
Alias "/var/www/example.com/public_html"
</Location>
<Location "/test.com">
Alias "/var/www/test.com/public_html"
</Location>
Or you can put it inside the virtual host if you don't want it available anywhere else
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Location "/example.com">
Alias "/var/www/example.com/public_html"
</Location>
<Location "/test.com">
Alias "/var/www/test.com/public_html"
</Location>
</VirtualHost>

Related

apache2 configuring two domains in same server

I am trying to configure two domains in one server. My environment is
My domain 1 is : www.streetview.live
My domain 2 is : www.riverview.live
Ubuntu 20.04
PHP 7
apache2
In my /etc/apache2/sites-enabled/000-default.conf I have the created the links for both the domains one after the other.
First for www.streetview.live
<VirtualHost *:80>
ServerName www.streetview.live
ServerAlias streetview.live
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/streetview
<Directory /var/www/html/streetview>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName www.streetview.live
ServerAlias streetview.live
DocumentRoot /var/www/html/streetview
SSLEngine on
SSLCertificateFile /etc/ssl/certs/streetview/www_streetview_live.crt
SSLCertificateKeyFile /etc/ssl/certs/streetview/www_streetview_live.key
SSLCertificateChainFile /etc/ssl/certs/streetview/www_streetview_live.ca-bundle
</VirtualHost>
Followed by www.riverview.live
<VirtualHost *:80>
ServerName www.riverview.live
ServerAlias riverview.live
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/riverview
<Directory /var/www/html/riverview>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName www.riverview.live
ServerAlias riverview.live
DocumentRoot /var/www/html/riverview
SSLEngine on
SSLCertificateFile /etc/ssl/certs/riverview/www_riverview_live.crt
SSLCertificateKeyFile /etc/ssl/certs/riverview/www_riverview_live.key
SSLCertificateChainFile /etc/ssl/certs/riverview/www_riverview_live.ca-bundle
</VirtualHost>
I have loaded all the files in the path /var/www/html/. I have one folder for streetview.live and another for riverview.live as follows
/var/www/html/streetview/
/var/www/html/riverview/
In the DNS server I have mapped the IP to www.streetview.live and similarly for the other site.
However, When I load the page, I face two problems.
The respective sites open only when I use www.streetview.live/streetview on the browser and similarly for riverview.live, I have to use www.riverview.live/riverview`.
For both the https is not getting enabled.
The server is hosted in AWS and I do have the ports opened in Security Group.

Apache ProxyPass not loading Resources

I configured apache proxypass and it's working but not loading images, javascript, CSS etc... I want to proxypass to another server, not localhost. Below is my configuration.
see error image
<VirtualHost *:80>
ServerName app.server.com
DocumentRoot /var/www/html/subdomain
RewriteEngine on
ProxyRequests Off
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass /apm http://192.168.1.102:9999/
ProxyPassReverse /apm http://192.168.1.102:9999/
</virtualHost>
After some research and reading some tutorials I got a solution.
<VirtualHost *:80>
ServerName app.server.com
DocumentRoot /var/www/html/subdomain
RewriteEngine on
ProxyRequests Off
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass / http://192.168.1.102:9999/
ProxyPassReverse / http://192.168.1.102:9999/
</VirtualHost>

Hosting multiple subdomains on a different Apache server

We host our website on a Microsoft server. For reasons I'm not going to explain here, we decided to host 3 subdomains on an Ubuntu 16.04 server.
My question is how to configure the virtual hosts on Apache for this.
I have (I think) all the info I need here.
My doubt is: As ServerName I'm going to write the subdomain (see example below), right? Basically, I'm going to treat the subdomains as if I had 3 different websites.
/etc/apache2/sites-available/sudomain1.example.com.conf
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName sudomain1.example.com
DocumentRoot /var/www/sudomain1.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/sites-available/sudomain2.example.com.conf
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName sudomain2.example.com
DocumentRoot /var/www/sudomain2.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/sites-available/sudomain3.example.com.conf
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName sudomain3.example.com
DocumentRoot /var/www/sudomain3.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Multiple domain with the same ip and port in apache

I want to bind two different domain in my VPS with the same ip and port, here is my httpd.conf:
<VirtualHost 106.187.96.123:80>
DocumentRoot /home/roy/sobuhu
ServerName aaa.com
</VirtualHost>
<VirtualHost 106.187.96.123:80>
DocumentRoot /disk1/allen/www
ServerName bbb.com
</VirtualHost>
<VirtualHost 106.187.96.123:80>
DocumentRoot /disk1/allen/www
ServerName www.bbb.com
</VirtualHost>
Can I config the ServerName use syntax like *.bbb.com ?
so I can access www.bbb.com、bbs.bbb.com with the DocumentRoot /disk1/allen/www.
Now I visit bbs.bbb.com, it will turn to /home/roy/sobuhu.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /home/roy/sobuhu
ServerName aaa.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /disk1/allen/www
ServerName bbb.com
ServerAlias *.bbb.com
</VirtualHost>

How to fix this virtual host setup?

I have setup up 2 virtual hosts that share the same IP on a centos server running apache 2.
#<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/a
ServerName www.a.com
ServerAlias a.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/b
ServerName www.b.com
ServerAlias b.com
</VirtualHost>
Regardless of what URL I point to. Site A shows up.
How can I fix? I should have a.com going to a and b.com going to b.
Thanks all
Is the following set in your httpd.conf?
NameVirtualHost *
An example from the documentation:
NameVirtualHost *
<VirtualHost *>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
make sure you have this directive as well
NameVirtualHost *
it could be displaying site A because it's set as the default document root. check your global settings, make sure the document root is not set to the same root as site A.
I have always declared the domain as the name.. never had issue.
NameVirtualHost localhost:80
<VirtualHost localhost:80>
DocumentRoot "/home/eddie/workspace/"
</VirtualHost>
<VirtualHost digbiz.localhost:80>
DocumentRoot "/home/eddie/workspace/Digital_Business/app/webroot"
</VirtualHost>
<VirtualHost wishlist.localhost:80>
DocumentRoot "/home/eddie/workspace/WishList/app/webroot"
</VirtualHost>
<VirtualHost phpmyadmin.localhost:80>
DocumentRoot "/srv/www/phpMyAdmin"
</VirtualHost>
<VirtualHost test.localhost:80>
DocumentRoot "/home/eddie/workspace/CakePHPTesting/app/webroot"
</VirtualHost>
<VirtualHost auth.localhost:80>
DocumentRoot "/home/eddie/workspace/EntMeetCapt/app/webroot"
</VirtualHost>

Resources