How to change ServerName in apache server? - apache

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.

Related

Apache2 and Vagrant: localhost mapping

I'm using Vagrant 1.6.3 for running Ubuntu 12.04 virtual machine. On this virtual machine I have Apache 2.4.9. For a long time Apache served only one site- there was 000-default.conf file in the /sites-available directory.
Now I want to add another site. I've created 2 files in /sites-available directory:
site1.local.conf
site2.local.conf
and I renamed 000-default.conf to default. Also I added site1.local and site2.local hosts to the ./etc/hosts file on the main machine.
Server names in Apache configs are set to site1.local and site2.local correspondently. And when I input http://site1.local:8080/ and http://site2.local:8080/ in browser everything works.
But I don't understand why http://localhost:8080/ still points to the first site? In what config should I define mapping for "localhost"? After setting up server names in apache configs I thought that http://localhost:8080/ should return "Can not connect to server" error.
Assuming you're using a name-based virtual host, site1 and site2 work because the client passes the target host in the header:
If the client provided a Host: header field the list is searched for a matching vhost and the first hit on a ServerName or ServerAlias is taken and the request is served from that vhost.
With localhost, it doesn't know how to match against the defined virtual host entries:
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 (as already mentioned before).
As far as the last part of your question, you could play around with adding another virtual host entry with localhost as the name definition.

define subdomain apache's vhost with lighttpd

Apache is runnig my server with port 80 and lighttpd with 81.
I defined vhost abc.com in apache.
I want to define track.abc.com and all request goes to lighttpd. Is is possible to define track subdomain for abc.com in lighttpd?
It is possible but rather than ports you need to have 2 separate IP addresses if you want to make regular request http://www.domainname.com in your browser. Otherwise you will be forced to call http://www.domainname.com if you intend to use the same IP address for both Apache and Lighttpd server.
You need to declare listening port in both Apache config and Lighttpd otherwise they both would try to bind IP address and port 80 which will result in error and only first server would start up.
I research and answer is Apache's ProxyPass option. First I configured my subdomain on Apache's vhost. and I added my subdomain's chost config file
ProxyPass / http://my_host_name:81/
Now, all my subdomains request goes to lighttp.

why can't use 443 in httpd.conf?

If I use 443 in httpd.conf and want to start the httpd, the error message is:
(98)Address already in use: make_sock: could not bind to address [::]:443
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:443
no listening sockets available, shutting down
Unable to open logs
Actually I don't use 443, I check the port of 443 by:
lsof -i:443
I think the port of 443 is used in ssl.conf, so I can't use it in httpd.conf.
When I use 444 or 666 in the httpd.conf, I can start the httpd.
This is the reason?
Without looking a closer look, yes, that looks like the reason. In the conf.d dir, the default setup is to load all files that end in .conf. ssl.conf sets some universal settings, and then defines a vhost on port 443.
my suggestion is:
copy the ssl.conf to ssl.conf.bk (or whatever, just so you have the original for reference)
Then edit the vhost in ssl.conf to suit your needs.
ps:
Let me back up and explain the conf.d dir just a little in case some reader is confused. Many projects, (not just Apache) use these dirs as a way to have a modular configuration file setup. An admin can just drop a conf file in the correct dir, and apache loads it the next time the service reloads. I use a configuration manager that drops the correct files on the correct servers for me, making it real easy to spin up more servers as needed.
pps:
Let me back up again and explain a vhost (aka 'virtualhost'). the Apache project has made their web server flexible enough to host multiple domains. Stick with me here. I can put an apache server on the internet, and point dns records for both www.foo.com and www.bar.com at my IP address, and apache is smart enough to produce different web pages for each. This is what the vhosts are for. the thing is that you are not doing that. Each vhost is a combination of a host name, and a port. the default vhosts are defined like this:
<VirtualHost _default_:443>
or
<VirtualHost *:443>
and these are catch-alls. So if you want http traffic, use the vhost you already have in httpd.conf, or if you want https traffic, use the one in ssl.conf. No need to get fancy if you are trying to just get'r done.
And good luck!

do i need an apache servername setup?

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

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.