How to set up an Apache virtual host on Ubuntu 20.04

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.

1. Premise##

Before proceeding with the guide below, make sure you meet the following prerequisites:

Second, create a directory structure##

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

Three, create a virtual host##

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>

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.

Four, summary##

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

How to set up an Apache virtual host on Ubuntu 16.04
How to set up an Apache virtual host on Ubuntu 20.04
How to set up an Apache virtual host on CentOS 7
How to set up Gogs on Ubuntu 14.04
How to set up R on Ubuntu 14.04
How to set up Shiny Server on Ubuntu 14.04
How to set up time synchronization on Ubuntu 18.04
How to set up a DNS server on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to set up Java Home on Ubuntu and Raspbian
How to set up vsftpd for anonymous downloads on Ubuntu 16.04
How to set up a production Elasticsearch cluster on Ubuntu 14.04
How to set up password authentication with Nginx on Ubuntu 14.04
How to set up vsftpd for user directories on Ubuntu 16.04
How to install Apache Kafka on Ubuntu 18.04
How to set PostgreSQL startup on Ubuntu 16.04
How to install Apache Maven on Ubuntu 20.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to set static IP on Ubuntu 18.04 Server
How to set static IP on Ubuntu 18.04 Server
How to set up SSH keys on CentOS 8
Explain how to set static IP on ubuntu14.04
How to configure Apache content caching on Ubuntu 14.04
How to set up Ghost one-click app for Ubuntu 16.04
How to protect Apache with Let&#39;s Encrypt on Ubuntu 16.04
Detailed steps to configure Ubuntu 16.04 and Apache virtual host
How to set a fixed IP based on Ubuntu 16.04
How to Run Tmux Service Scripts on Ubuntu Start Up
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to set or modify the time zone on Ubuntu 20.04
How to install Memcached on Ubuntu 18.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to build an NFS file sharing server on Ubuntu 16.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04