Search My Techie Guy

Wednesday, October 4, 2017

How to make HTTPD (Apache) service to listen on multiple port numbers - Linux?


Summary:

In this post, we are going to make our HTTPD service listen on multiple port numbers, for example 80, 8080, 443, and 9008

Problem or Goal:

Our aim is to make our HTTPD service (Apache) to listen on multiple service port numbers 80, 8080, 443, 9008

Cause:

In so many applications, you will find yourself with a requirement to make your HTTPD service to listen on multiple port numbers, it could be for security reasons or just trying to fine tune or load balance your server. 

Solution:

Before we change the configuration, let's run a simple test to check which ports are open? and which ones are not!

Testing port 80, OK
# wget localhost:80
--2017-10-04 16:08:11--  http://localhost/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.13”

100%[=====================================================================================================================================================>] 5,404       --.-K/s   in 0s      

2017-10-04 16:08:11 (293 MB/s) - “index.html.13” saved [5404/5404]

Testing port 8080, Not OK
# wget localhost:8080
--2017-10-04 15:24:52--  http://localhost:8080/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8080... failed: Connection refused.
Connecting to localhost|127.0.0.1|:8080... failed: Connection refused.

Testing port 443, Not OK
# wget localhost:443
--2017-10-04 15:25:00--  http://localhost:443/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:443... failed: Connection refused.
Connecting to localhost|127.0.0.1|:443... failed: Connection refused.

Testing port 9008, Not OK
# wget localhost:9008
--2017-10-04 15:25:08--  http://localhost:9008/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:9008... failed: Connection refused.
Connecting to localhost|127.0.0.1|:9008... failed: Connection refused.

Now let us make the server listen on all the above ports that tested "Not Ok"

Login into your server as user "root" or make sure you have "sudo" privileges!
Access the "httpd.conf" file located under "/etc/httpd/conf" and edit it to include the new port numbers.
see screen shoots below:

Before:

After editing:

Remember to restart your HTTPD service:
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]


Problem Solved?

Let's repeat the connection tests:

Testing port 80, OK
# wget localhost:80
--2017-10-04 16:08:11--  http://localhost/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.13”

100%[=====================================================================================================================================================>] 5,404       --.-K/s   in 0s      

2017-10-04 16:08:11 (293 MB/s) - “index.html.13” saved [5404/5404]

Testing port 8080, OK
# wget localhost:8080
--2017-10-04 16:08:19--  http://localhost:8080/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.14”

100%[=====================================================================================================================================================>] 5,404       --.-K/s   in 0s      

2017-10-04 16:08:19 (330 MB/s) - “index.html.14” saved [5404/5404]

Testing port 443,  OK
# wget localhost:443
--2017-10-04 16:08:23--  http://localhost:443/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.15”

100%[=====================================================================================================================================================>] 5,404       --.-K/s   in 0s      

2017-10-04 16:08:23 (382 MB/s) - “index.html.15” saved [5404/5404]

Testing port 9008, OK
# wget localhost:9008
--2017-10-04 16:08:27--  http://localhost:9008/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:9008... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.16”

100%[=====================================================================================================================================================>] 5,404       --.-K/s   in 0s      

2017-10-04 16:08:27 (353 MB/s) - “index.html.16” saved [5404/5404]

Our server can successfully listen on ports 80, 8080, 443 and 9008

No comments: