• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Apache Virtual Hosts on Debian 9

Written by Admin, Updated On May 7, 2019
a2ensite, apache, debian, httpd, virtualhost
How To Set Up Apache Virtual Hosts on Debian 9

In this tutorial, we will show you how to set up Apache Virtual Hosts on Debian 9.

Using Apache Virtual Hosts you can host multiple domains on a single server. Apache will break its functionality and components into individual units so you can customize independently. The basic unit that describes an individual site or domain is called a virtual host.

Prerequisites#

  • A Debian 9 server with a non-root user with sudo privileges.
  • Apache should installed and configured, as shown in How to Install Apache on Debian 9 Server.
  • A domain name should pointing to your server IP address.

Creating the Directory Structure#

First, we will make a directory structure where website data will be stored and serving to visitors for a domain. You can set the document root to any location that you want but it’s good practice to set in directory structure. Generally in all /var/www.

/var/www/
├── example1.com
│   └── public_html
├── example2.com
│   └── public_html

Let’s create document root directory for first domain. Execute following command to create directory :

sudo mkdir -p /var/www/example1.com/public_html

We will also create a index.html file under the domain document root directory for testing purpose. This page will be show by default when visitors will visit your site.

Creating a new index.html file using your favorite text editor by typing :

sudo nano /var/www/example1.com/public_html/index.html

Add the following lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Success! example1.com set up completed!</h1>
  </body>
</html>

Since we created the directory and files with sudo user so they are owned by our root user. We will change document root directories ownership to avoid permission issue for our regular user. Thus, regular user can modify files in our web directories without any issues.

sudo chown -R www-data: /var/www/example1.com

Create Virtual Host Files#

For a Debian machine, Apache Virtual Host configuration files are located at /etc/apache2/sites-available directory. So we will create Virtual Host configuration files at this location.

Create a new file using your choice text editor by typing :

sudo nano /etc/apache2/sites-available/example1.com.conf
<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html

    <Directory /var/www/example1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example1.com-access.log combined
</VirtualHost>
  • ServerName: This should be your domain name and match with virtual host configuration.
  • ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
  • DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
  • -Indexes: It will prevent directory listings.
  • FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLog, CustomLog: Specifies the location for log files.

You can give any names to your configuration file but it’s recommended to give file name same as domain name.

Now, We will enable virtual host file by creating symbolic links to the /etc/apache2/sites-enabled directory.

In Debian systems you can create symbolic links either by helper script or can create manually. To create using a2ensite helper script execute following command :

sudo a2ensite example1.com

To create symbolic link manually type :

sudo ln -s /etc/apache2/sites-available/example1.com.conf /etc/apache2/sites-enabled/

Now check the syntax by type :

sudo apachectl configtest

It will show below output if there are no errors:

Output
Syntax OK

You must restart apache2 service to get changes effect by below command :

sudo systemctl restart apache2

You can verify by opening your http://example1.com to your web browser and it will show you as following :

Apache Virtual Hosts

Finally, you created a virtual host for a single domain, like this you can repeat for multiple domains on a single server.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Create a Sudo User on Debian
Next Article   How to Create a Sudo User on Ubuntu

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install and Use Docker on Debian 11

    How to Install and Use Docker on Debian 11

    March 10, 2023

1 Comment

  1. Lilliana Reply
    June 21, 2019 at 8:45 am

    I’m not sure where you are getting your information, but good
    topic. I needs to spend some time learning more or
    understanding more. Thanks for great information I was looking for this info for my mission.

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook