How to attach my domain name to my server IP address (Apache) - apache

for example I have a domain name www.example1.com
and I have in my apache conf this code:
<VirtualHost 109.201.175.107:80>
DocumentRoot /home/localname/www
ServerName www.example1.com
# Other directives here
</VirtualHost>
in the virtual host I wrote my ip and then I attach it to the ServerName is that right?, but anyway when I open the www.example1.com it shows my an error 404, or maybe I need to change the ip address that is attached the domain name, can you tell me step by step what I need to do, I haved searched all the google but didn't understand.

<VirtualHost 109.201.175.107:80>
ServerName example1.com:80
ServerAlias www.example1.com
//other options
ServerAdmin Email Id
DocumentRoot /home/localname/www
# Other directives here
ErrorLog Physical path to error log folder
</VirtualHost>
Please Check this for details
http://bytes.com/serveradministration/webservers/apache/virtual-hosting/app/dynamic_static_router.html

Your domain name should be mapped with your server public Ip address, this is all required after the configuration that you have made. Ask your dns provider for this.
on ping yourdomainname.com on command prompt, it should resolve your public IP address (109.201.175.107)

Solved for Ubuntu 15.04, I found the appropriate solution and hope it will help you too.
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
ServerAlias *.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Source

Related

How to set up Apache with 3 sites: Two name based virtual hosts and one via the IP address

I have 2 sites linked to domains already.
For this I have 2 conf files into /etc/apache2/sites-available/:
<domain1>.conf
<domain2>.conf
With DocumentRoot /var/www/domain1 and DocumentRoot /var/www/domain2.
In addition, I need to setup the 3rd site direct linked to server IP.
For this I created conf file:
IP.conf (IP is IP of the server):
<VirtualHost *:80>
ServerAdmin <email>
ServerName <IP>
ServerAlias <IP>
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run:
sudo a2ensite <IP>.conf
And:
restart apache service.
But it doesn't help.
Could you advise how to configure routing?
The issue is the default Apache config always uses the IP address of the server. So that IP.conf will never be loaded.
You need to edit the default Apache config — located in sites-available — to make your changes.
Looking at your config, you are indicating the raw IP address for the ServerName and ServerAlias that will — effectively — defeat the purpose of setting up name based virtual hosts:
<VirtualHost *:80>
ServerAdmin <email>
ServerName <IP>
ServerAlias <IP>
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
What happens in a case like this is the configs will be ignored because the default Apache setup will always defer to the IP address of the machine you are on. Heck, it would even use all network interfaces if your server has multiple IP addresses.
For name based virtual hosting to work, you must use the domain/host name in your config. Something like this for domain1:
<VirtualHost *:80>
ServerAdmin <email>
ServerName <domain1>
ServerAlias <domain1>
DocumentRoot /var/www/domain1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And this for domain2
<VirtualHost *:80>
ServerAdmin <email>
ServerName <domain2>
ServerAlias <domain2>
DocumentRoot /var/www/domain2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now for the IP address host config, you should go into your Apache config directory — /etc/apache2/ on Debian/Ubuntu or /etc/httpd/ on CentOS/RedHat — and look inside the sites-available directory. There should be a file named 000-default.conf:
/etc/apache2/sites-available/000-default.conf
And make the changes you are showing in your IP.conf in there. At a most basic level just change the DocumentRoot to be this:
DocumentRoot /var/www/html/wordpress
Then restart Apache and it should be working as expected.

How to setup two virtual hosts in CentOS?

I have two domains I'm trying to host on the same IP. This is what I've been trying, but it doesn't work:
<VirtualHost host1.me:80>
ServerAdmin admin#menet.me
DocumentRoot /var/www/html/host1/
ServerName host1.me
ErrorLog logs/host1-error_log
CustomLog logs/host1-access_log common
</VirtualHost>
<VirtualHost host2.me:80>
ServerAdmin admin#menet.me
DocumentRoot /var/www/html/host2/
ServerName www.host2.me
ServerAlias host2.me
ErrorLog logs/host2-error_log
CustomLog logs/host2-access_log common
</VirtualHost>
What should I put in my httpd.conf?
if you are accessing the urls locally i.e. on server itself,
then u need to add their entries in /etc/hosts file.
eg: suppose ur server ip is 192.168.1.1, thn put following entry in hosts file:
192.168.1.1 host1.me host2.me www.host2.me
save the file...
and then check...
hope this helps :)

Editted Virtual Host ServerName in httpd.conf but it seems it did not take effect

So I have 4 Virtual Host. I made the 3 Virtual Hosts 2 days ago and it
all works fine. When I made another Virtual Host the next day, it
won't load up the newly created Virtual Host. So I tried renaming the Server Name of my 1st Virtual
Host which is running.
I changed the ServerName and Alias of www.test1.biboglobal.com to hogehoge.com with still the directory of test1. Saved the file and restarted apache. But when I tried to browse hogehoge.com,it won't load the contents.
I tried browsing the test1.biboglobal.com and it still worked. But
that Server Name does not exist anymore because I changed it to
hogehoge.com.
I've searched all about httpd.conf not taking changes but found no answer. I hope someone can help me.
I also tried removing all Virtual Hosts and restarted Apache. But still, it was able to browse test1, test2, test3.biboglobal.com. Although it only displayed the Apache Test Page, not the index.html inside their respective directory.
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerAdmin webmaster#hogehoge.com //**This was supposed to be test1.biboglobal.com**
DocumentRoot /var/www/test2.biboglobal.com/public_html
ServerName www.hogehoge.com //**This was supposed to be test1.biboglobal.com**
ServerAlias hogehoge.com //**This was supposed to be test1.biboglobal.com**
ErrorLog /var/www/test1.biboglobal.com/error.log
# CustomLog /var/www/test1.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test2.biboglobal.com
DocumentRoot /var/www/test2.biboglobal.com/public_html
ServerName www.test2.biboglobal.com
ServerAlias test2.biboglobal.com
ErrorLog /var/www/test2.biboglobal.com/error.log
# CustomLog /var/www/test2.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test3.biboglobal.com
DocumentRoot /var/www/test3.biboglobal.com/public_html
ServerName www.test3.biboglobal.com
ServerAlias test3.biboglobal.com
ErrorLog /var/www/test3.biboglobal.com/error.log
# CustomLog /var/www/test3.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test4.biboglobal.com
DocumentRoot /var/www/test4.biboglobal.com/public_html
ServerName www.test4.biboglobal.com
ServerAlias test4.biboglobal.com
ErrorLog /var/www/test4.biboglobal.com/error.log
CustomLog /var/www/test4.biboglobal.com/requests.log common
</VirtualHost>

How to configure apache to use ipv6 for subdomains

I have just setup a website using a domain like domain.com. And then setup another website sub.domain.com.
Now both of them can be visited via ipv4. And then I added the ipv6 address[a:b::c:d]. However, when I restarted httpd service, the following message appeared:
[warn] VirtualHost a:b::c:d:80 overlaps with VirtualHost a:b::c:d:80,
the first has precedence, perhaps you need a NameVirtualHost
directive.
I can now only visit domain.com via ipv6, not sub.domain.com. But, somehow, I can visit domain.com/sub via ipv6. I seems that ipv6 address cannot be treated like ipv4 address in apache. I am wondering how could I configure the apache to behave as what I wish.
I have the following lines in my httpd.conf file:
Listen 1.2.3.4:80
Listen [a:b::c:d]:80
<VirtualHost 1.2.3.4:80>
DocumentRoot /var/www/html
ServerName domain.com
ErrorLog "/var/www/logs/error.log"
CustomLog "/var/www/logs/access.log" common
</VirtualHost>
<VirtualHost [a:b::c:d]:80>
DocumentRoot /var/www/html
ServerName domain.com
ErrorLog "/var/www/logs/ipv6_error.log"
CustomLog "/var/www/logs/ipv6_access.log" common
</VirtualHost>
<VirtualHost 1.2.3.4:80>
DocumentRoot /var/www/html/sub
ServerName sub.domain.com
ErrorLog "/var/www/logs/sub_error.log"
CustomLog "/var/www/logs/sub_access.log" common
</VirtualHost>
<VirtualHost [a:b::c:d]:80>
DocumentRoot /var/www/html/sub
ServerName sub.domain.com
ErrorLog "/var/www/logs/sub6_error.log"
CustomLog "/var/www/logs/sub6_access.log" common
</VirtualHost>
Just as Jeremy Visser mentioned, I searched the config file and found the following line:
NameVirtualHost 1.2.3.4:80
It seems that this is the problem. Then I added this line:
NameVirtualHost [a:b::c:d]:80
The problem was settled!

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.