Listen to Non-Standard Ports with Apache

This section provides a tutorial example on how to add virtual hosts to listen to additional ports other than standard ports (80 and 443) with and without systemd Socket Activation service.

By default, Apache HTTP Server is configured to use 2 standard TCP ports: 80 for HTTP connections and 443 for HTTP over SSL/TLS connections. And these 2 ports are managed by the Socket Activation module in the "systemd" program.

If you check the list of "systemd" service unit files using the "systemctl" command, you will see 2 services related to "httpd":

herong$ systemctl list-unit-files | grep -e "httpd\..*"

httpd.service                              enabled  
httpd.socket                               enabled 

The output of the "httpd.service" status confirms that it is listening on ports 80 and 443:

herong$ systemctl status httpd.service

* httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; ...)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
   Active: active (running) since Sat 2024-03-30 20:10:41
  Process: 736003 ExecReload=/usr/sbin/httpd $OPTIONS ...
... httpd[443337]: Server configured, listening on: port 443, port 80

The output of the "httpd.socket" status confirms that the Socket Activation module is managing ports 80 and 443:

herong$ systemctl status httpd.socket

* httpd.socket - Apache httpd Server Socket
   Loaded: loaded (/usr/lib/systemd/system/httpd.socket; enabled; ...)
  Drop-In: /usr/lib/systemd/system/httpd.socket.d
   Active: active (listening) since Mon 2024-03-25 08:31:39
   Listen: [::]:80 (Stream)
           [::]:443 (Stream)
    Tasks: 0 (limit: 48033)

Port 80 is defined in "httpd.socket" configuration file:

herong$ more /usr/lib/systemd/system/httpd.socket


Port 443 is defined in a drop-in file added by the "mod_ssl" package:

herong$ more /usr/lib/systemd/system/httpd.socket.d/10-listen443.conf 

# This file is part of mod_ssl. It enables listening on port 443 when
# socket activation is used.


Using Socket Activation module to manage TCP port sockets is more efficient than letting "httpd" to manage them directly. But it requires extra configuration to add additional ports.

For example, if you try to add a virtual host to listening on 8080 in an extra Apache HTTP server configuration file:

herong$ sudo vi /etc/httpd/conf.d/extra.conf

Listen 8080

<VirtualHost _default_:8080>
  DocumentRoot /var/www/extra

You will see the following error when re-starting the server:

herong$ sudo systemctl restart httpd.service
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.

herong$ systemctl status httpd.service
AH00526: Syntax error on line 1 of /etc/httpd/conf.d/extra.conf:
Systemd socket activation is used, but this port is not configured in systemd

To fix this error, you need to add your own drop-in file to define port 8080, in the same way as the "mod_ssl" package. And restart the "httpd.socket" and "httpd.service" services:

herong$ sudo vi /usr/lib/systemd/system/httpd.socket.d/20-extra.conf 


herong$ sudo systemctl restart httpd.socket 
herong$ sudo systemctl restart httpd.service

If managing httpd ports with Socket Activation gives you too much trouble, you can disable it, and let httpd to manage them directly:

herong$ sudo systemctl stop httpd.service

herong$ sudo systemctl stop httpd.socket 
herong$ sudo systemctl disable httpd.socket 

herong$ sudo systemctl start httpd.service

Without using Socket Activation, you can add any additional ports to Apache HTTP Server directly.

Table of Contents

 About This Book

 Introduction to Linux Systems

 Process Management

 Files and Directories

Running Apache HTTP Server (httpd) on Linux Systems

 What Is Apache HTTP Server "httpd"

 Install Apache HTTP Server "httpd"

 Enable Remote Access to "httpd" Service

 Publish Home Page index.html

 "apachectl status/start/stop" Commands

 Verify Apache HTTP Server "httpd" Environment

 Requirements for Supporting HTTPS on Apache

 Web Server Certificate and Required Fields

 Apache Configuration for HTTPS Protocol

 Common Issues with Apache HTTPS Support

Listen to Non-Standard Ports with Apache

 Running Apache Tomcat on Linux Systems

 Running PHP Scripts on Linux Systems

 Running MySQL Database Server on Linux Systems

 Running Python Scripts on Linux Systems

 Conda - Environment and Package Manager

 GCC - C/C++ Compiler

 OpenJDK - Open-Source JDK

 Graphics Environments on Linux

 SquirrelMail - Webmail in PHP

 Tools and Utilities


 Full Version in PDF/EPUB