How to Configure Apache for Multiple IP Addresses?
Configuring the Apache HTTP Server to listen on multiple IP addresses
allows you to host distinct websites on different network interfaces or
bind your server to specific IP addresses. This process involves
modifying the core Apache configuration file to include multiple
Listen directives and setting up corresponding
VirtualHost blocks to route traffic accurately. This guide
provides a straightforward, step-by-step walkthrough to successfully
bind Apache to multiple IPs on both Debian/Ubuntu and RHEL/Rocky Linux
systems.
Step 1: Identify Your Server’s IP Addresses
Before modifying any configuration files, you need to know the exact IP addresses available on your network interfaces. You can find these by running the following command in your terminal:
ip addr showLook for the lines starting with inet to identify the
specific IPv4 addresses (for example, 192.168.1.50 and
192.168.1.60) that you want Apache to use.
Step 2: Modify the Listen Directives
Apache determines which network interfaces and ports to monitor using
the Listen directive. You need to open your main Apache
configuration file to add your specific IP addresses.
- Ubuntu/Debian:
/etc/apache2/ports.conf(or/etc/apache2/apache2.conf) - RHEL/Rocky Linux/CentOS:
/etc/httpd/conf/httpd.conf
Open the file with a text editor like nano:
sudo nano /etc/apache2/ports.confBy default, you might see Listen 80, which tells Apache
to listen on port 80 across all available network interfaces. To
restrict Apache to specific IP addresses, replace or supplement that
line with explicit IP and port combinations:
Listen 192.168.1.50:80
Listen 192.168.1.60:80Step 3: Configure Virtual Hosts for Each IP
Once Apache is listening on the correct IPs, you must configure Virtual Hosts to tell the server how to handle requests coming into each specific address.
Open your Virtual Host configuration file (for example,
/etc/apache2/sites-available/000-default.conf on Ubuntu or
/etc/httpd/conf.d/vhosts.conf on RHEL) and define your
blocks like this:
<VirtualHost 192.168.1.50:80>
ServerName site1.local
DocumentRoot /var/www/site1
ErrorLog ${APACHE_LOG_DIR}/site1_error.log
CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>
<VirtualHost 192.168.1.60:80>
ServerName site2.local
DocumentRoot /var/www/site2
ErrorLog ${APACHE_LOG_DIR}/site2_error.log
CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>Each <VirtualHost> tag must explicitly match one
of the IP addresses specified in your Listen
directives.
Step 4: Test and Restart Apache
After saving your changes, it is critical to test the configuration syntax for errors before restarting the web server. Running a test prevents unexpected downtime.
Run the configuration test command:
sudo apache2ctl configtest(Note: Use sudo httpd -t on RHEL-based
systems.)
If the output reads Syntax OK, safely restart the Apache
service to apply the new IP bindings:
sudo systemctl restart apache2(Note: Use sudo systemctl restart httpd on
RHEL-based systems.)
Your Apache web server is now successfully configured to handle web traffic across multiple independent IP addresses.