Ugacomp

How to Configure Apache to host multiple websites

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Configuring Apache to host multiple websites involves creating Virtual Hosts, which are independent configurations for different websites on the same server. Here are the general steps to set up multiple websites on Apache:

RECOMMENDED READING: How to install Apache Server on Ubuntu Linux

Navigate to Apache Configuration Directory:

The Apache configuration directory contains the configuration files used by the Apache HTTP Server. The location of this directory can vary depending on the operating system and the Apache installation method. Here are common paths for the Apache configuration directory on different systems:

Ubuntu/Debian

Main Configuration Directory: /etc/apache2/

cd /etc/apache2/

You will need to go to Site-Specific Configuration: /etc/apache2/sites-available/

cd /etc/apache2/sites-available

CentOS/RHEL

In CentOS/RHEL systems, the Apache main Configuration Directory is located under this path: /etc/httpd/

cd /etc/httpd

Site-Specific Configuration: /etc/httpd/conf.d

/etc/httpd/conf.d

Windows

In Windows Operating systems, the Apache main configuration Directory is located under the following path:

C:\Program Files\Apache Group\Apache2\conf\

Inside the configuration directory, you’ll find various configuration files, including the main configuration file (httpd.conf) and additional files or directories for specific purposes (such as sites-available on Debian-based systems). The main configuration file typically includes directives for global settings, and additional files or directories may be used for virtual hosts, modules, or other specific configurations.

Upload the Sites to their respective root directories

Upload the files for each website to its respective directory. Usually in the Apache server, the following is the directory path where website files are upload

cd /var/www/

Inside the /var/www/ directory, you need to create separate directories for each website you want to host.

Assuming you plan to host two websites, the first website directory containing all the files will be website1, or you can give it the appropriate name for easy identification: This means that the DocumentRoot path will be as follows:

mkdir /var/www/website1

The command “mkdir /var/www/website1” creates a new directory named “website1” within the “/var/www/” directory on the file system. This command is commonly used in web server configurations to establish a dedicated directory for hosting the files of a specific website, and it ensures that the necessary directory structure is in place for Apache or another web server to serve the site’s content from the specified location.

Change website1 with your preferred name you want. Make sure you upload the website files in this directory:

For the second website, we will name the directory: website2

mkdir /var/www/website2

Use FTP to upload website files

Now that you’ve created the appropriate directories for your websites, you will need FTP to upload your files from your computer to these directories.

Here is the step-by-step guide on how to set up FTP on Ubuntu Linux

When uploading the files, you will need to navigate to the appropriate directory for each website.

For example, to navigate to the directory path for the first website:

cd /var/www/website1

Here is how to navigate to the second website directory path:

cd /var/www/website2

Do the same if you plan to run more than 2 websites on Apache

sudo nano /etc/apache2/sites-available/website2.conf

Just like you did in the file for the first website, we need to add the following configuration for the second website too:

Add Permissions to the Website directories

Setting proper permissions on website directories is crucial to ensure that the Apache web server can read and serve the website files.

  • Set Ownership

Change the ownership of the website directories to the Apache user and group.

   sudo chown -R www-data:www-data /var/www/website1
   sudo chown -R www-data:www-data /var/www/website2
  • Adjust Permissions

Grant read and execute permissions to the owner, and read permissions to the group and others:

sudo chmod -R 755 /var/www/website1
sudo chmod -R 755 /var/www/website2

  • Verify Permissions (Optional)

Confirm that the permissions have been set correctly

ls -l /var/www/website1
ls -l /var/www/website2

Create Virtual Host Configuration Files

In the context of setting up multiple websites, you might be interested in the virtual host configurations, which are often stored in a directory like sites-available or conf.d. Each virtual host configuration file in these directories represents the settings for a specific website.

Create the Virtual Host for the first website

You can use a text editor (e.g., nano or vim) to create the Virtual Host configuration file. In this example, we’re calling the Virtual Host configuration for the first website: website1.conf

sudo nano /etc/apache2/sites-available/website1.conf

You need to add the following basic configuration in the file you’ve created above

   <VirtualHost *:80>
       ServerAdmin [email protected]
       ServerName website1.com
       DocumentRoot /var/www/website1
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
   </VirtualHost>

We’re calling the Virtual Host configuration for the second website: website2.conf:

sudo nano /etc/apache2/sites-available/website2.conf

Just like in the file for the first website, add the following configuration directives to the wesbitesite2.conf file:

   <VirtualHost *:80>
       ServerAdmin [email protected]
       ServerName website2.com
       DocumentRoot /var/www/website2
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
   </VirtualHost>

Adjust the configuration settings for each Virtual Host file

You will need to make sure the following directives in each Virtual Host file you’ve created are adjusted according to your setup:

  • ServerName

This directive specifies the primary domain name of the virtual host. It is used to uniquely identify the virtual host when multiple virtual hosts are configured on the same server. You will need to put the appropriate domains or subdomains using the ServerName directive.

For example, if the domain for the first website is website1.com, then the ServerName in the Virtual Host file will be set as follows:

ServerName website1.com

Similarly, if the domain for the second website is website2.com, then the ServerName in the Virtual Host file will be as follows:

ServerName website2.com
  • DocumentRoot

This directive sets the root directory for the website files served by the virtual host. It defines the base directory from which Apache serves documents. In this article, we’ve set /var/www/website1 as the DocumentRoot for website1 and /var/www/website2 as DocumentRoot for website2

Enable the Virtual Hosts

Enabling virtual hosts in Apache refers to the process of activating and making a specific virtual host configuration effective on the web server. In Apache, a virtual host allows the server to host multiple websites on the same machine, and each virtual host has its own configuration settings.

When you create a virtual host configuration file (e.g., website1.conf), it is initially in an inactive state. Enabling the virtual host involves creating a symbolic link to the configuration file from the sites-enabled directory. The a2ensite command (short for Apache2 Enable Site) is commonly used to enable virtual hosts on Debian-based systems. For example:

sudo a2ensite website1.conf
sudo a2ensite website2.conf

These commands create symbolic links to the virtual host configuration files in the sites-enabled directory, allowing Apache to recognize and apply the settings specified in these files. After enabling virtual host files, you typically need to restart Apache for the changes to take effect:

sudo service apache2 restart

Set Up Hosts File (Optional)

If you’re testing on your local machine, you might want to add entries to your hosts file for each domain.

sudo nano /etc/hosts

Add lines like:

   127.0.0.1   website1.com
   127.0.0.1   website2.com

Test the websites


Open your web browser and navigate to the specified domain names. You should see the corresponding websites.

Remember to replace website1.com and website2.com with your actual domain names, and /var/www/website1 and /var/www/website2 with the actual paths to your website files.

Additionally, keep in mind that these instructions are for a basic setup. Depending on your specific requirements, you may need to configure SSL, handle server aliases, or make other adjustments.

RECOMMENDED READING:

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.