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.
Related
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".
On my webserver, I want to serve several websites just based on domain name.
For example, I want a webserver that will serve "mycoolsite.com" and "badstuff.org".
I pointed both sites at the same IP address. In httpd-vhosts, I made two entries:
<VirtualHost *:80>
DocumentRoot /www/mycoolsite
ServerName www.mycoolsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/badstuff
ServerName www.badstuff.org
</VirtualHost>
But when I go to badstuff.org, I get served mycoolsite.com! Why is this happening?
Apache Doc
I also know that when I use MAMP, I have to adjust my "hosts" file too. Is this relevant?
Have you added the NameVirtualHost directive before the vhosts declaration ?
NameVirtualHost *:80
<VirtualHost *:80>
...
</VirtualHost>
You may want to check your configuration by command:
$/usr/local/apache2/bin/httpd -S
If everything is OK, try to check again by restarting the server.
I'm having some issue with setting/adding sub domains.. to my apache2(httpd)..
I have this in my httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin stitofte#homiecraft.pro
DocumentRoot /var/www/html/HomieCraft
ServerName www.homiecraft.pro
ServerAlias homiecraft.pro
</VirtualHost>
<VirtualHost *:80>
ServerName stats.homiecraft.pro
ServerAlias www.stats.homiecraft.pro
ServerAdmin stitofte#homiecraft.pro
DocumentRoot /var/www/html/stats
</VirtualHost>
homiecraft.pro works just fine...
but stats.homiecraft.pro doesn't work at all.... I have made a DNS a record.. And made "stats" point to the listening address... But still it doesn't work...
Any help would be nice... I have had this setup before... But for some reason i can't get it to work this time...
In Apache2 the Virtual Hosts must be add in ../apache2/conf/httpd-vhosts.conf File instead of main configuration file i.e ../apache2/conf/httpd.conf
then reboot the apache2 servie i.e ../apache2/bin/apa[tab] -k restart.
Now It will work fine.
Normally the problem is in the Dns record, check the subdomain add at dns record and check the type, which is sets CNAME and the main domain the type is A.
And check too the ip direction the subdomain
Change the serverAlias at subdomains quit the www.
This has probably been asked but I can't find a straight answer, or the ones I found don't work.
I have one domain mydomain.com, resolving to an IP; let's call it 8.8.8.8. The DNS settings also point two subdomains to that IP address with an A record. These are dev.mydomain.com and staging.mydomain.com. Both have an A-record pointing to 8.8.8.8.
On the server (8.8.8.8) I have two virtual hosts files. These are as follows:
staging.mydomain.com.conf
<VirtualHost *:80>
ServerName staging.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
And...
dev.mydomain.com.conf
<VirtualHost *:80>
ServerName dev.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
The problem is:
Regardless of whether I visit http://staging.mydomain.com or http://dev.mydomain.com, I always land on staging.mydomain.com (Apache serves these files).
I have restarted Apache and even the server. If I change the order of the .conf files so that dev is first, I always see that. Any suggestions would be so appreciated. Thanks!
update
I find myself back at this problem again! If you know that your syntax is correct, you might have a bad symlink. Delete it and recreate again, restarting apache in-between. I just did this and it solved hours of head-scratching. On CentOS you can check your available vhosts with httpd -S
update 2
I've also found this issue to exist when the apache log files for the virtual host don't exist, or aren't writable.
Sounds like you need to add NameVirtualHost directive to your configuration.
NameVirtualHost *:80
Under some circumstances Apache may not be able to handle *:80 VirtualHosts correctly. In those cases you should map VirtualHosts directly on specific IPs.
NameVirtualHost 8.8.8.8:80
<VirtualHost 8.8.8.8:80>
ServerName staging.mydomain.com
ServerAlias stage.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
<VirtualHost 8.8.8.8:80>
ServerName dev.mydomain.com
ServerAlias development.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
You can also run apachectl -t -D DUMP_VHOSTS to see how Apache parses the VirtualHost configuration.
Update: As mentioned in the comments, usually you can just use NameVirtualHost *:80. So most of the time you can configure the virtual hosts as follows.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName staging.mydomain.com
ServerAlias stage.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
<VirtualHost *:80>
ServerName dev.mydomain.com
ServerAlias development.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
OK. I have LAMP server on my local development machine. I need to develop few sites on it. So, I've created two files (configs of my sites) in $APACHE/sites-available and made symlinks for them for /sites-enabled. After that I've started sudo /etc/init.d/apache2 restart. No errors or warnings. When I try mysite.dev it gives it, and when foobar.dev - it gives mysite.dev!
So, apache gives one site for all virtual hosts. How to fix it?
http://pastebin.com/qjcx6RS3 (first site)
http://pastebin.com/FdVStJm8 (second site)
You'll need to set NameVirtualHost-directive like this in your httpd.conf:
NameVirtualHost *:80
Example with two vhosts (like in your case):
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
... it would of course also be possible to include the vhosts from seperate files into the httpd.conf.