How to Bind apache with two ports? - apache

I am having to servers giving services in two different ports 8080, 2379
how to configure apache to listen to the above ports and forwards the request to available server?

To tell Apache it should listen on specific port(s) add the Listen directive in the configuration (e.g. /etc/httpd/conf/httpd.conf). It can be specified multiple times, as in:
Listen 8080
Listen 2379
Details on this directive can be found here.
This will serve the content from DocumentRoot on these ports. If you want a different configuration on each port, you need to take a look at VirtualHost directive, which essentially allows you to host multiple websites on the same socket (IP/port).

Related

Varnish and additional ports

How can I add another port to Apache, and keep Varnish listening on port 80 only? The other new port would have a different document root.
Using Apache's sites-available configuration files you can specify what port the configured name should resolve on.

Apache seems to break when listening on multiple port/IP combinations

EDIT: To clarify a bit, I am running Apache 2.2.11
I'm setting up a development server which would only listen on a specific port/IP 10.0.10.4:80. But I potentially see issues with my current configuration of my live server as it listens on all IP's. These are two separate Apache instances with their own httpd.conf file.
I currently have my live apache instance listening on all IP's for 3 specific ports, Ex:
Listen *:8081
NameVirtualHost *:8081
Listen *:8080
NameVirtualHost *:8080
Listen *:80
NameVirtualHost *:80
I have multiple Virtual hosts running on my live server with definitions of:
<VirtualHost *:8081></VirtualHost>
<VirtualHost *:8080></VirtualHost>
<VirtualHost *:80></VirtualHost>
I would like to restrict the IPs for the live server to 3 IPs and 3 ports (9 total) so that there is not chance the live server could be served when trying to request the development server. The live server listens on 3 IPs for redundancy. NAT translates the public IP to one of the three.
10.0.10.1:8081
10.0.10.1:8080
10.0.10.1:80
10.0.10.2:8081
10.0.10.2:8080
10.0.10.2:80
10.0.10.3:8081
10.0.10.3:8080
10.0.10.31:80
The reason the two servers are running as separate Apache instances is so that I can start/stop/reset/crash them independently.
When I set the live server to use the combination of IPs/Ports with their own Listen and NameVirtualHost lines the Apache instance with not start up again. I've read that the declaration has to match the NameVirtualHost value, but I tried to lists multiple IP/Port pairs in the declaration with no luck. However, I forget if the Apache instance started up but didn't display the sites, or if it didn't start up at all (I'd rather not take down the live sites again in the middle of the day to find out).
Considering that the router NATs the public IP to any one of the three - you can try Mixed port-based and ip-based virtual hosts configuration for your live server and also for your development server. Good Luck!

how to access phpmyadmin only the port 8080 in apache

I've tried
vim /etc/phpmyadmin/apache.conf
but I can not put a VirtualHost here.
I want to change this configuration to list only the port 8080, can anyone help?
thank you
what I want is:
www.site.com/phpmyadmin -> failure
www.site.com:8080/phpmyadmin -> OK
I want to leave access to port 80 for the rest of the site.
You can change the Listen directive to 8080.
Search the apache configuration for Listen and change it from
Listen 80
to
Listen 8080
And restart the server. Bear in mind, this will be global to the whole apache server though. On centos or redhat, it'll be called "httpd.conf"
Usually the phpmayadmin configuration is included for all the Virtualhosts, that's a package installation behavior, and that's quite bad.
The file /etc/phpmyadmin/apache.conf is included from the main configuration (sometimes from a file in /etc/apache2/conf.d/phpmyadmin.conf).
Thoe first thing you could do is remove this main-all-virtualhosts-inclusion and only include this file with the Include keyword in one Virtualhost.
This allows two things, first you could use a dedicated ServerName for this host. Second you can alter the Port of the Virtualhost (or you can just do one of theses things).
Check this previous answer about IP/Name Virtualhosts, it will help you figure how Virtualhosts works. The Solution for you is to:
forbid the phpmyadmin configuration inclusion on the main-general-shared configuration level
Listen on both port 80 and 8080
Declare two NameVirtualHost, one on *:80 one on *:8080
Use a Virtualhost *:80 for classical application/websites, ServerName: www.site.com, ensure phpmyadmin configuration file is not included
Use a Virtualhost *:8080 including the phpmyadmin configuration, ServerName: www.site.com

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!

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.