Ubuntu Server Apache Multiple Websites: The Ultimate Guide : cybexhosting.net

Welcome to our comprehensive guide on how to set up and manage multiple websites on Ubuntu Server using Apache. We understand that setting up and managing multiple websites on a single server can be a daunting task, especially for beginners. However, with our step-by-step guide, you will be up and running in no time.

Table of Contents

Introduction

If you’re looking to host multiple websites on a single Ubuntu server, you’ll need to set up a web server that can handle multiple domains. Apache, the most popular web server software, is a great choice for this task. In this guide, we will go through all the steps required to set up and manage multiple websites using Apache on Ubuntu Server.

Prerequisites

Before we dive into setting up multiple websites on Ubuntu Server, there are a few prerequisites that you must have in place:

  • A Ubuntu Server with root access
  • A registered domain name for each website you want to host
  • A basic understanding of the Linux command line and Apache web server

Install Apache

The first step in setting up multiple websites on Ubuntu Server is to install the Apache web server. Apache is the most widely used web server software, and Ubuntu includes it in its default package repositories. To install Apache, follow these steps:

  1. Update the package list: sudo apt update
  2. Install Apache: sudo apt install apache2

Once the installation is complete, Apache will start automatically. You can check the status of Apache by running the following command:

sudo systemctl status apache2

If Apache is running, you should see something like this:

Output
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Mon 2021-05-10 15:24:43 UTC; 5min ago
 Main PID: 10591 (apache2)
    Tasks: 55 (limit: 2292)
   CGroup: /system.slice/apache2.service
           ├─10591 /usr/sbin/apache2 -k start
           ├─10592 /usr/sbin/apache2 -k start
           └─10593 /usr/sbin/apache2 -k start

Configure Firewall

By default, Ubuntu Server comes with a firewall called UFW (Uncomplicated Firewall) installed. Before we proceed with setting up virtual hosts, we need to configure the firewall to allow HTTP and HTTPS traffic. To do this, follow these steps:

  1. Enable UFW: sudo ufw enable
  2. Allow HTTP traffic: sudo ufw allow 'Apache'
  3. Allow HTTPS traffic: sudo ufw allow 'Apache Full'
  4. Check the status of UFW: sudo ufw status

You should see the following output:

Output
Status: active

To                         Action      From
--                         ------      ----
Apache                     ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
Apache (v6)                ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Create Virtual Hosts

Virtual hosts allow you to host multiple websites on a single server by directing web traffic to the appropriate website based on the domain name. To create virtual hosts on Ubuntu Server, follow these steps:

  1. Copy the default Apache configuration file to create a new virtual host: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
  2. Edit the virtual host file: sudo nano /etc/apache2/sites-available/example.com.conf
  3. Replace the contents of the file with the following:
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
  • ServerAdmin: The email address of the server administrator
  • ServerName: The domain name of the website
  • ServerAlias: Any additional domain names that should point to this website
  • DocumentRoot: The location of the website files on the server
  • ErrorLog and CustomLog: The location of the log files for this website
  1. Disable the default Apache virtual host: sudo a2dissite 000-default.conf
  2. Enable the new virtual host: sudo a2ensite example.com.conf
  3. Reload Apache to apply the changes: sudo systemctl reload apache2

Repeat these steps for each website you want to host on your server.

Configure DNS

Once you have set up your virtual hosts, you need to point your domain names to your server’s IP address. To do this, you need to configure your domain name’s DNS settings to include an “A” record pointing to your server’s IP address. The exact steps for doing this will depend on your domain registrar, but generally, you can follow these steps:

  1. Login to your domain registrar’s website and navigate to the DNS settings for the domain name you want to point to your server
  2. Add an “A” record with the following details:
    • Host: @ (or leave blank)
    • Points to: Your server’s IP address
    • TTL: 3600 (or the default value)
  3. Save the changes and wait for them to propagate (usually a few minutes to a few hours)

Test Configuration

Now that you have set up your virtual hosts and configured your DNS settings, it’s time to test your configuration. To do this, follow these steps:

  1. Open a web browser and navigate to one of your domain names
  2. You should see the website you set up for that domain name
  3. If you see the Apache default page, make sure that you have disabled the default virtual host and enabled your new virtual host, and that your DNS settings are correctly configured

FAQs

What is Apache?

Apache is the most widely used web server software, accounting for over half of all web servers on the internet. It is open-source software that is free to use and can run on a variety of operating systems, including Linux, Windows, and macOS.

Why do I need virtual hosts?

Virtual hosts allow you to host multiple websites on a single server by directing web traffic to the appropriate website based on the domain name. Without virtual hosts, you would need a separate server for each website you want to host, which can be expensive and inefficient.

What is a DNS record?

A DNS record is a piece of information that tells the internet where to find a particular website. When you type a URL into your web browser, your computer sends a request to a DNS server to convert the domain name into an IP address. The DNS server responds with the IP address of the server hosting the website, and your computer connects to that server to retrieve the website.

How do I find my server’s IP address?

You can find your server’s IP address by running the following command:

hostname -I

This will display your server’s IP address(es).

Source :