Latest news from 5wire Networks

How to setup Virtual Hosts on Apache

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
 

(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

25% DISCOUNT FOR LIFE

Life time discount when you purchase your first order. Applies to Web Hosting, Reseller Hosting, and Cloud Servers.

Free Domain included on yearly Web Hosting plans.

Use code WEBOFF at the checkout!

Terms and Conditions apply. Voucher code is only valid for new customers.