Creating Virtual Hosts, Generate SSL Certificates _ Keys and Enable CGI Gateway in Gentoo Linux

Setting up a web server to host multiple websites is a common task for system administrators and developers. In this guide, we will cover how to create virtual hosts, generate SSL certificates and keys, and enable CGI gateway in Gentoo Linux.

Virtual hosts allow you to host multiple websites on a single server, each with their own unique domain name and content. This is useful when you want to host multiple websites with different purposes or for different clients on a single server.

Enabling SSL on your website is important for security and to establish trust with your users. SSL certificates are used to encrypt the communication between the user's browser and the web server, preventing attackers from intercepting or modifying the data in transit.

CGI (Common Gateway Interface) is a standard protocol used to run scripts on a web server. CGI scripts can be written in a variety of programming languages such as Perl, Python, and Bash, and can be used to add dynamic content to your website or to perform administrative tasks.

Prerequisites

Before we begin, make sure that you have the following prerequisites

  • A Gentoo Linux machine with root access

  • Apache web server installed and running

  • SSL module and CGI module enabled in Apache web server

  • OpenSSL installed

Creating Virtual Hosts

Step 1: Create a Directory Structure

The first step is to create a directory structure for your virtual host. Use the following command to create a directory structure

sudo mkdir -p /var/www/example.com/public_html

This command will create a directory named example.com in the /var/www directory.

Step 2: Create a Virtual Host Configuration File

Next, we need to create a virtual host configuration file for our virtual host. Use the following command to create a new virtual host configuration file

sudo nano /etc/apache2/vhosts.d/example.com.conf

In this file, add the following configuration

<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/example.com/public_html
   ErrorLog /var/www/example.com/error.log
   CustomLog /var/www/example.com/access.log combined
</VirtualHost>

In the above configuration, we have defined the ServerName and ServerAlias as example.com and www.example.com. We have also specified the DocumentRoot, ErrorLog, and CustomLog locations for our virtual host.

Step 3: Enable the Virtual Host

Once the virtual host configuration file is created, we need to enable it in Apache web server. Use the following command to enable the virtual host

sudo ln -s /etc/apache2/vhosts.d/example.com.conf /etc/apache2/vhosts.d/10_example.com.conf

Step 4: Restart Apache Web Server

After enabling the virtual host, we need to restart the Apache web server to apply the changes

sudo systemctl restart apache2

Generating SSL Certificates & Keys

Step 5: Generate SSL Certificates & Keys

SSL certificates and keys are essential for secure communication between the web server and the client. First, create the SSL directory

sudo mkdir -p /etc/apache2/ssl

To generate SSL certificates and keys, use the following command

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/example.com.key -out /etc/apache2/ssl/example.com.crt

This command will create a self-signed SSL certificate and key for our virtual host valid for 365 days.

Step 6: Configure Apache to Use SSL

After generating the SSL certificate and key, we need to configure Apache to use SSL. Create a new virtual host configuration file

sudo nano /etc/apache2/vhosts.d/example.com.ssl.conf

In this file, add the following configuration

<VirtualHost *:443>
   ServerName example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/example.com/public_html
   ErrorLog /var/www/example.com/error.log
   CustomLog /var/www/example.com/access.log combined
   SSLEngine on
   SSLCertificateFile /etc/apache2/ssl/example.com.crt
   SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
</VirtualHost>

Step 7: Enable SSL Module and Virtual Host

Enable the SSL module using the following command

sudo a2enmod ssl

Enable the new SSL virtual host and restart Apache

sudo a2ensite example.com.ssl
sudo systemctl restart apache2

Enabling CGI Gateway

Step 8: Enable CGI Gateway

CGI allows the web server to execute external programs or scripts. First, enable the CGI module

sudo a2enmod cgi

To enable CGI, we need to add the CGI handler to the Apache configuration file

sudo nano /etc/apache2/httpd.conf

In the file, add the following lines

<Directory "/var/www">
   Options +ExecCGI
   AddHandler cgi-script .cgi .pl
</Directory>

Step 9: Test CGI

Now that we have enabled CGI, we can test it by creating a simple CGI script. Create a file named test.cgi in the virtual host directory

sudo nano /var/www/example.com/public_html/test.cgi

In the file, add the following code

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CGI Test</title></head><body>"
echo "<h1>CGI Test</h1>"
echo "<p>This is a test CGI script.</p>"
echo "</body></html>"

Make the file executable and restart Apache

sudo chmod +x /var/www/example.com/public_html/test.cgi
sudo systemctl restart apache2

Finally, open your web browser and go to https://example.com/test.cgi. If everything is configured correctly, you should see a webpage displaying the message "CGI Test".

Key Configuration Summary

Component Configuration File Purpose
HTTP Virtual Host /etc/apache2/vhosts.d/example.com.conf Non-secure website access (port 80)
HTTPS Virtual Host /etc/apache2/vhosts.d/example.com.ssl.conf Secure website access (port 443)
SSL Certificate /etc/apache2/ssl/example.com.crt Public key for SSL encryption
SSL Private Key /etc/apache2/ssl/example.com.key Private key for SSL decryption
CGI Configuration /etc/apache2/httpd.conf Enable script execution

Conclusion

Creating virtual hosts, generating SSL certificates, and enabling CGI gateway in Gentoo Linux allows you to host multiple secure websites with dynamic content capabilities on a single server. Virtual hosts provide isolation between sites, SSL ensures encrypted communication, and CGI enables server-side scripting for interactive web applications.

Updated on: 2026-03-17T09:01:38+05:30

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements