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.

By the end of this guide, you will have learned how to create virtual hosts for your websites, generate SSL certificates and keys to enable secure communication, and enable CGI gateway to run scripts on your server.

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

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

This command will create a symbolic link from the virtual host configuration file to the /etc/apache2/vhosts.d/ directory.

Step 4: Restart Apache Web Server

After enabling the virtual host, we need to restart the Apache web server to apply the changes. Use the following command to restart the Apache web server −

sudo systemctl restart apache2

Step 5: Generate SSL Certificates & Keys

SSL certificates and keys are essential for secure communication between the web server and the client. 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 an SSL certificate and key for our virtual host.

Step 6: Configure Apache to Use SSL

After generating the SSL certificate and key, we need to configure Apache to use SSL. Use the following command to 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>

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. Additionally, we have enabled SSL using the SSLEngine directive and specified the paths to our SSL certificate and key files.

Save and close the file.

Step 7: Enable the New Virtual Host and SSL

Now that we have created the SSL virtual host configuration file, we need to enable it and restart Apache to apply the changes.

First, enable the SSL module using the following command −

sudo a2enmod ssl

Next, enable the new virtual host using the following command −

sudo a2ensite example.com.ssl

Finally, restart Apache using the following command −

sudo systemctl restart apache2

Step 8: Enable CGI Gateway

CGI (Common Gateway Interface) is a standard protocol used by web servers to execute external programs or scripts on a web server. We can use it to run server-side scripts such as PHP or Perl. In this step, we will enable CGI on our Gentoo Linux system.

To enable CGI, we need to add the CGI handler to the Apache configuration file. Use the following command to open the 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>

Save and close the file.

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 /var/www/example.com/public_html directory using the following command −

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>"

Save and close the file. Make the file executable using the following command −

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

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".

Conclusion

Creating virtual hosts, generating SSL certificates and keys, and enabling CGI gateway in Gentoo Linux are important tasks for web developers and server administrators. By creating virtual hosts, you can host multiple websites on a single server, each with their own settings and configurations. Using SSL certificates and keys ensures that your website traffic is secure, protecting sensitive information from being intercepted by hackers.

In addition, enabling CGI gateway allows you to run CGI scripts on your server, which can be useful for creating dynamic web applications. However, it is important to note that enabling CGI scripts can also introduce security risks if not properly configured.

By following the steps outlined in this article, you can create virtual hosts, generate SSL certificates and keys, and enable CGI gateway on your Gentoo Linux server with ease. It is important to follow best practices and ensure that your server is properly secured to prevent unauthorized access and protect your data.

Updated on: 26-Jun-2023

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements