If you need to serve different web applications from the same server using Apache, setting up multiple ports with separate directories is an efficient way to manage your server resources. In this guide, we'll walk you through the process of configuring Apache to listen on multiple ports, each serving content from different directories.
First, make sure that Apache is installed on your server. You can install it using the following commands:
sudo apt-get update
sudo apt-get install apache2
Next, enable the mod_alias
and mod_proxy
modules, which are necessary for the configuration:
sudo a2enmod alias
sudo a2enmod proxy
Edit the ports.conf
file to instruct Apache to listen on multiple ports, such as 8080 and 8081:
sudo nano /etc/apache2/ports.conf
Add the following lines to the file:
Listen 8080
Listen 8081
Create separate virtual host configuration files for each port and directory.
sudo nano /etc/apache2/sites-available/8080.conf
Add the following configuration:
<VirtualHost *:8080>
DocumentRoot /var/www/site1
<Directory /var/www/site1>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error8080.log
CustomLog ${APACHE_LOG_DIR}/access8080.log combined
</VirtualHost>
sudo nano /etc/apache2/sites-available/8081.conf
Add the following configuration:
<VirtualHost *:8081>
DocumentRoot /var/www/site2
<Directory /var/www/site2>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error8081.log
CustomLog ${APACHE_LOG_DIR}/access8081.log combined
</VirtualHost>
Create the directories for each site and add some test files:
sudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2
echo "<h1>Site 1 on Port 8080</h1>" | sudo tee /var/www/site1/index.html
echo "<h1>Site 2 on Port 8081</h1>" | sudo tee /var/www/site2/index.html
Enable the virtual hosts you just created:
sudo a2ensite 8080.conf
sudo a2ensite 8081.conf
Finally, restart Apache to apply all the changes:
sudo systemctl restart apache2
You should now be able to access your sites via the configured ports:
Each port will serve content from its respective directory, providing a clean and organized way to manage multiple web applications on a single server.
Published By: Krishanu Jadiya
Updated at: 2024-08-15 01:15:26