Skip to main content

How to setup Virtual Hosts on Apache

James Ensor

Virtual hosts allow you to host multiple websites on a single server using the same IP address. This is a common practice for web developers and system administrators. Here’s a breakdown of the steps involved:

Prerequisites

  • An Apache web server installed and running.
  • Administrative access to the server (root or sudo privileges).
  • Domain names pointing to your server’s IP address (you can use your local machine’s hosts file for testing purposes).

Steps

Create Directory Structure

  • Decide on a directory structure for your websites’ content. A common approach is to have a public_html directory under the document root (usually /var/www/html). You can create subdirectories within public_html for each virtual host.
sudo mkdir -p /var/www/html/your_domain_1 sudo
mkdir -p /var/www/html/your_domain_2

Grant Permissions

  • Set the appropriate ownership and permissions for the created directories. The web server user (usually www-data on Ubuntu/Debian) should have read and execute permissions.
sudo chown -R www-data:www-data /var/www/html/your_domain_1
sudo chown -R www-data:www-data /var/www/html/your_domain_2
sudo chmod -R 755 /var/www/html/your_domain_1
sudo chmod -R 755 /var/www/html/your_domain_2

Create Default Pages (Optional):

  • Create basic index files (e.g., index.html) for each virtual host directory to display a message when someone visits the domain.

Create Virtual Host Files:

  • Apache uses configuration files to define virtual hosts. These files reside in the /etc/apache2/sites-available/ directory.
  • Use a text editor (e.g., nano) to create a new virtual host file for each domain (e.g., your_domain_1.conf).
sudo nano /etc/apache2/sites-available/your_domain_1.conf

Configure Virtual Host Files:

  • Add the following configuration to the virtual host files, replacing your_domain_1 with your actual domain name and adjusting the DocumentRoot directive to point to the corresponding directory created in step 1.

<VirtualHost *:80> ServerName your_domain_1 ServerAlias www.your_domain_1 (optional) DocumentRoot /var/www/html/your_domain_1 <Directory /var/www/html/your_domain_1> Require all granted </Directory> </VirtualHost>

  • The ServerName directive specifies the primary domain name for the virtual host.
  • The ServerAlias directive (optional) allows you to define additional aliases for the domain.
  • The DocumentRoot directive specifies the directory containing the website’s content.
  • The <Directory> block sets permissions for the document root directory.

Enable Virtual Hosts and Restart Apache

  • Enable the newly created virtual host files using the a2ensite command followed by the filename (without the .conf extension).
sudo a2ensite your_domain_1.conf

Restart Apache for the changes to take effect. The specific command to restart Apache may vary depending on your Linux distribution. Here are some common examples:

sudo systemctl restart apache2 (

for

Ubuntu/Debian)

sudo apachectl restart (older distributions)

(Optional) Localhost Testing:

If you’re working on your local machine, you can edit your hosts file (usually located at /etc/hosts) to point your domain names to your server’s IP address (e.g., 127.0.0.1 your_domain_1). This allows you to test your virtual hosts without having to configure external DNS settings.

Testing

Once you’ve restarted Apache, open a web browser and navigate to your domain names. You should see the default pages you created in step 3 or your website’s content if you uploaded it to the respective directory.

Additional Tips

You can create more complex virtual host configurations to define things like custom error documents, logging, and security.

Choose 5wire for Superior Cloud Server Performance, Reseller Hosting Flexibility, and Reliable Forex Servers. Scale your business effortlessly with 5wire’s cloud server solutions, flexible reseller hosting packages, and dependable forex servers.