Step 5 — Setting Up Virtual Hosts (Recommended) #
When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name.
Info: If you are setting up a domain name with DigitalOcean, please refer to our Networking Documentation.
Apache on Debian 11 has one server block enabled by default that is configured to serve documents from the /var/www/html
directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html
, create a directory structure within /var/www
for a your_domain site, leaving /var/www/html
in place as the default directory to be served if a client request doesn’t match any other sites.
Create the directory for your_domain as follows:
sudo mkdir -p /var/www/your_domain
Copy
Next, assign ownership of the directory to the user you’re currently signed in as with the $USER
environment variable:
sudo chown -R $USER:$USER /var/www/your_domain
Copy
The permissions of your web roots should be correct if you haven’t modified your umask
value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
sudo chmod -R 755 /var/www/your_domain
Copy
Next, create a sample index.html
page using your preferred text editor. Here, we’ll use nano
:
nano /var/www/your_domain/index.html
Copy
Inside, add the following sample HTML:
/var/www/your_domain/index.html
<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished. If you’re using nano
, you can do this by pressing CTRL + X
, then Y
and ENTER
.
In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf
directly, make a new one at /etc/apache2/sites-available/your_domain.conf
:
sudo nano /etc/apache2/sites-available/your_domain.conf
Copy
Insert the following configuration block, which is similar to the default, but updated for your new directory and domain name:
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Notice that we’ve updated the DocumentRoot
to our new directory and ServerAdmin
to an email that the your_domain site administrator can access. We’ve also added two directives: ServerName
, which establishes the base domain that will match this virtual host definition, and ServerAlias
, which defines further names that will match as if they were the base name.
Save and close the file when you are finished.
Now enable the file with the a2ensite
tool:
sudo a2ensite your_domain.conf
Copy
Disable the default site defined in 000-default.conf
:
sudo a2dissite 000-default.conf
Copy
Next, test for configuration errors:
sudo apache2ctl configtest
Copy
You should receive the following output:
OutputAH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Restart Apache to implement your changes:
sudo systemctl restart apache2
Copy
Apache will now be serving your domain name. You can test this by navigating to http://your_domain
, where you will see something like the following:
