do i need an apache servername setup? - apache

I'm no really sure if SO is the place to ask this question - let me know if not. I thought about superuser, but it didn't seem to fit into their faq specs much.
Anyway - the question! Do i need to put in the ServerName value in my apache2.conf file if i'm just using the server to service a single website? If so, what should i set it to?
I'm following this guide:
http://meppum.com/2009/jan/17/installing-django-ubuntu-intrepid/
and it suggests i set the "SeverName" setting in my apache2.conf file. I checked about and found this article, that discusses "ServerName"
http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:Ch20:_The_Apache_Web_Server
Which is interesting and all, but it doesn't really tell me when i need to set the ServerName. 'Far as i can judge, i only need it when i'm doing some sort of something.domainname.com, like, say, answers.yahoo.com
Is this the case? If not, what is servername (for, say, a domain called www.yahoo.com at, say, 192.0.0.1) meant to be?

If the server is hosting a single website, you don't have to use VirtualHosts, so you don't have to define ServerName, however it's recommended to do it.
If you don't do it, Apache will try to find out the hostname based on the server IP performing a reverse DNS lookup.
From the Apache documentation:
The ServerName directive sets the request scheme, hostname and port
that the server uses to identify itself. This is used when creating
redirection URLs.
Additionally, ServerName is used (possibly in conjunction with
ServerAlias) to uniquely identify a virtual host, when using
name-based virtual hosts.
For example, if the name of the machine hosting the web server is
simple.example.com, but the machine also has the DNS alias
www.example.com and you wish the web server to be so identified, the
following directive should be used:
ServerName www.example.com
ServerName www.example.com If no ServerName is specified, then the
server attempts to deduce the hostname by performing a reverse lookup
on the IP address. If no port is specified in the ServerName, then the
server will use the port from the incoming request. For optimal
reliability and predictability, you should specify an explicit
hostname and port using the ServerName directive.
Answering your questions:
Do i need to put in the ServerName value in my apache2.conf file if
i'm just using the server to service a single website?
No, you don't, but it's recommended.
If so, what should i set it to?
The syntax is : ServerName [scheme://]fully-qualified-domain-name[:port]
Is this the case?
No, not only.
If not, what is servername (for, say, a domain called www.yahoo.com at, say, 192.0.0.1) meant to be?
ServerName www.yahoo.com

Related

404 Not Found The requested URL /xxxxx was not found on this server

Please i need help, I have gone through a couple of similar issues and how they were resolved and it seems mine is such a pain it just wont work.
I used a Turnkey Linux Appliance [OrangeHRM] and i want to use its Apache server to host another site/software [Sentrifugo] and it is so difficult, each time i host it only the default site shows up.
i have read through several similar issues here on stack overflow and it didn't work, i need someone to please take a look at my case.
Sincere appreciations in anticipation
Below is the output of grep "<VirtualHost" /etc/apache2/sites-enabled/*
/etc/apache2/sites-enabled/adminer.conf:<VirtualHost *:12322>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:80>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:443>
/etc/apache2/sites-enabled/sentrifugo2.conf:<VirtualHost *>
Good morning, based on what we discussed:
you have 4 VH (VirtualHost): *:12322, *:443, *:80, *
*:12322 is not a default port, it would have to be specified explicitly (http://SOMETHING:12322/...)
*:443: default SSL port (https://SOMETHING/...)
*:80: default http port (http://SOMETHING/...)
*: matches all ports
Now you are using http://IP/sentrifugo2/...
Using the IP address, Apache will check the VH in order and use the first one that matches.
If you ask for http://IP/..., since this is port 80 (default for http), Apache will match the third VH (the *:80 one).
But here you want the 4th one, the one with *
Solutions:
Works, but is not the best: define your 4th VirtualHost with a port. <VirtualHost *:80>. Then modify the order in which your VH appear in Apache and put the sentrifugo VH before the orangehrm.conf one. But that could create a new problem where the orangehrm.conf is not usable anymore... Keep reading...
Best: use name virtual hosting:
<VirtualHost *:80>
ServerName orangehrm.conf
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
<VirtualHost *:80>
ServerName sentrifugo
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
So you see, this way, Apache can diffentiate between the two *:80, using the requested domain (or name). On your client system, in /etc/hosts, define the two names:
10.0.0.18 orangehrm.conf
10.0.0.18 snetrifugo
So when you type http://orangehrm.conf or http://sentrifugo Apache will know which one you want.
Note: on Apache 2.4, this works no problem. On Apache 2.2, you need to specify NameVirtualHost directive (see Apache documentation on this).
Again works but not the best: might not be possible for you. You can split between the VH using a different IP address. Ex. 10.0.0.18 is orangehrm.conf and <VirtualHost 10.0.0.18:80> is used. Then 10.0.0.19 is sentrifugo, so <VirtualHost 10.0.0.19:80> is used (with Listen 10.0.0.19:80). But you must be allowed to configure extra IP addresses on your host for this...
another possibility: split on the port. <VirtualHost *:80> is for orangehrm.com, <VirtualHost *:81> is for sentrifugo. For this, you need to specify http://10.0.0.18:81/sentrifugo. This also requires Listen *:80 and Listen *:81.
In conclusion Apache needs a way to know which one you want. It can decide with: IP, Port, Name. Name is the most flexible.
IMHO using IP address is not the way to go. It works in simple setups, but quickly limits you. Use domain names. Here I showed you how to do it in your local host file, the "best-best" way to do is to set them up in DNS.

How to change ServerName in apache server?

I downloaded Apache binary from http://www.apachelounge.com/ After installing, in the httpd.conf file on line 220, I changed #ServerName www.example.com:80 to ServerName www.example.com:80. Now everything works fine. The server users the domain name localhost. Also localhost:80 automatically redirects to localhost.
Since ServerName is www.example.com:80 then why does the server work on localhost instead of www.example.com:80?
If I change every instance of www.example.com:80 to mylocalserver:80 then why doesn't the apache server work on mylocalserver:80?
DNS as in name resolution happens before you reach Apache HTTPD Server. When you put a name in your browser or anywhere, that you reach your server or not depends only on that resolution and if resolves the ip of the server you have configured, so it has nothing do be with how httpd is configured that you reach it with one name, and you don't with another.
As for httpd, it works on any name you may want because HTTPD does not know about your DNS setup. It listens on a IP address and if a request reaches the server (through the ip:port it is binded to) then and only then it will check the "Host" http header inside the request to decide to which virtualhost (if more than one and it has been configured properly) to deliver the request.
So you can use any name you like, what matter is how you resolve it and on which ip:port combination you end up.
Your question doesn't give a whole lot of information, but I'll try to answer it anyway.
Also localhost:80 automatically redirects to localhost.
That's probably not a redirect, but your browser removing the :80 part as it's the default port on the web.
Since ServerName is www.example.com:80 then why does the server work on localhost instead of www.example.com:80?
Probably because both localhost and www.example.com refer to the web server, and the web server responds to both of them. localhost is commonly configured to be 127.0.0.1 and thus will refer to your local set-up.
If I change every instance of www.example.com:80 to mylocalserver:80 then why doesn't the apache server work on mylocalserver:80?
mylocalserver might not be referring to anything. Try editing your hosts file (/etc/hosts on *nix, %SYSTEM%\Drivers\etc\hosts) to include mylocalserver to refer it to 127.0.0.1 just like localhost.
The problems you're having seem to stem from a misunderstanding about the domain names. Domain names translate into IP addresses. www.example.com translates to some IP address on the internet, but localhost translates into 127.0.0.1 usually, like defined in the hosts file. You may also just use the IP address in the ServerName variable, such as 127.0.0.1.

Why should we create A DNS record for subdomains when we can do it via httpd.conf?

I want to implement a blog in my site with usernames as subdomains like user1.domain.com.
I've read bunch of articles that was mentioned to create an A record like below in DNS:
*.domain.com A 127.0.0.1 ;OR you site ip address
Then use .htaccess to redirect requests from subdomains to a folder in your server.
But I find another approach that was said to just modify your httpd.conf as below:
<VirtualHost *:80>
DocumentRoot /abs/path/to/webroot
ServerName domainname.com
ServerAlias *.domainname.com
<Directory /abs/path/to/webroot>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Then retrieve subdomains via $_SERVER['HTTP_HOST'].
The question is why should someone use htaccess and put pressure on apache and create A DNS record when we can do a simple thing like ServerAlias *.domainname.com in httpd.conf?
I appreciate in advance for your views.
You need to setup both a wildcard DNS record and configure a server alias in httpd.conf to enable the hosting of arbitrary sub domains. They configure completely different things.
Add the wildcard DNS records is what allows clients to determine the IP address of your sub domain. With the wildcard DNS record about user1.domain.com will resolve to 127.0.0.1. If you do not have a DNS record no user1.domain.com will not be a valid name and it will not resolve to an IP address. the DNS record is to allow clients to connect to your server.
The httpd.conf configuration is local to your Apache server. It does not affect clients. The line ServerAlias *.domainname.com will configure Apache to tell it to process the arbitrary sub domains. Client's will not be directly affected by this setting.
You have added the ServerAlias, but for your computer to find that user1.domain.com and user2.domain.com should resolve to 127.0.0.1, you need to update your HOSTS file or DNS Table.
The configuration you have written is for Apache Server for handling the redirects when the user requests the domain. So, essentially, either you need a Dynamic HOSTS file or a service which resolves *.domain.com to your 127.0.0.1 server.
Hope this helps. :)

I want Apache only to listen to port 80 on the addresses I specify. Can I?

I have a bunch of domains pointing to one IP address (I have a feeling this will be the main thing stopping this from working) and one of them I would like to point to a node.js server on port 80 instead of having to faff about with proxies in Apache.
I've already removed the virtualhost for *:80 in apache (it warns me of this every time I restart it).
When I ask node to listen to domain.one:80 though (just an example, not what I'm really using), it doesn't work - it tells me the address is in use even though there's no VirtualHost for domain.one:80.
I suspect it's to do with the fact that domain.one and domain.two both point to the same IP, right? If not, what the heck can I do? Thanks :)
Ports are directly tied to an IP address. Each IP address can only have one process listening to a given port. Since your domain names all resolve to the same IP address you cannot have a separate node process for each one listening on port 80.
If you wish to keep this scheme, you'll need to have each node server listen on a different port and configure reverse proxies in Apache as described here. You can also consider using nginx as it also has this capability (configured slightly differently).
Yes. You can specify a servername in the vhost. Then you can only specify an IP or * in the tag. Create a *:80 Vhost and add
<VirtualHost *:80>
ServerName domain.one
DocumentRoot /blah/blah
....
</VirtualHost>
to it. That will filter by the domain name.

limit apache virtualhost to one domain?

I have configuration of virtualhost:
<VirtualHost xx.xx.xx.xxx>
ServerName mydomain.com
ServerAlias *
...
</VirtualHost>
everything works fine on mydomain.com. But the problem is that someone linked his domain to my application ip and I want to avoid this. Is it possible to limit virtualhost to only one domain "mydomain.com" - so if somebody will point his domain to my ip - virtualhost will not process this request?
There's no way to prevent it from answering HTTP requests on that IP address. I think that if you were to use name based hosting and then create a vhost with ServerName 1.2.3.4 then it'd be matched for requests to the IP address and you'd be able to add some special behaviour there (return an error code, redirect to the named site, etc).
If you just want to make sure that people don't bookmark the IP address and always see mydomain.com in the browser then have a look at the section on Canonical Hostnames in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html