How to Run Multiple Apache Ports on a Server with Multiple Directories

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.

Step 1: Install Apache

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

Step 2: Enable Necessary Apache Modules

Next, enable the mod_alias and mod_proxy modules, which are necessary for the configuration:

sudo a2enmod alias
sudo a2enmod proxy

Step 3: Configure Apache to Listen on Multiple Ports

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

Step 4: Set Up Virtual Hosts

Create separate virtual host configuration files for each port and directory.

For Port 8080:

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>

For Port 8081:

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>

Step 5: Create Directories for the Sites

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

Step 6: Enable the Virtual Hosts

Enable the virtual hosts you just created:

sudo a2ensite 8080.conf
sudo a2ensite 8081.conf

Step 7: Restart Apache

Finally, restart Apache to apply all the changes:

sudo systemctl restart apache2

Step 8: Access the Sites

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

Card Image

Ultimate Guide to Setting Up PHP Development Environment with Apache on Ubuntu 20.04

Comprehensive guide to setting up a PHP development environment using Apache on Ubuntu 20.04. Includes step-by-step instructions, installation of dependencies, SSL configuration, and setting up Laravel with Composer.

Card Image

Setup PHP Laravel Environment with Docker: Apache, Ubuntu, and MongoDB

Guide to setting up a PHP Laravel environment with Docker, including configuration for Apache, Ubuntu, and MongoDB.

Card Image

Setting Up CI/CD Pipeline for Laravel on GitLab with AWS EC2 Deployment

Guide to setting up a CI/CD pipeline for a Laravel project on GitLab, including deploying to an AWS EC2 instance and configuring SSH keys for remote access to a Git repository.

Card Image

Top 50 Docker Interview Questions and Answers

Prepare for your next DevOps interview with these top 50 Docker interview questions, designed to help you understand and master Docker's core concepts and practices.