How to change the port Apache

Apache HTTP server – one of the most used web servers on the Internet today. According to its characteristics and versatility, it surpasses its competitors (such as Nginx).

One of the most important functions of Apache is the ability to load and run different types of modules and configurations, without stopping the web server. In addition, no need to recompile the server, adding new modules. Play an important role .htaccess files that can change the configuration of the web server in specific directories. In this article we will discuss how to change the port of apache in Ubuntu and CentOS.

How to change the port Apache

The Apache server is configured by default to listen for incoming connections through port 80. If you configure the TLS Protocol, the server will listen to connections on port 443. To use other ports, you’ll need to tweak the Apache configuration file.

In the system of Debian/Ubuntu, edit the configuration file /etc/apache2/ports.conf, and the distros based on RHEL/CentOS – /etc/httpd/conf/httpd.conf. Depending on the installation package, open the file with a text editor and add a new port as follows:

sudo nano /etc/apache2/ports.conf

sudo nano /etc/httpd/conf/httpd.conf

In this example, we will configure Apache HTTP server to listen for connections on port 8081. Make sure that after the Listen 80 Directive, which gives instructions to the server to listen on port 80, were added the following line:

Listen 8081

After you have added the above line, you need to create or modify virtual hosts Apache to bind the host to the desired port. In the example below, we change the Apache port to the default virtual host the web server from 80 to 8081.

Need to open and edit the file /etc/apache2/sites-enabled/000-default.conf specifying the port 8081, as shown in the picture below.

sudo nano /etc/apache2/sites-enabled/000-default.conf

To apply the changes and the new ports become available to Apache, restart the service. After restarting the port 8081 should be displayed in the output of utilities ss or netstat. The commands for Ubuntu:

sudo systemctl restart apache2

sudo netstat-tlpn | grep apache

sudo ss-tlpn | grep apache

For CentOS:

sudo systemctl restart httpd

sudo netstat-tlpn | grep httpd

sudo ss-tlpn | grep httpd

Configuring ports apache is completed. You can now open a browser and log on to the web server by entering a domain name or IP address and port 8081. The Apache page should be displayed in the browser. However, if you can’t view the web page, return to the server console and verify that firewall rules are set correctly to allow network traffic on this port:


If you have enabled SELinux in CentOS or RHEL, install the policycoreutils package to add the necessary rules. And then restart Apache to apply the changes.

sudo yum install policycoreutils

To open the Apache port 8081 add SELinux rules.

sudo semanage port-a-t http_port_t -p tcp 8081

sudo semanage port-m-t http_port_t -p tcp 8081

sudo systemctl restart httpd

Open a browser and attempt to go to your server by IP address or domain name using port 8081. Verify the health of the port. The browser should display the Apache page.


If you are still unable to access this address, make sure that this port allowed in the firewall of your server.


(Visited 21 times, 1 visits today)