Skip to content

How to Change Port Number in Apache

Advertisement

Apache, the world’s most widely used web server software, offers flexibility and a robust set of features. One common task that administrators often encounter is changing the server’s port number. This article provides a detailed, step-by-step guide to help you successfully change the port number in Apache.

Introduction

The default HTTP port for Apache is 80, and for HTTPS, it’s 443. However, there are various reasons you might need to change these, such as running multiple servers on one machine or avoiding conflicts with other applications. This guide assumes a basic understanding of Apache and its configuration files.

Step-by-Step Guide to Changing the Port Number in Apache

Step 1: Access the Apache Configuration File

The first step involves accessing the Apache main configuration file, typically named httpd.conf or apache2.conf. The location of this file varies depending on the operating system and Apache version:

  • On RHEL-based Systems: /etc/httpd/conf/httpd.conf
  • On Debian-based Systems: /etc/apache2/ports.conf
  • For Windows users, it’s often located in the conf directory within the Apache installation directory.

Step 2: Locate the Listen Directive

Within the configuration file, look for lines starting with the Listen directive. This directive tells Apache on which IP addresses and ports it should listen for requests. By default, you might see something like:


Listen 80

Step 3: Change the Port Number

To change the port number, simply edit the number after the Listen directive. For instance, if you want Apache to listen on port 8080, modify the line to:


Listen 8080

If you’re running Apache with SSL/TLS and want to change the HTTPS port, change Listen 443 in configuration file as well as in Virtual hosts.

Step 4: Check for Virtual Hosts Settings

If your Apache server uses virtual hosts, you’ll also need to change the port number in each <VirtualHost> block in the configuration files. The line usually looks like this:


<VirtualHost *:80>

Change 80 to your new port number.

After making the changes, save the configuration file. Ensure you do not unintentionally change any other settings.

Step 5: Restart Apache

For the changes to take effect, restart the Apache server. This can be done via command line. On Unix/Linux systems, the command is often:

sudo systemctl restart apache2 

On Windows, you can restart Apache through the Apache Service Monitor or by using the httpd.exe -k restart command in the command prompt.

Step 6: Test the New Configuration

Open your web browser and try accessing your site using the new port number, for example, http://your_system_ip:8080. If everything is configured correctly, your site should load.

Changing Apache Default Port
Changing Apache Default Port

Conclusion

Changing the port number in Apache is a straightforward process, but it requires careful attention to detail. Always remember to back up your configuration files before making any changes. This precaution allows you to restore the original settings in case something goes wrong.

By following these steps, you can customize your Apache installation to suit your specific needs and maintain a smoothly running web server.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. hello,
    i can’t .i ‘ve this error ,after this command :”alexandre@alexandre-developpeur:~$ sudo systemctl restart apache2
    Job for apache2.service failed because the control process exited with error code.
    See “systemctl status apache2.service” an”

    and :”alexandre@alexandre-developpeur:~$ sudo cat /etc/apache2/ports.conf
    # If you just change the port or add more ports here, you will likely also
    # have to change the VirtualHost statement in
    # /etc/apache2/sites-enabled/000-default.conf
    Listen 80
    Listen 8081

    Listen 443

    Listen 443

    alexandre@alexandre-developpeur:~$ sudo cat /etc/apache2/sites-enabled/000-default.conf

    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request’s Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName http://www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, …, trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    #ErrorLog ${APACHE_LOG_DIR}/error.log
    #CustomLog ${APACHE_LOG_DIR}/access.log combined
    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with “a2disconf”.
    #Include conf-available/serve-cgi-bin.conf

    alexandre@alexandre-developpeur:~$
    “

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top