12

I'm trying to host my website on single server running centos7.1. I'm able to virtualhost on port 80 but when I change port of second-site to 8080 it points to first-site. However, for URL http://62.210.xx.xx:8080/ its working correctly, pointing to second-site.

I am doing this way:

httpd.conf

Listen 80
Listen 8080

also tried 
Listen 0.0.0.0:80
Listen 0.0.0.0:8080
but same

commented out for apache 2.4
#NameVirtualHost 62.210.xx.xx:80
#NameVirtualHost 62.210.xx.xx:8080

ServerAdmin root@localhost

#first-site.com
<VirtualHost 62.210.xx.xx:80>
ServerName first-site.com
ServerAlias www.first-site.com
DocumentRoot /var/www/first-site
</VirtualHost>

#second-site.com
<VirtualHost 62.210.xx.xx:8080>
ServerName second-site.com
ServerAlias www.second-site.com
DocumentRoot /var/www/second-site
</VirtualHost>


netstat -tulpn | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      25253/httpd
tcp6       0      0 :::8080                 :::*                    LISTEN      25253/httpd


/etc/host/
62.210.xx.xx www.first-site.com first-site.com www.second-site.com second-site.com

/usr/sbin/httpd -t
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/httpd/conf/httpd.conf:46
Syntax OK

Update

apachectl -S
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, 
using 62.210.xx.xx. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
62.210.xx.xx:8080      m.second-site.com (/etc/httpd/conf/httpd.conf:427)
62.210.xx.xx:80        is a NameVirtualHost
     default server first-site.com (/etc/httpd/conf/httpd.conf:363)
     port 80 namevhost first-site.com (/etc/httpd/conf/httpd.conf:363)
             alias www.first-site.com
     port 80 namevhost image.first-site.com (/etc/httpd/conf/httpd.conf:3                                                                             77)
     port 80 namevhost m.first-site.com (/etc/httpd/conf/httpd.conf:389)
     port 80 namevhost second-site.com (/etc/httpd/conf/httpd.conf:401)
             alias www.second-site.com
     port 80 namevhost image.second-site.com (/etc/httpd/conf/httpd.conf:415)
     port 80 namevhost third-site.com (/etc/httpd/conf/httpd.conf:439)
             alias www.third-site.com
     port 80 namevhost image.third-site.com (/etc/httpd/conf/httpd.conf:453)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex fcgid-pipe: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
Mutex fcgid-proctbl: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="xxxxxxx" id=1000
Group: name="root" id=0
Zanon
  • 233
  • 1
  • 2
  • 13
Tall boY
  • 221
  • 1
  • 3
  • 13
  • What happend if you try `62.210.xx.xx:8080` address directly ? – Froggiz Dec 01 '15 at 11:06
  • You said : `it points to first virtual host` in your original post...so it point on first virtual host when you try www.second-site.com ? Do you have a firewall which could block some ports ? – Froggiz Dec 01 '15 at 13:54
  • yes, I have firewall turned on, I have added port 8080 to public before making virtualhost. – Tall boY Dec 01 '15 at 13:57
  • What is the result of the command `nc -zv 62.210.xx.xx 8080` ? – Froggiz Dec 01 '15 at 14:11
  • What if you change `62.210.xx.xx:8080` and `62.210.xx.xx:80` to `*:8080` and `*:80`. Also, `nc` can be `netstat`. There are several different netstat. Install one and try to open the port. – Halfgaar Dec 03 '15 at 11:00
  • @Halfgaar I have tried with `*` in place of ip same result pointing to first site, I think the issue is with ipv6 as apache is listening on tcp6 – Tall boY Dec 03 '15 at 11:02
  • Try adding the port to the `ServerName` directive: `ServerName www.second-site.com:8080`, `ServerName www.first-site.com:80`. That solved problems for me in the past. – Gerald Schneider Dec 03 '15 at 11:16
  • @GeraldSchneider I tried that too but didn't work – Tall boY Dec 03 '15 at 11:20
  • @Froggiz `62.210.xx.xx:8080` is pointing to sendsite but url pointing to firstsite – Tall boY Dec 03 '15 at 12:08
  • @TallboY, if you are not using IPV6, then try changing `Listen 80 Listen 8080` to `Listen 0.0.0.0:80 Listen 0.0.0.0:8080` and also remove the `NameVirtualHost 62.210.xx.xx:80 NameVirtualHost 62.210.xx.xx:8080` lines, as they are deprecated. – Diamond Dec 03 '15 at 12:54
  • @bangal doing this has no effect – Tall boY Dec 03 '15 at 13:17

5 Answers5

6

Try making the Listen statement match your VirtualHost.

If you want to only bind to the public IP 62.210.xx.xx, use it everywhere.

Listen 62.210.xx.xx:80
Listen 62.210.xx.xx:8080

<VirtualHost 62.210.xx.xx:80>
  ServerName first-site.com
  ServerAlias www.first-site.com
  DocumentRoot /var/www/first-site
</VirtualHost>

<VirtualHost 62.210.xx.xx:8080>
  ServerName second-site.com
  ServerAlias www.second-site.com
  DocumentRoot /var/www/second-site
</VirtualHost>

--or--

If you want to bind to all interfaces on that server, use wildcards.

Listen 80
Listen 8080

<VirtualHost *:80>
  ServerName first-site.com
  ServerAlias www.first-site.com
  DocumentRoot /var/www/first-site
</VirtualHost>

