Article Directory
A virtual host is an Apache configuration command that allows you to run one or more commands on a single server. Through virtual hosting, you can specify the root directory of the website (host website files), create an independent security policy for each website, use different [SSL certificates] (https://cloud.tencent.com/product/symantecssl?from=10680), and so on.
This article describes how to set up an Apache virtual host on Ubuntu 20.04.
Before proceeding with the guide below, make sure you meet the following prerequisites:
The file root directory is the place of the website [file storage] (https://cloud.tencent.com/product/cfs?from=10680) corresponding to the domain name and the place where the website requests are responded to. You can set the website root directory to any location you want. In this example, we will use the following directory structure:
/var/www/
├── domain1.com
│ └── public_html
├── domain2.com
│ └── public_html
For each hosted domain name, its domain name root directory is set to /var/www/<domain_name> /public_html
.
Start creating the root directory for the domain name:
sudo mkdir -p /var/www/domain1.com/public_html
We will create an index.html
file in the root directory of the domain name, which will be displayed when you browse the domain name in your browser: /var/www/domain1.com/public_html/index.html
<! DOCTYPE html><html lang="en" dir="ltr"><head><meta charset="utf-8"><title>Welcome to domain1.com</title></head><body><h1>Success! domain1.com home page!</h1></body></html>
Because the above command is executed as a sudo user, the newly created files and directories belong to root. To avoid any permission problems, modify the root directory of the domain name and all files in this directory to the apache user (www-data
):
sudo chown -R www-data:/var/www/domain1.com
On Ubuntu systems, the Apache virtual host configuration file is in the /etc/apache2/sites-available
directory. They can enable virtual host configuration by creating a symbolic link to the /etc/apache2/sites-enabled
directory, which will be read when Apache starts.
Open a text editor of your choice and create the following virtual host configuration file: /etc/apache2/sites-available/domain1.com.conf
< VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
ServerAdmin [email protected]
DocumentRoot /var/www/domain1.com/public_html
< Directory /var/www/domain1.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
< /Directory>
ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
< /VirtualHost>
ServerName
: The domain name matched by the virtual host. This may be your domain name.
ServerAlias
: match other domain names or subdomains of this virtual host, such as www
subdomain.
DocumentRoot
: Apache website root directory
Options
: Directive to control the server characteristics of the specified directory
- Indexes
: prevent directory listing
FollowSymLinks
: When this option is enabled, Apache will allow access to symbolic link files
AllowOverride
: Specify which directives in the .htaccess
file can override directives in the configuration file.
ErrorLog
, CustomLog
: Specify the location of the log file.
You can name the configuration file as you like, but the best practice is to use the domain name as the name of the virtual host configuration file.
To enable a new virtual host file, use the a2ensite
helper script to create a symbolic link of the virtual host file to the sites-enabled
directory:
sudo a2ensite domain1.com
Another option is to manually create a symbolic link, like the following:
sudo ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/
Once completed. Test the configuration file for any syntax errors:
sudo apachectl configtest
If there are no errors, you will see the following output:
Syntax OK
Restart the Apache server for the changes to take effect:
sudo systemctl restart apache2
Finally, to verify that everything is as smooth as expected, open http://domain1.com
in your browser and you can see the index.html
page.
You have learned how to create an apache virtual host configuration on an Ubuntu server to host multiple domain names.
Repeat the above steps to create other virtual hosts for all domain names.
Original: https://linuxize.com/post/how-to-set-up-apache-virtual-hosts-on-ubuntu-20-04/
Copyright notice: This work uses Creative Commons attribution-Share 4 in the same way.0 International license agreement for licensing.
Recommended Posts