How To Setup Virtual Host Apache on Rocky Linux 10
In this tutorial, we will show you how to setup Virtual Host Apache on Rocky Linux 10. Setting up Apache virtual hosts on Rocky Linux 10 enables you to host multiple websites on a single server efficiently. This comprehensive guide walks you through every step of the process, from initial installation to advanced configuration and security hardening.
Understanding Apache Virtual Hosts
Apache virtual hosts represent a fundamental feature that allows a single Apache web server to serve multiple websites or domains simultaneously. Virtual hosts work by mapping different domain names or IP addresses to separate directories on the server, creating isolated environments for each domain while maximizing server resource utilization.
There are two primary types of virtual hosts available in Apache. Name-based virtual hosting allows multiple domains to share the same IP address while serving different content based on the domain name in the HTTP request header. IP-based virtual hosting assigns each domain its own unique IP address, though this method is less commonly used due to IPv4 address scarcity.
Virtual hosts prove invaluable for web developers, small businesses, and hosting providers who need to manage multiple websites cost-effectively. They enable complete separation of website files, logs, and configurations while maintaining optimal server performance and security.
Prerequisites and System Requirements
Before beginning the Apache virtual host setup process, ensure your Rocky Linux 10 system meets the necessary requirements for a stable installation. Your server should have at least 1GB of RAM and 10GB of available disk space for basic Apache operations, though production environments typically require significantly more resources.
Administrative privileges are essential for this configuration process. You must possess either root access or sudo privileges to install packages, modify system configurations, and manage services effectively. Active network connectivity is required to download packages from Rocky Linux repositories and apply system updates.
Additional prerequisites include basic command-line knowledge and familiarity with text editors such as vi or nano. Understanding fundamental Linux file permissions and directory structures will facilitate smoother configuration processes.
Installing Apache on Rocky Linux 10
Apache installation on Rocky Linux 10 utilizes the DNF package manager, which provides comprehensive dependency resolution and secure package installation from official repositories. The Apache web server package is called httpd
in Red Hat-based distributions, maintaining consistency with enterprise Linux standards.
System Update Process
Begin by updating your Rocky Linux system to ensure all packages and security patches are current. Execute the following command to update your system:
sudo dnf update -y
The DNF package manager fetches updates from both AppStream and BaseOS repositories. This process may take several minutes depending on your system’s current state and available updates.
Apache Installation Steps
Install Apache using the following command:
sudo dnf install httpd -y
The installation process downloads the Apache HTTP Server package and its dependencies. Rocky Linux typically installs Apache version 2.4.53 or newer, which includes modern security features and performance improvements.
Verify the installation by checking the Apache version:
httpd -v
Service Configuration
Start and enable the Apache service to ensure it runs automatically at boot:
sudo systemctl start httpd
sudo systemctl enable httpd
Check the service status to confirm proper operation:
sudo systemctl status httpd
Firewall Configuration
Rocky Linux 10 includes firewalld as the default firewall management tool, which blocks incoming web traffic to Apache by default. Configuring firewall rules properly ensures your virtual hosts remain accessible while maintaining security.
Open the required ports for HTTP and HTTPS traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
Reload the firewall configuration to apply changes:
sudo firewall-cmd --reload
Verify the firewall rules are active:
sudo firewall-cmd --list-services
This configuration allows standard web traffic on port 80 (HTTP) and secure traffic on port 443 (HTTPS) while maintaining system security.
Creating Directory Structure for Virtual Hosts
Organizing virtual host configurations using a structured directory approach simplifies management and maintenance. Create directories called sites-available
and sites-enabled
for holding virtual host files:
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Website Document Root Setup
Create document root directories for each domain you plan to host. For example, to create directories for two domains:
sudo mkdir -p /var/www/example1.com/html
sudo mkdir -p /var/www/example2.com/html
Set appropriate permissions and ownership for the web directories:
sudo chown -R apache:apache /var/www/example1.com
sudo chown -R apache:apache /var/www/example2.com
sudo chmod -R 755 /var/www/example1.com
sudo chmod -R 755 /var/www/example2.com
Create simple test HTML files to verify virtual host functionality:
sudo tee /var/www/example1.com/html/index.html > /dev/null <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example1.com</title>
</head>
<body>
<h1>Success! Example1.com virtual host is working!</h1>
</body>
</html>
EOF
Configuring Apache Main Configuration
Modify the main Apache configuration file to include virtual host files from the sites-enabled
directory. Edit the httpd.conf file:
sudo nano /etc/httpd/conf/httpd.conf
Add the following line at the end of the file to enable virtual host inclusion:
IncludeOptional sites-enabled/*.conf
This configuration directive tells Apache to load all .conf
files from the sites-enabled
directory, enabling modular virtual host management.
Understanding Configuration Hierarchy
Apache processes configuration files in a specific order, with the main httpd.conf
file loaded first, followed by files in the conf.d
directory, and finally files specified by Include
or IncludeOptional
directives. This hierarchy allows for flexible configuration management while maintaining system stability.
Creating Virtual Host Configuration Files
Virtual host configuration files contain the specific settings for each website hosted on your server. Create a configuration file for your first virtual host:
sudo nano /etc/httpd/sites-available/example1.com.conf
Add the following basic virtual host configuration:
<VirtualHost *:80>
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/html
<Directory /var/www/example1.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example1.com-error.log
CustomLog /var/log/httpd/example1.com-access.log combined
</VirtualHost>
Virtual Host Directive Explanations
The ServerName
directive specifies the primary domain name for the virtual host. The ServerAlias
directive allows additional domain names to access the same content. The DocumentRoot
directive defines the directory containing the website’s files.
The Directory
block provides specific permissions and options for the website’s document root. The Options
directive controls server features, while AllowOverride
determines which directives can be overridden by .htaccess
files.
Setting Up Multiple Virtual Hosts
Creating multiple virtual hosts follows the same pattern as the first configuration. Create a second virtual host configuration file:
sudo nano /etc/httpd/sites-available/example2.com.conf
Add the configuration for the second domain:
<VirtualHost *:80>
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/example2.com/html
<Directory /var/www/example2.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example2.com-error.log
CustomLog /var/log/httpd/example2.com-access.log combined
</VirtualHost>
Enabling Virtual Hosts
Enable virtual hosts by creating symbolic links in the sites-enabled
directory:
sudo ln -s /etc/httpd/sites-available/example1.com.conf /etc/httpd/sites-enabled/
sudo ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/
This approach allows you to easily enable or disable virtual hosts without deleting configuration files.
DNS Configuration and Testing
Proper DNS configuration ensures your domains resolve to your server’s IP address. For production environments, configure DNS records with your domain registrar or DNS hosting provider. For testing purposes, modify your local hosts file.
Local Testing Configuration
Edit your local hosts file to test virtual hosts before DNS propagation:
sudo nano /etc/hosts
Add entries for your test domains:
127.0.0.1 example1.com www.example1.com
127.0.0.1 example2.com www.example2.com
Configuration Validation
Before restarting Apache, validate your configuration for syntax errors:
sudo apachectl configtest
If the configuration is correct, you’ll see “Syntax OK” message. Restart Apache to apply changes:
sudo systemctl restart httpd
Test your virtual hosts by opening a web browser and navigating to your configured domains.
SSL/TLS Configuration for Virtual Hosts
Securing virtual hosts with SSL/TLS certificates protects data transmission and improves search engine rankings. Install the SSL module for Apache:
sudo dnf install mod_ssl -y
SSL Certificate Installation
For production environments, obtain SSL certificates from a trusted Certificate Authority or use Let’s Encrypt for free certificates. Create SSL virtual host configurations:
sudo nano /etc/httpd/sites-available/example1.com-ssl.conf
Add the SSL virtual host configuration:
<VirtualHost *:443>
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example1.com.crt
SSLCertificateKeyFile /etc/ssl/private/example1.com.key
<Directory /var/www/example1.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example1.com-ssl-error.log
CustomLog /var/log/httpd/example1.com-ssl-access.log combined
</VirtualHost>
HTTP to HTTPS Redirection
Configure automatic redirection from HTTP to HTTPS by modifying your HTTP virtual host:
<VirtualHost *:80>
ServerName example1.com
ServerAlias www.example1.com
Redirect permanent / https://example1.com/
</VirtualHost>
Troubleshooting Common Issues
Virtual host configuration problems often stem from syntax errors, permission issues, or DNS configuration problems. Use Apache’s built-in diagnostic tools to identify and resolve issues.
Configuration Debugging
Use apachectl -S
to display virtual host configuration summary:
sudo apachectl -S
This command shows how Apache interprets your virtual host configurations and identifies potential conflicts.
Log File Analysis
Monitor Apache error logs for configuration and runtime issues:
sudo tail -f /var/log/httpd/error_log
Check individual virtual host logs for specific site issues:
sudo tail -f /var/log/httpd/example1.com-error.log
Common Permission Problems
Ensure proper file permissions for web directories:
sudo find /var/www/example1.com -type d -exec chmod 755 {} \;
sudo find /var/www/example1.com -type f -exec chmod 644 {} \;
Security Best Practices
Implementing security measures protects your virtual hosts from common vulnerabilities and attacks. Configure security headers and access controls for each virtual host.
Server Information Hiding
Prevent Apache from revealing server information by adding these directives to your virtual host configurations:
ServerTokens Prod
ServerSignature Off
Access Control Configuration
Implement access restrictions for sensitive directories:
<Directory /var/www/example1.com/html/admin>
Require ip 192.168.1.0/24
Require valid-user
</Directory>
Security Headers Implementation
Add security headers to protect against common web vulnerabilities:
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Performance Optimization
Optimizing Apache performance ensures your virtual hosts serve content efficiently, even under heavy load. Configure caching, compression, and resource limits appropriately.
EnableMod_deflate for Compression
Enable compression to reduce bandwidth usage:
LoadModule deflate_module modules/mod_deflate.so
<Location />
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
</Location>
Caching Configuration
Configure browser caching for static content:
<Directory /var/www/example1.com/html>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</Directory>
Backup and Maintenance
Regular backup and maintenance procedures ensure your virtual host configurations remain secure and functional. Implement automated backup strategies and monitoring systems.
Configuration Backup Script
Create a script to backup Apache configurations:
#!/bin/bash
BACKUP_DIR="/backup/apache"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/httpd_config_$DATE.tar.gz /etc/httpd/
tar -czf $BACKUP_DIR/websites_$DATE.tar.gz /var/www/
Automated Maintenance Tasks
Configure logrotate for Apache logs:
sudo nano /etc/logrotate.d/httpd
Add log rotation configuration:
/var/log/httpd/*log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 640 apache apache
sharedscripts
postrotate
systemctl reload httpd
endscript
}
Monitoring Virtual Host Health
Implement monitoring scripts to check virtual host availability:
#!/bin/bash
DOMAINS=("example1.com" "example2.com")
for domain in "${DOMAINS[@]}"; do
if curl -s --head "http://$domain" | head -n 1 | grep -q "200 OK"; then
echo "$domain is up"
else
echo "$domain is down"
fi
done
Congratulations! You have successfully installed vHost Apache. Thanks for using this tutorial to set up virtual hosts Apache web server on Rocky Linux 10 system. For additional help or useful information, we recommend you check the official Apache website.