Serving two websites with Apache with single domain name and single IP address
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>开发者_如何学Python
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
精彩评论