Search My Techie Guy

Wednesday, October 4, 2017

How to change the HTTPD (Apache) listening port from the default port 80 to another port number - Linux?

Summary:

In this post i successfully changed the listen port of HTTPD (Apache) service from the default port 80 to port 9090
OS: Linux (Oracle Enterprise Linux/Redhat Enterprise Linux)

Problem or Goal:

To change the HTTPD listening port from the default port 80 to another port say 9090

Cause:

For security reasons, sometimes it's wise to change your server's HTTPD listening port from the default 80 to another unique port number. There are many more other reasons why you might find yourself with this requirement.

Solution:

First a simple command to test the listening port of your HTTPD service. 
Login to your server and run this command

 # wget localhost:80

Expected out put:

 --2017-10-04 14:12:35--  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.10”

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

2017-10-04 14:12:35 (316 MB/s) - “index.html.10” saved [5404/5404]

The output above indicates that our HTTPD service is listening on 80 (we successfully connected to port 80)

Now use the same command to test connection to port 9090

 # wget localhost:9090

Output:

 --2017-10-04 14:25:16--  http://localhost:9090/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:9090... failed: Connection refused.
Connecting to localhost|127.0.0.1|:9090... failed: Connection refused.

The above output shows that our HTTPD service is not running on port 9090 (connection was refused).

Let us now change that.
Login again into your linux box, as user root or make sure you have sudo privileges.

Edit the file called "httpd.conf" located under "/etc/httpd/conf"

# cd /etc/httpd/conf
# vi httpd.conf

Before:

Change the port number to 9090:

Now restart the HTTPD service:
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]


Problem Solved?

Let's test again with our wget command:

 [root@~]# wget localhost:9090
--2017-10-04 14:38:05--  http://localhost:9090/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:9090... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5404 (5.3K) [text/html]
Saving to: “index.html.11”

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

2017-10-04 14:38:05 (285 MB/s) - “index.html.11” saved [5404/5404]

We have successfully connected to port 9090 for http service :-)

No comments: