To host multiple websites of different domains on a single Apache HTTP Server, you can configure name-based virtual hosts. With name-based virtual hosts, the Apache HTTP Server can distribute different websites to different domains that resolve to the server IP address.
About this task
You can configure a name-based virtual host for both the example.com and example.net domains with separate document root directories. Both virtual hosts serve static HTML content.
Procedure
-
Install the
httpd package:
-
Edit the
/etc/httpd/conf/httpd.conf file:
-
Append the following virtual host configuration for the
example.com domain:
<VirtualHost *:80>
DocumentRoot "/var/www/example.com/"
ServerName example.com
CustomLog /var/log/httpd/example.com_access.log combined
ErrorLog /var/log/httpd/example.com_error.log
</VirtualHost>
These settings configure the following:
-
Append a similar virtual host configuration for the
example.net domain:
<VirtualHost *:80>
DocumentRoot "/var/www/example.net/"
ServerName example.net
CustomLog /var/log/httpd/example.net_access.log combined
ErrorLog /var/log/httpd/example.net_error.log
</VirtualHost>
-
Create the document roots for both virtual hosts:
# mkdir /var/www/example.com/
# mkdir /var/www/example.net/
-
Install the
policycoreutils-python-utils package to run the restorecon command:
# dnf install policycoreutils-python-utils
-
If you set paths in the
DocumentRoot parameters that are not within /var/www/, set the httpd_sys_content_t context on both document roots:
# semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?"
# restorecon -Rv /srv/example.com/
# semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?"
# restorecon -Rv /srv/example.net/
These commands set the httpd_sys_content_t context on the /srv/example.com/ and /srv/example.net/ directory.
-
If you use
firewalld, open port 80 in the local firewall:
# firewall-cmd --permanent --add-port=80/tcp
# firewall-cmd --reload
-
Enable and start the
httpd service:
# systemctl enable --now httpd
Results
- Create a different example file in each virtual host’s document root:
# echo "vHost example.com" > /var/www/example.com/index.html
# echo "vHost example.net" > /var/www/example.net/index.html
- Use a browser and connect to
http://example.com. The web server shows the example file from the example.com virtual host.
For details, see httpd(8) and httpd.conf(5) man pages on your system.