Configuring virtual hosts in Apache on Linux involves setting up multiple websites on a single Apache web server, each with its own domain name and separate configuration. Here’s a basic guide to configuring virtual hosts in Apache on Linux:
/etc/apache2/sites-available/
or /etc/httpd/conf.d/
..conf
. For example, to create a virtual host configuration for example.com
, you can use example.com.conf
.
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
example.com
with your domain name, /var/www/example.com/public_html
with the path to your website files, and webmaster@example.com
with your email address.sites-enabled
directory. Use the a2ensite
command, followed by the name of your configuration file (without the .conf
extension).
sudo a2ensite example.com
sudo systemctl restart apache2
Once configured, Apache will serve each website from its respective document root directory, based on the domain name requested by the client. This allows you to host multiple websites on a single Apache server, each with its own configuration and content.