Hosting Multiple Websites On The Apache Web Server With A Single IP Address

If you need to host multiple websites on your Pi with a single IP address, then you'll probably have assigned different ports for each site. The steps below explain how to configure Apache so that it can host two or more websites.

At the Pi command line, edit the Apache configuration file /etc/apache2/ports.conf.

sudo nano /etc/apache2/ports.conf

For each different port, insert pairs of lines as shown below. In this example we're using ports 8080 and 8085:

NameVirtualHost *:8080

Listen 8080


NameVirtualHost *:8085

Listen 8085

The way that Apache is intended to work is that the configuration files for all the available websites are placed in /etc/apache2/sites-available. You then run the a2ensite script to create a symbolic link in the /etc/apache2/sites-enabled directory that points to a corresponding configuration file in /etc/apache2/sites-available directory. You disable an enabled website using the a2dissite script. When you have enabled or disabled websites, the changes are not effective immediately and you need to restart Apache so that it reads and loads all the enabled websites listed in /etc/apache2/sites-enabled.

At the Pi command line, create a new Apache configuration file for each website that you wish to host. If you wish to start with a basic template, use /etc/apache2/sites-available/default.

cd /etc/apache2/sites-available

sudo cp default my-new-website

sudo nano my-new-website

Change the port number of the VirtualHost keyword and save the changes.

VirtualHost *:8080

Now repeat this for each additional website.

To make these changes live, you need to restart Apache so that it reloads the new configuration. Note any error messages when Apache restarts and make corrections to the corresponding file.

sudo /etc/init.d/apache2 reload