Table of Contents
Running multiple websites on different ports using Apache Virtual Hosts is a common setup for development, testing, or running multiple applications on a single server without extra IP addresses.
Step 1: Make Sure Apache Is Installed and Running
Before configuring virtual hosts, ensure Apache is installed and running properly on your system so port-based hosting will work correctly.
sudo apachectl status
If Apache is not installed:
sudo apt install apache2 -y
Step 2: Allow Apache to Listen on Multiple Ports
Apache must be configured to listen on the ports you want to use, otherwise virtual hosts on those ports will not respond.
sudo nano /etc/apache2/ports.conf
Add your custom ports (example: 8080 and 8081):
Listen 8080
Listen 8081
Step 3: Create Virtual Host Configuration Files
Each port should have its own virtual host file to keep configurations clean and easy to manage.Create the first virtual host file.
sudo nano /etc/apache2/sites-available/site8080.conf
Example configuration:
<VirtualHost *:8080>
ServerName site8080.local
DocumentRoot /var/www/site8080
</VirtualHost>
Step 4: Create a Second Virtual Host for Another Port
At this stage, you need to define a new Apache virtual host that listens on a different port, allowing you to run another website or application independently on the same server in a clean and SEO-friendly setup.
sudo nano /etc/apache2/sites-available/site2.conf
Then add a virtual host configuration for the new port (for example, 8081):
<VirtualHost *:8081>
ServerName site2.local
DocumentRoot /var/www/site2
<Directory /var/www/site2>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/site2_error.log
CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>
Step 5: Create Website Directories
Apache needs physical directories to serve content for each virtual host, otherwise you’ll get permission or 404 errors.
sudo mkdir -p /var/www/site8080 /var/www/site8081
Set correct permissions:
sudo chown -R www-data:www-data /var/www/site8080 /var/www/site8081
Step 6: Enable the Virtual Hosts
After creating the configuration files, enable them so Apache can load the new port-based virtual hosts.
sudo a2ensite site8080.conf
sudo a2ensite site8081.conf
Step 7: Restart Apache to Apply Changes
After updating the virtual host and port settings, restarting Apache is required to apply the new configuration and ensure the server starts listening on the newly defined ports correctly.
sudo systemctl restart apache2
Step 8: Test Virtual Hosts on Different Ports
Now you can access each site using its port number directly in your browser to confirm everything works.If you are using a remote server, replace localhost with your server IP.
http://localhost:8080
http://localhost:8081
Step 9: Allow Ports in Firewall (If Needed)
To make the new Apache ports accessible from outside the server, you may need to allow them through the system firewall to prevent blocked connections.
sudo ufw allow 8080
Allow the new Apache port through the firewall:
sudo ufw allow 8081
