How to create a virtual host in fedora.

How to create a virtual host in fedora.

Setting up a virtual host allows you to host multiple websites on a single server by mapping different domain names to specific directories. In this guide, we’ll talk about the steps to create a virtual host on Fedora.

What is a Virtual Host?

A virtual host enables your web server (Apache) to serve different websites based on domain names. This is helpful when running multiple sites or applications on a single server. Let’s create a virtual host on Fedora.

Prerequisites

  • Fedora system with Apache installed
  • Root or sudo user access
  • A domain name (optional for demonstration)

If Apache isn’t installed yet, run the following command:

sudo dnf install httpd -y

Enable and start Apache:

sudo systemctl enable httpd
sudo systemctl start httpd

Step 1: Create Directories for Virtual Hosts

Each virtual host should have its own directory to store website files.

sudo mkdir -p /var/www/web1.com
sudo mkdir -p /var/www/web2.com

Set appropriate permissions:

sudo chown -R apache:apache /var/www/web1.com /var/www/web2.com
sudo chmod -R 755 /var/www

Step 2: Create Virtual Host Configuration Files

Create a file at /etc/httpd/conf.d/ location with .conf extension and insert the following code block.

sudo vim /etc/httpd/conf.d/web1.com.conf

<virtualhost *:80>
   DocumentRoot “/var/www/web1.com”
   ServerName web1.com

   <Directory “/var/www/web1.com”>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
   </Directory>
</virtualhost>

Step 3: Modify /etc/hosts File (For Testing Locally)

sudo vim /etc/hosts

Insert the domain in hosts file at /etc/hosts as

127.0.0.1 web1.com

Restart the httpd service

sudo systemctl restart httpd

Step 4: Verify the Virtual Hosts

http://web1.com

Step 5: Troubleshooting

403 Forbidden Error:
Ensure that the permissions for the /var/www directories are correct:

sudo chown -R apache:apache /var/www

Firewall Issues:
Make sure HTTP traffic is allowed:

sudo firewall-cmd –permanent –add-service=http
sudo firewall-cmd –reload

if you are with me so far then enjoy.