The last tutorial on Installing LAMP in Gentoo Linux just covered the basic installation process without additional settings available for Apache to better control your domains.

This tutorial is strictly connected with previous one on Gentoo LAMP and discusses extra settings for LAMP environment such as create Virtual Hosts on Apache, generate SSL Certificate files and Keys, enable secure SSL protocol on HTTP transactions and use Apache CGI Gateway so you can run Perl or Bash scripts over your website.
Requirements
- Install LAMP in Gentoo Linux
Step 1: Create Apache Virtual Hosts
This topic uses a fake domain name – gentoo.lan – enabled through local hosts file, with website files served from /var/www/gentoo.lan – DocumentRoot directive, without a valid DNS record, to demonstrate how multiple Virtual Hosts can be enabled on Gentoo using Apache web Server.
1. To begin, open Gentoo hosts file for editing and add a new line with your domain name.
At the end of the file make it look similar to this.

2. Test your fake domain with ping command and the domain should respond with its IP address.

3. The process of activating Apache Virtual Hosts is quite simple. Just open Apache default virtual hosts file located on /etc/apache2/vhosts.d/ path and before last statement, enter your new Virtual Host definition enclosed under … directives. c
Containing your custom settings like ServerName and DocumentRoot path. Use the following file template as a guide for a new Virtual Host and include it on 00_default_vhost.conf file (for non-SSL websites).

As you can see by visualizing this file content, the file is highly commented with explanations and also keeps localhost Virtual Host definition – which you can use it as a guide.
4. After finishing editing file with your custom Virtual Host, restart Apache to apply settings and make sure that you create DocumentRoot directory in case you have changed this directive and the path doesn’t exist by default ( in this case was changed to /var/www/gentoo.lan). I’ve also created a small PHP file to test webserver configurations.
5. To verify it, open a browser and point it your virtual domain name http://gentoo.lan/info.php.

Using this procedure you can add as many non-SSL websites as you wish using Apache Virtual Hosts, but for a real Internet-facing machine assure that you have your domains registered and you use valid DNS server records.
To remove a Virtual Host just comment out or delete its directives enclosed under … on 00_default_vhost.conf file.
Step 2: Genereate SSL Certificates and Keys for Virtual Hosts
SSL is a cryptographic protocol used to exchange information over a secure communication channel in Internet or inside networks using Certificates and symmetric/asymmetric Keys.
6. To simplify Certificates and keys generation process use the following Bash script that acts as a command and automatically creates all you need with your SSL domain name settings.
First start by creating Bash script using the following command.
Add the following file content.

7. After the file has been created, append execute permissions on it and run it to generate SSL Keys and Certificates.

When you run it at the first time, will ask you to enter your domain name. Enter your domain name for which you generate SSL settings and fill Certificate with required information, the most important one, Common Name, use your server FQDN.
Default location where all your Certificates and Keys are hosted using this method is /etc/apache2/ssl/.
8. Now it’s time to create gentoo.lan Virtual Host SSL equivalent. Use the same method as for non-SSL Virtual Hosts but this time editing /etc/apache2/vhosts.d/00_default_ssl_vhosts.conf file with slightly changes.
First open file for editing and make the following changes.
Under Listen 443 directive add the following content.

Use following template for a new Virtual Host and append new SSL Certificate + Key path and names.

Virtual Hosts definitions must end before this last three statements.

9. After finishing editing Virtual Host file, restart Apache service and direct your browser to your domain using HTTPS protocol https://gentoo.lan.

Using this procedure, you can add SSL websites with their own Certificates and Keys using Apache Virtual Hosts. To remove SSL Virtual Hosts comment out or delete its directives enclosed under … on /etc/apache2/vhosts.d/00_default_ssl_vhosts.conf file.
Step 3: Enable CGI Interface
The CGI (Common Gateway Interface) it allows Apache to interact with external programs, primary consisting in Perl or BASH scripts, which can add a dynamic content to your website.
10. Before enabling CGI gateway make sure Apache was compiled with USE CGI modules flags support on Portage make.conf file: cgi cgid. To enable GCI support for Apache open /etc/conf.d/apache2 file and append CGI module on APACHE2_OPTS line.
Assure that this line has similar content.

11. After CGI modules are enabled, open your website definition host you want to enable CGI interface and add the following content inside Virtual Host directives.

12. If you have a directory inside your DocumentRoot (/var/www/gentoo.lan/) path that holds CGI scripts you can enable just that directory to serve dynamic Perl or Bash scripts.
13. For SSI (Server Side Includes) append +Includes statement on Options and add .shtml file extension.
14. To test some simple .cgi and .pl scripts on Apache CGI gateway create the following scripts inside you Virtual Host DocumentRoot (/var/www/gentoo.lan/).
Perl Script
Add the following Perl content.
Bash Script
Add the following Bash content.
15. After the files have been created, make them executable, restart Apache daemon and point your browser to the following URLs.

Now you can transform Gentoo into a powerful Web hosting platform with fine tuning settings for your system performance and maximum control over your entire environment.
No comments:
Post a Comment