Apache employs Virtual Host technology, allowing the operation of multiple websites on a server with a single IP address. By setting reasonable ownership permissions for each website's directory, the management and security of multiple websites can also be ensured. This article introduces the method of running multiple websites on an Ubuntu 20.04 cloud server using Apache.
Apache's default root directory is /var/www, and a separate directory needs to be created for each website. For the purpose of illustration, let's consider three websites: example.com, example.net, and example.org.
$ sudo mkdir -p /var/www/example.com/public_html
$ sudo mkdir -p /var/www/example.net/public_html
$ sudo mkdir -p /var/www/example.org/public_html
The newly created directories are owned by the root user. We can set different users for each website, changing the ownership of each site's directory to the respective user. For this example, we'll use USER1, USER2, and USER3.
$ sudo chown -R USER1:USER1 /var/www/example.com/public_html
$ sudo chown -R USER2:USER2 /var/www/example.net/public_html
$ sudo chown -R USER3:USER3 /var/www/example.org/public_html
Next, change the permissions of the above directories to 755. This means that the directories can be read and executed by everyone (including visitors), but only the owners (USER1, USER2, USER3) have write permissions.
$ sudo chmod -R 755 /var/www/example.com/public_html
$ sudo chmod -R 755 /var/www/example.net/public_html
$ sudo chmod -R 755 /var/www/example.org/public_html
Ensure that the files within the above directories inherit the root directory's ownership.
$ sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \;
$ sudo find /var/www/example.net/public_html -type d -exec chmod g+s {} \;
$ sudo find /var/www/example.org/public_html -type d -exec chmod g+s {} \;
Create separate configuration files for each website in the /etc/apache2/sites-available directory. Taking example.com as an example:
$ sudo nano /etc/apache2/sites-available/example.com.conf
The configuration file should include the following content: The ServerAdmin directive is used to set the email address for receiving notifications about access errors. The DocumentRoot directive sets the root directory of the website. The ServerName binds the domain name to the website, directing requests for that domain name to the default document in the DocumentRoot directory. The CustomLog directive sets the path for the website's access log file, while the ErrorLog directive sets the path for the website's error log file.
<VirtualHost *:80>
ServerName example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Use the a2ensite command to enable the configuration file for the above website.
$ sudo a2ensite example.com.conf
$ sudo a2ensite example.org.conf
$ sudo a2ensite example.net.conf
Disable the default website's configuration file and restart Apache to apply the changes.
$ sudo a2dissite 000-default.conf
$ sudo systemctl restart apache2
Next, add a default homepage for each website to start serving content. For example.com, create an index.html file in the root directory.
$ sudo nano /var/www/example.com/public_html/index.html
The file content is as follows:
<html>
<head>
<title>This is example.com website</title>
</head>
<body>
<h1>This is <b>example.com</b> website.</h1>
</body>
</html>
Finally, visit http://example.com to test if it displays correctly. The operation method for other websites is similar. This concludes the method of running multiple websites on an Ubuntu cloud server using Apache.
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58