Install Apache and Simple VirtualHost on Debian 9 Server

Install Apache and Simple VirtualHost on Debian 9 Server

You’ve got a Debian 9 server and you just want a simple web server running, with more than one site if possible. No fancy control panels, no magic scripts, just Apache, a couple of domains, and a clean config that works.

Let’s walk through installing Apache and setting up basic VirtualHost on Debian 9, plus how to test it from your own PC.

Who This Is For (And What We’re Doing)

You’ll find this useful if:

  • You have a Debian 9 server (VPS, dedicated, or local VM)
  • You want to run Apache as your web server
  • You need more than one site or domain on the same server (VirtualHost)
  • You’re fine testing using the hosts file (no DNS server yet)

We’ll do this in four main chunks:

  1. Install Apache on Debian 9
  2. Create two VirtualHost configs (linux.lan and debian.lan)
  3. Create document root folders and test index.html files
  4. Test from a client PC using the hosts file

Basic safety note: Debian config files live under /etc/apache2. We’ll touch those, but nothing crazy. Just be careful with sudo and avoid editing as root over a bad connection if you can.

Step 1 – Install Apache on Debian 9

First job: get Apache (the apache2 package) installed and running.

  1. Update your package list (optional, but usually a good idea before installs):
    bash
    sudo apt update
  2. Install Apache:
    bash
    sudo apt install apache2
  3. After install finishes, Apache should start automatically. Test from your browser by hitting your server’s IP address:
  4. Example: http://YOUR_SERVER_IP

You should see Apache’s default page (the typical “It works!” or Debian-style default site). That’s enough to confirm Apache is up.

If the page doesn’t load, double-check:

  • The server firewall allows HTTP (port 80)
  • The Apache service is running:
    bash
    sudo systemctl status apache2

Step 2 – Plan Your VirtualHost Setup

Apache can host many domains or subdomains on one server. In Apache, this is called VirtualHost.

In this example, we’ll create two sites:

  • Domain: linux.lan, document root: /var/www/linux.lan
  • Domain: debian.lan, document root: /var/www/debian.lan

Right now there’s no DNS server, so these .lan domains are just for local/internal use. We’ll make them work later using the hosts file on your PC.

Step 3 – Create VirtualHost Config Files

On Debian, Apache VirtualHost configs live in /etc/apache2/sites-available/. We’ll add one file per domain, then enable them.

3.1 Go to the VirtualHost config directory

From your Debian server shell:

cd /etc/apache2/sites-available/

3.2 Create the first VirtualHost: linux.lan

Create a new config file for linux.lan (use nano, vim, or your editor of choice):

sudo nano linux.lan.conf

Put a basic VirtualHost configuration inside. Use your server’s IP where needed if you like, but here’s a simple name-based example structure:

<VirtualHost *:80>
    ServerName linux.lan
    DocumentRoot /var/www/linux.lan

    <Directory /var/www/linux.lan>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/linux.lan-error.log
    CustomLog ${APACHE_LOG_DIR}/linux.lan-access.log combined
</VirtualHost>

Save and exit.

3.3 Create the second VirtualHost: debian.lan

Now create the config for debian.lan:

sudo nano debian.lan.conf

Example structure:

<VirtualHost *:80>
    ServerName debian.lan
    DocumentRoot /var/www/debian.lan

    <Directory /var/www/debian.lan>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/debian.lan-error.log
    CustomLog ${APACHE_LOG_DIR}/debian.lan-access.log combined
</VirtualHost>

Save and exit.

3.4 Enable the VirtualHosts and mod_rewrite

Debian uses a2ensite to enable site configs. We’ll also enable mod_rewrite since it’s commonly needed (pretty URLs, basic rewrites, etc.).

Run:

sudo a2ensite linux.lan.conf
sudo a2ensite debian.lan.conf
sudo a2enmod rewrite

Then restart Apache to apply changes:

sudo systemctl restart apache2

Check Apache status to be sure it came back up cleanly:

sudo systemctl status apache2

If you see errors, it’s probably a typo in one of the VirtualHost files. In that case, fix the config and restart again.

Step 4 – Create Document Root Folders and Test Files

Right now the VirtualHosts point to /var/www/linux.lan and /var/www/debian.lan, but those directories don’t exist yet. Let’s fix that and drop simple index.html files in each.

4.1 Create document root for linux.lan

On your server:

sudo mkdir -p /var/www/linux.lan

Create a basic index.html to test:

sudo nano /var/www/linux.lan/index.html

Example content:

<!DOCTYPE html>
<html>
<head>
    <title>linux.lan</title>
</head>
<body>
    <h1>linux.lan is working</h1>
    <p>This is the test page for linux.lan.</p>
</body>
</html>

Save and exit.

4.2 Create document root for debian.lan

Now do the same for the second site:

sudo mkdir -p /var/www/debian.lan
sudo nano /var/www/debian.lan/index.html

Example content:

<!DOCTYPE html>
<html>
<head>
    <title>debian.lan</title>
</head>
<body>
    <h1>debian.lan is working</h1>
    <p>This is the test page for debian.lan.</p>
</body>
</html>

Save and exit.

4.3 (Optional) Fix basic permissions

On a fresh Debian setup, Apache usually runs as www-data. If you’re just testing simple static pages under /var/www/..., default permissions often work fine.

If needed, you can set simple ownership like this:

sudo chown -R www-data:www-data /var/www/linux.lan /var/www/debian.lan

Again, this is optional and depends on how you want to manage files, but don’t go crazy with chmod 777. Keep things as tight as possible.

Step 5 – Test VirtualHosts Using the Hosts File

We’re not running a DNS server, so your client PC has no idea what linux.lan or debian.lan mean. The workaround is the local hosts file on the client.

This lets you map:

  • A specific IP address → A specific domain name

excellent for internal testing or small office use.

5.1 On a Linux client: edit /etc/hosts

On your Linux desktop/laptop (the client that will access the sites):

  1. Open /etc/hosts with root privileges:
    bash
    sudo nano /etc/hosts
  2. Add lines mapping your server IP to the domains, for example:
    192.168.1.100 linux.lan
    192.168.1.100 debian.lan

Replace 192.168.1.100 with the actual IP of your Debian 9 server.

  1. Save and exit.

Now, from that Linux client, open a browser and try:

  • http://linux.lan
  • http://debian.lan

You should see the different test pages we created.

5.2 On a Windows client: edit hosts file

On Windows, the hosts file lives here:

  • C:\Windows\System32\drivers\etc\hosts

Steps (run as Administrator):

  1. Open Notepad as Administrator (right-click → Run as administrator).
  2. In Notepad, open:
  3. C:\Windows\System32\drivers\etc\hosts
  4. Add the same lines with your server IP:
    192.168.1.100 linux.lan
    192.168.1.100 debian.lan
  5. Save the file.

Then test from your browser on that Windows PC:

  • http://linux.lan
  • http://debian.lan

If everything is correct, each domain should load its own index.html.

Wrap-Up and Next Steps

Let’s recap what you’ve done:

  • Installed Apache (apache2) on Debian 9 and confirmed it runs
  • Created two VirtualHost configs: linux.lan and debian.lan
  • Added document roots under /var/www/... with simple test pages
  • Enabled the sites and mod_rewrite, then restarted Apache
  • Mapped domains to your server using the hosts file on Linux and Windows clients

From here, you can start replacing those index.html files with real sites, or plug in PHP, CMSs like WordPress, and so on.

If this saved you time, bookmark CrushEdge for more fixes.

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.