I open up a port, let's say port 81 to listen to incoming requests.
If the incoming request is www.myexample.com, then I want to redirect it to
C:\myexamplemain
folder.
If the incoming request is blog.myexample.com, then I want to redirect it to
C:\myexampleblog
folder.
Given that there are a lot of redirection rules for www.myexample.com and blog.myexample.com, I have to create a separate VirtualHost files for these two. So I need a separate configuration files that resolves the DocumentRoot. How to best do this?
The best way to do this is via virtual hosts.
NameVirtualHost *:81
<VirtualHost *:81>
DocumentRoot C:\myexamplemain
ServerName www.myexample.com
</VirtualHost>
<VirtualHost *:81>
DocumentRoot C:\myexampleblog
ServerName blog.myexample.com
</VirtualHost>
What file these are in doesn't matter. Apache processes its configuration as if it was all in one file. You can put one bit in one file and the other virtual host bit in another file and it's OK.
Related
I have two domain names that point to the same IP address. One of the domains is hosted locally on the server, the other is just a forward to another domain (we migrated our Confluence to the cloud, but need to keep old links alive)
My httpd.conf looks a little like this (a lot of, hopefully, irrelevant info removed).
Listen 80
Listen 443
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerName esd.domain.com
Redirect / https://esd.domain.com/
</VirtualHost>
<VirtualHost *:443>
ServerName esd.domain.com
DocumentRoot "C:/Apache/htdocs"
SSLEngine On
SSLCertificateFile "/Apache/conf/esd.domain.com.crt"
SSLCertificateKeyFile "/Apache/conf/esd.domain.com.key2"
</VirtualHost>
<VirtualHost *:80>
ServerName documentation.domain.com
Redirect "/display/" "https://domain.atlassian.net/wiki/display/"
Redirect "/" "https://domain.atlassian.net/"
</VirtualHost>
<VirtualHost *:443>
ServerName documentation.domain.com
Redirect "/display/" "https://domain.atlassian.net/wiki/display/"
Redirect "/" "https://domain.atlassian.net/"
</VirtualHost>
This works great for esd.domain.com, but documentation.domain.com ends up at esd.domain.com as well.
Does anyone have any pointers on how I should be doing this please? I'm an httpd novice who has inherited this system and struggling! Thanks in advance.
This page seems to suggest it's possible: https://docs.jelastic.com/name-based-apache-virtual-host
The only difference with mine is that the second domain is a redirect.
I found out that the issue wasn't with the .conf file, rather that
httpd -k restart
Wasn't actually restarting due to a permissions problem. It was reporting errors in the .conf file so gave the impression it was restarting, when in fact it wasn't able to shut down the running instance. In the end I found a small error in the log file that the service couldn't be stopped and sorted the permissions out.
Now the above .conf file works a charm.
I am using Apache 2.2 on CentOS. I have set the correct DNS records.
By default, my Apache is serving from /var/www/html without any VirtualHosts set up at all. I would like to serve something else, say /var/www/html/bakery, when a request for bakery.myfakesite.com comes in, but continue to serve the default folder for plain myfakesite.com requests.
To this end, I wrote in httpd.conf:
<VirtualHost *:80>
ServerName myfakesite.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName bakery.myfakesite.com
DocumentRoot /var/www/html/bakery
</VirtualHost>
The problem is that this results in a conflict, where only the first virtualhost is actually used. I've done something wrong, but I'm not quite sure what. What should I be doing instead?
There should be a
NameVirtualHost *:80
right before your vhost definitions
that means "hey, I want you to match requests comparing their http_host with the serverName in the following vhosts, instead of just sending everything to the first one that matches IP and port".
I have tried but nothing seems to work for me here. I have an Apache server with multiple domains set up. Currently they all point to the root folder (C:\xampp\htdocs) but I'd like to organize things a little better here on. I'd like to use Apache's VirtualHost to point each new domain to a subfolder inside this folder but have all older websites point to C:\xampp\htdocs in the mean time until I can migrate each.
The issue is that when I use the following code, all my domains point to the subfolder
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain\"
ServerName www.newdomain.com
</VirtualHost>
I need to add a Virtual that listens to all other domains and points them to C:\xampp\htdocs
But I'm running out of ideas. Please help
Try that:
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName _default_
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain"
ServerName www.newdomain.com
</VirtualHost>
You need a default virtualhost, that's the catch_all thing, when the Host Header defined in the HTTP query does not match any ServerName directive the default VH is useD. And the default VH is the first one defined in the configuration.
I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml
I am trying to host two websites using Apache from the same Ubuntu server. I have one ip address, and I only have one domain (which resolves to the ip address). So I want requests to the domain name to give one website, and requests to the ip address to give the other.
I have symlinks in /etc/apache2/sites-enabled to two files, pointing to the config for my two sites.
One contains:
<VirtualHost 1.2.3.4:80>
ServerName 1.2.3.4
stuff
</VirtualHost>
while the other contains
<VirtualHost domain.net:80>
ServerName domain.net
stuff
</VirtualHost>
However, when I start Apache, I get the following message:
[warn] VirtualHost 1.2.3.4:80 overlaps with VirtualHost domain.net:80, the first has precedence, perhaps you need a NameVirtualHost directive
and when I point my browser at either domain.net or 1.2.3.4 I get the site that I want associated with the ip address.
If I delete either symlink, then pointing a browser at either the domain name or the ip address gives the only enabled website. (As you would hope.)
As I understand it, both config files in sites-enabled are being loaded at once, and the one containing the ip address trumps the one containing the domain name. The warning suggests looking at the NameVirtualHost directive, but all the help I can find online refers to cases where you have two domain names pointing to the same ip address.
As always, and help or advice would be much appreciated.
(For what it's worth, the websites are both Rails applications, and I'm deploying using Passenger, but I don't think that's important here.)
This is how I do it:
NameVirtualHost 1.2.3.4:80
<VirtualHost 1.2.3.4:80>
ServerName localhost
DocumentRoot /var/www/default
</VirtualHost>
<VirtualHost 1.2.3.4:80>
ServerName mydomain.net
DocumentRoot /var/www/mydomain
</VirtualHost>
Apache looks for a suitable virtualhost for every request. If it doesn't find one that matches the ServerName or any of the ServerAliases then it takes the first one. It doesn't really matter what you use for the ServerName in the first VirtualHost as it will always be used if none of the other VirtualHosts match.
Had this problem, here is what I did:
Edit httpd.conf
sudo vi /etc/apache2/httpd.conf
Add this line
NameVirtualHost *:80
NOTE: You can replace *:80 with your_ip_address:80
Now create the domain name config file. I use the domain_name.com
sudo vi /etc/apache2/sites-available/domain.com
Add this to the file
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/domain.com/public_html/
ErrorLog /var/www/domain.com/logs/error.log
CustomLog /var/www/domain.com/logs/access.log combined
</VirtualHost>
Make sure the directories in the domain.com config exists
/var/www/domain.com/public_html/
/var/www/domain.com/logs
NOTE: use the mkdir command like this if needed
sudo mkdir /var/www/domain.com/public_html/
sudo mkdir /var/www/domain.com/logs
Now we need to enable the new config file like this
sudo a2ensite domain.com
You should see a notice to restart apache, use this command
/etc/init.d/apache2 restart
Now we need a test file to view
sudo vi /var/www/domain.com/public_html/index.html
Add some text
Hello domain.com
Open your web browser and go to your new domain
http://domain.com
Make sure you have the instruction
NameVirtualHost *:80
in /etc/apache2/ports.conf