Nerveware


Apache

Intro

Apache is the choice of webserver for many. It's free to download and use and has a lot of convenient features. This document will show how create and handle virtual hosts, how to enable CGI within Apache and will briefly discribe features in Apache such as .htacces and .htpasswd files.

Before we begin

When I develop sites on my laptop, I usually relocate my web root to my home directory.

$ touch -p $HOME/sites $ mv /var/www /var/www-old $ ln -s $HOME/sites /var/www

Also, its nice to use url's instead of ip addresses to navigate to your site. Since no wildcards are allowed, you will have to create a rule for every redirect.

echo 127.0.0.1 mysite.local www.mysite.local >> /etc/hosts

Finally, the commands within this document must be executed as root or user with root privileges.

Virtual hosts

Virtual hosts, or vhosts for short, are needed whenever a system has to run multiple sites. It is recommend to make a first generic site before you add more. You would do so because, whenever Apache does not know what do do with the request, it redirects to the first known site. That would be bad if your visitor wants to see a video and, well, uncomfortable situations might occur. The 'default' is configured in /etc/apache2/sites-available/000-default.conf and is an simple configuration. To create a default vhost, creating a file index.html in /var/www/html would probably be enough.

Creating vhosts

The basic vhost configuration for mysite.local looks something as this:

<VirtualHost *:80> ServerAdmin root@mysite.local ServerName mysite.local ServerAlias www.mysite.local DocumentRoot /var/www/mysite.local <Directory /var/www/mysite.local/> Options Indexes FollowSymLinks MultiViews AllowOverride ALL Order allow,deny allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn </VirtualHost>

Now that the configuration exists, enabling the site is quite easy. After executing the ocmmands below, Apache will show you a webpage with a singel word, namely 'test', if you navigate to mysite.local.

$ mkdir -p /var/www/mysite.local/ $ echo test > /var/www/mysite.local/index.html $ a2enmod rewrite $ a2ensite mysite.conf $ systemctl reload apache2

To disable your site, execute the following commands.

$ a2dissite mysite.conf $ systemctl reload apache2

Enable SSL

In this section I will show you how to add a self signed certificate to Apache. Self signed certificates are typically not trusted by third parties or modern browsers. I would suggest that you use one of those fancy and free certificate providers after following this part of the tutorial.
First we're going to create a special directory to store the certificate(s) and enable ssl in Apache.

$ mkdir /etc/apache2/ssl $ a2enmod ssl

Create the certificate. You will be asked to answer a few questions. Please read them carefully. This certificate will remain valid for a year and is RSA with 2048 bit encryption.

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout /etc/apache2/ssl/mysite.local.key \ -out /etc/apache2/ssl/mysite.local.crt

Append the following xml node underneath the other vhost.

<VirtualHost *:443> SSLEngine ON SSLCertificateFile /etc/apache2/ssl/mysite.local.crt SSLCertificateKeyFile /etc/apache2/ssl/mysite.local.key SSLCACertificateFile /etc/apache2/ssl/mysite.local.crt ServerAdmin root@mysite.local DocumentRoot /var/www/mysite.local ServerName mysite.local ServerAlias www.mysite.local <Directory /var/www/mysite.local/> Options Indexes FollowSymLinks MultiViews AllowOverride ALL Order allow,deny allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log logLevel warn </VirtualHost>

There's no reason to make use of HTTP, therefore it's recommended to redirect to HTTPS if the site is intended for private use. Note that sites with self signed certificates will have a warning whenever a visitor goes to your page.

Add the following to the HTTP node.

RewriteEngine ON Redirect permanent / https://mysite.local

What rests now is to reload the configuration.

$ a2dissite mysite.conf $ a2ensite mysite.conf $ systemctl reload apache2

Enable CGI

I've created a web framework in C++, just because I can. It's a simple MVC framework which redirects every request to a.out (I was lazy). Internally it handles the requests and calls the appropriate controller. The controller calls a view, and such, and so forward. The point is that you want to redirect the requests that don't have matching files in your www root, and not requests to your javascript, CSS or images. Again, you need to load a module.

$ a2enmod cgi

The configuration is straightforward, but I haven't explained everything yet. (I will in the following section). Keep in mind that you will have to return headers yourself.

<VirtualHost *:80> ServerAdmin root@localhost ServerName mysite.local ServerAlias www.mysite.local ErrorLog /var/log/apache2/error.log LogLevel debug DocumentRoot "/var/www/mysite.local" DirectoryIndex a.out <Files ~ "\.out"> Options +ExecCGI AddHandler cgi-script .out </Files> <Directory "/var/www/mysite.local/"> RewriteEngine on DirectoryIndex a.out RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) a.out?$1 [L] Options Indexes FollowSymLinks MultiViews AllowOverride ALL Order allow,deny allow from all </Directory> </VirtualHost>

To get you started, here is a minimal Hello World response from your webserver.

#include int main(void) { printf("Content-Type: text/html\n\nHello world\n"); return 0; }

.htaccess and .htpasswd

todo