Table of Contents#
- Prerequisites
- Install the Apache Web Server
- Configure the Firewall
- Create a Directory Structure for Your Websites
- Set Up Proper Permissions
- Create Sample HTML Files for Testing
- Create Virtual Host Configuration Files
- Test Apache Configuration and Restart the Service
- Verify Your Virtual Hosts Are Working
- Troubleshooting Common Issues
- Conclusion
- References
1. Prerequisites#
Before you start, ensure you have the following:
- A Rocky Linux 8 or 9 server (physical, virtual, or cloud-based).
- A non-root user with
sudoprivileges (for secure administration). - One or more domain names (e.g.,
example.com,test.com) pointed to your server’s public IP address (or use local domain names for testing). - Basic familiarity with the Linux command line.
- If using a cloud server: Ensure your cloud provider’s security group allows incoming traffic on ports 80 (HTTP) and 443 (HTTPS).
2. Install the Apache Web Server#
Apache is available in Rocky Linux’s default software repositories under the package name httpd. Follow these steps to install and enable it:
Step 2.1: Update System Packages#
First, update your system’s package index to ensure you install the latest version of Apache:
sudo dnf update -yStep 2.2: Install Apache#
Install the Apache package using dnf:
sudo dnf install httpd -yStep 2.3: Enable and Start Apache#
Enable Apache to start automatically on system boot, then start the service:
sudo systemctl enable httpd --nowStep 2.4: Verify Apache is Running#
Check the status of the Apache service to confirm it’s active:
sudo systemctl status httpdYou should see a status message indicating active (running).
3. Configure the Firewall#
Rocky Linux uses firewalld as its default firewall. To allow incoming HTTP and HTTPS traffic, you need to open the corresponding ports:
Step 3.1: Allow HTTP and HTTPS Traffic#
Run the following commands to permanently allow HTTP (port 80) and HTTPS (port 443) services:
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanentStep 3.2: Reload the Firewall#
Apply the changes by reloading firewalld:
sudo firewall-cmd --reloadStep 3.3: Verify Firewall Settings#
Check that the services are allowed:
sudo firewall-cmd --list-servicesYou should see http and https in the output.
4. Create a Directory Structure for Your Websites#
Apache uses /var/www as the default root directory for web content. For virtual hosts, we’ll create a separate directory for each domain, with a public_html subdirectory as the document root (where your site’s public files will live).
For example, for example.com and test.com:
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html/var/www/[domain]/: The parent directory for all files related to the domain./var/www/[domain]/public_html/: The document root where your site’s HTML, CSS, and JavaScript files are stored.
5. Set Up Proper Permissions#
Apache runs as the apache user on Rocky Linux, so it needs read access to your site’s files. You’ll also want your non-root user to have write access to edit site files.
Step 5.1: Set Directory Ownership#
Change the group ownership of your /var/www directories to the apache group, and keep your user as the owner:
sudo chown -R $USER:apache /var/www/example.com
sudo chown -R $USER:apache /var/www/test.comStep 5.2: Set File Permissions#
Set permissions so your user can read/write files, and the Apache group can read/execute files:
sudo chmod -R 755 /var/wwwStep 5.3: Configure SELinux Contexts#
Rocky Linux enables SELinux by default, which blocks Apache from accessing files unless the correct security context is set. First, install the policycoreutils-python-utils package (if missing) to manage SELinux contexts:
sudo dnf install policycoreutils-python-utils -yThen, set the appropriate SELinux context for your web directories:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"
sudo restorecon -Rv /var/wwwThis ensures Apache can access and serve files from your document roots.
6. Create Sample HTML Files for Testing#
Create a simple index.html file for each domain to verify your virtual hosts are working.
For example.com:#
nano /var/www/example.com/public_html/index.htmlPaste the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Example.com</title>
</head>
<body>
<h1>Success! The example.com Virtual Host is Working.</h1>
<p>This is the default page for your example.com domain.</p>
</body>
</html>Save and exit the editor (press Ctrl+O, then Enter, then Ctrl+X).
For test.com:#
nano /var/www/test.com/public_html/index.htmlPaste this content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Test.com</title>
</head>
<body>
<h1>Success! The test.com Virtual Host is Working.</h1>
<p>This is the default page for your test.com domain.</p>
</body>
</html>Save and exit.
7. Create Virtual Host Configuration Files#
Apache reads virtual host configuration files from /etc/httpd/conf.d/ (any file ending with .conf will be loaded). Create a separate configuration file for each domain.
Step 7.1: Create a Configuration File for example.com#
sudo nano /etc/httpd/conf.d/example.com.confPaste the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
ServerName example.com
ServerAlias www.example.com
# Logging configuration
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
# Allow access to the document root
<Directory /var/www/example.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>Step 7.2: Create a Configuration File for test.com#
sudo nano /etc/httpd/conf.d/test.com.confPaste this configuration (replace test.com with your domain):
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/test.com/public_html
ServerName test.com
ServerAlias www.test.com
ErrorLog /var/log/httpd/test.com-error.log
CustomLog /var/log/httpd/test.com-access.log combined
<Directory /var/www/test.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>Step 7.3: Disable the Default Welcome Page (Optional)#
By default, Apache shows a welcome page when visitors access your server’s IP address. To disable it:
sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak8. Test Apache Configuration and Restart the Service#
Before restarting Apache, always test your configuration files for syntax errors to avoid downtime:
sudo apachectl configtestIf you see Syntax OK, restart Apache to apply the changes:
sudo systemctl restart httpdIf there’s an error, the command will display a message pointing to the issue (e.g., a missing semicolon or typo in your configuration).
9. Verify Your Virtual Hosts Are Working#
Now, test your virtual hosts to ensure they serve the correct content.
Option 1: Using a Web Browser#
If your domains are pointed to your server’s public IP, open a browser and visit:
http://example.com(you should see the example.com welcome page)http://test.com(you should see the test.com welcome page)
Option 2: Local Testing (Without a Public Domain)#
If you’re testing locally, edit your machine’s hosts file to map the domains to your server’s IP:
- Linux/macOS: Open
/etc/hostswithsudo nano /etc/hostsand add:
Replace192.168.1.100 example.com test.com192.168.1.100with your Rocky Linux server’s local IP. - Windows: Open
C:\Windows\System32\drivers\etc\hostsas Administrator and add the same line.
Then, visit the domains in your browser as above.
Option 3: Using curl (Command Line)#
On the Rocky Linux server, run:
curl http://example.com
curl http://test.comYou should see the HTML content of your sample pages in the output.
10. Troubleshooting Common Issues#
If your virtual hosts aren’t working, check these common issues:
- Syntax Errors in Configuration: Run
sudo apachectl configtestto identify and fix syntax issues. - SELM Blocking Access: Reapply SELinux contexts with
sudo restorecon -Rv /var/www. - Firewall Blocking Traffic: Verify HTTP/HTTPS are allowed with
sudo firewall-cmd --list-services. - Incorrect Permissions: Double-check ownership with
ls -l /var/wwwand ensure theapachegroup has read access. - Domain Not Resolving: Use
nslookup example.comto confirm your domain points to the correct IP.
11. Conclusion#
Congratulations! You’ve successfully configured Apache Virtual Hosts on Rocky Linux. You can now host multiple websites on a single server, each with its own isolated content and configuration.
Next steps to enhance your setup include:
- Enabling HTTPS: Use Let’s Encrypt and Certbot to install free SSL certificates for your domains (run
sudo dnf install certbot python3-certbot-apacheto get started). - Adding More Virtual Hosts: Repeat steps 4–8 for additional domains.
- Securing Apache: Implement hardening measures like disabling directory listing, using mod_security, or configuring password protection for sensitive directories.