<VirtualHost *:8080>
  ServerName second-site.com
  ServerAlias www.second-site.com
  DocumentRoot /var/www/second-site
</VirtualHost>

Essentially, the Listen statement tells apache to bind to that IP/port. Specifying 0.0.0.0:80 would only listen on ipv4 addresses. Specifying just the port number would bind to all interfaces on port 80.

Similarly, when SYN packets come in, the VirtualHost statement attempts to match based on the Listen statements.

TheCompWiz
  • 7,409
  • 17
  • 23
  • Also, if the server is behind a NAT, don't use the public IP. Use the IP that is physically assigned to the server. – TheCompWiz Dec 04 '15 at 06:22
  • Tried both ways, Still not working with url `www.second-site.com` pointing to `www.first-site.com` working with http://62.210.xx.xx:8080 – Tall boY Dec 04 '15 at 07:24
  • Based on your output of apachectl -S, are you sure you don't have other "Listen" statements? Also, have you tried setting up a "default" site that goes to a 404 page? – TheCompWiz Dec 04 '15 at 07:57
  • 1
    Default site is `first-site.com`, there are no other `Listen` statements, Also why do I need to set up a "default" site that goes to a 404 page – Tall boY Dec 04 '15 at 09:35
4

You need to decide whether you're using name based virtualhosts or IP/port based ones. You've currently got the two mixed up together.

If you're going to put servers on their own ports, then you should not define NameVirtualHost for those ports. When you do define that for the host and port that the request arrives on, apache choses the virtalhost configuration to use on the basis of the servername and serveraliases.

I'm not clear on whether Apache will be prepared to choose a virtualhost on the basis of the domain name when that virtualhost block is defined using a different IP/port. What you are describing sounds like that might be what's happening.

In your /etc/hosts file you specify first-site.com and second-site.com. Neither of these are defined virtual hosts, since for those you use the www. prefix. If you are using name based virtualhostsYou presumably want to add ServerAlias directives to your virtualhost blocks. Meanwhile, your opening paragraph uses an unhelpful shorthand for the sites you're refering to - you'd be better off changing that to reference the exact URL(s) you are having trouble with.

mc0e
  • 5,866
  • 18
  • 31
  • As per your instructions I have added `www` prifix in host file and `ServerAlias` in virtualhost block and removed `NameVirtualHost`, but still problem is same second site pointing to first. – Tall boY Dec 03 '15 at 14:12
  • Can you show me an example for proper name based virtual host on different ports. – Tall boY Dec 03 '15 at 14:48
  • Name based virtual hosting doesn't require you to use different ports. Port based virtualhosts don't require the `NameVirtualHost` directive. There aren't many good reasons to use port based virtual hosting these days. – mc0e Dec 03 '15 at 17:01
3

What you are experiencing is the default behavior as per the Apache documentation.

enter image description here

Any request to an address other than 172.20.30.50 will be served from the main server. A request to 172.20.30.50 with an unknown hostname, or no Host: header, will be served from www.example.com.

Therefore, to be able to reach the second-site.com virtualhost, you have to do it through http://second-site.com:8080/ .

Make sure that you include these domain names in your local hosts file (e.g. /etc/hosts , C:\Windows\System32\drivers\etc\hosts , etc.) if you haven't configured DNS yet. If this is not an option, you can also set up a dynamic hostname with DynDNS or other provider, and use it for your ServerName/ServerAlias.

Inigo Flores
  • 211
  • 2
  • 6
  • Not working with `http://second-site.com:8080` only working with `ip:8080`, Also all the domains are included in local `/etc/hosts`. – Tall boY Dec 07 '15 at 09:35
  • Have you set your `ServerName` to **second-site.com** in your `VirtualHost` definition? – Inigo Flores Dec 07 '15 at 10:10
  • Yes I have set servername and serveralias as described in question under virtualhost defination – Tall boY Dec 07 '15 at 10:17
  • ok, now its working with `http://second-site.com:8080`, but I dont want to access website this way, I want to access website like `http://second-site.com` – Tall boY Dec 08 '15 at 13:17
  • Just set the second virtual host to listen in port 80, as the first one. – Inigo Flores Dec 08 '15 at 15:43
1

Is 62.210.xx.xx the IP of your server or a NAT IP before your server ? You must have the real IP of the server on the VirtualHost, and the one by which the request is comming if you have many IP on the server.

mick
  • 735
  • 6
  • 7
  • how can I find real IP of server? `ifconfig` gives me this `inet 62.210.xx.xx netmask 255.255.255.0 broadcast 62.210.xx.255` and `hostname -i ::1 27.0.0.1` – Tall boY Dec 05 '15 at 04:54
1

As Inigo Flores already pointed out, this is default apache behavior.

By typing second-site.com in your browser, it will always connect to port 80. If you want to access a different port, you need to specify it in the url like second-site.com:8080

If you want to access your site like second-site.com, there is no point in using different ports.

fetch101
  • 88
  • 6
  • Ok, I got it now, port based hosting works with domain.com:port type url not with domain.com, I was under the impression that I can host on different ports with domain.com type url, thanks for making it clear. This makes all answers correct, but I am going to accept your's as It made me clear what works how. Thanks – Tall boY Dec 08 '15 at 15:25
  • I find baffling that this answer was accepted instead of mine. – Inigo Flores Dec 08 '15 at 15:53
  • @InigoFlores Users can accept any answer they wish, even wrong answers. – Michael Hampton Dec 08 '15 at 16:57