How to set up Apache virtual hosts on Debian 9

In this tutorial, we'll show you how to set up Apache virtual hosts on Debian 9.

Apache virtual hosts allow you to host multiple websites on a single computer. When using virtual hosts, you can specify for each domain or subdomain different document root directory (the directory containing the website files), create a separate security policies, using different SSL certificates, and so on.

Although this tutorial is written for the Debian 9, but the same steps apply to all Debian-based distributions.

prerequisites

Before continuing with the tutorial, make sure that the following prerequisites are met:

  • Let the domain name to point to your server IP address. In this tutorial we will use example.com.
  • You have installed Apache on Debian server .
  • Log in as a user with sudo privileges.

Create a directory structure

Document root directory is stored website files, and response files are stored in the directory upon request. You can document root directory set to any desired location. In this guide, we will use the following directory structure:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Hosted on our servers for each domain name that will document root is set to / var / www / <domain_name> / public_html.

Let's start by creating the document root directory of the first domain example.com:

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

We will also create an index.html file in the root domain of the document, the file will be displayed when you access field in your browser.

Open your favorite text editor, create a new file /var/www/example.com/public_html/index.html and paste the following into it:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

We run the command sudo user, newly created files and directories owned by the root user.

In order to avoid any permission issues, we will change the ownership of all files in the document root domain and the directory for the apache server user www-data):

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

Create a virtual host

On Debian systems, Apache virtual host configuration file located in / etc / apache2 / sites-available directory, and can be enabled by creating a configuration file pointed in the / etc / apache2 / sites-enabled directory symbolic link.

Open your text editor of choice and create the following basic virtual host configuration file: /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html

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

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
  • ServerName: should this virtual domain host configuration match. This should be your domain name.
  • ServerAlias: all other domains or sub-domains should also be matched with this virtual host, usually the www subdomain.
  • DocumentRoot: the directory where the site files.
  • Options: This particular control instructions available in a directory server function.
    • -Indexes: prevent directory listing.
    • FollowSymLinks: When you enable this option, Apache will follow symbolic links.
  • AllowOverride: Which directive specifies the .htaccess file can override declared configuration instructions.
  • Specify the location of the log file: ErrorLog, CustomLog.

You can name the virtual host configuration files as needed, but it is recommended to use the domain name as the name of the configuration file.

To enable the new virtual host file, create a symbolic link to the virtual host configuration file in sites-enabled directory, which is read by Apache during startup.

In the Debian system, you can use the helper script called a2ensite enable Web Hosting:

sudo a2ensite example.com

Another option is to manually create a symbolic link, as shown below:

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

After enabling configuration by typing the following test for correct syntax:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart apache2 service for the change to take effect:

sudo systemctl restart apache2

To verify that everything, open http://example.com in your favorite browser as expected, you will see the following:

How to set up Apache virtual hosts on Debian 9

in conclusion

In this tutorial, you learned how to create Apache virtual host configuration on a single Debian server hosting multiple domains. You can repeat the same steps to create additional virtual host for other domains.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160058.htm