Skip to content

Apache Configuration

Zwetan Kjukov edited this page Aug 27, 2015 · 4 revisions

To run our CGI we use Apache and so here our apache conf for as3lang.org

/etc/apache2/sites-available/as3lang.www

#www.as3lang.org
<VirtualHost *:80>

    ServerAdmin ${APACHE_SERVER_ADMIN}
    ServerName www.as3lang.org
    ServerAlias as3lang.org
    ServerSignature Off

    Header always set X-ProcessingTime "%D"

    #Enables DNS lookups on client IP addresses
    #HostnameLookups On|Off|Double
    HostnameLookups Double

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    ErrorLog ${APACHE_LOG_DIR}/as3lang.www-error.log
    CustomLog ${APACHE_LOG_DIR}/as3lang.www-access.log vhost_combined

    ScriptLog /home/vhosts/as3lang.org/www/logs/cgi.log
    ScriptLogBuffer 1024
    ScriptLogLength 10385760

    DocumentRoot /home/vhosts/as3lang.org/www/htdocs

    <Directory />
      Options FollowSymLinks
      AllowOverride All
    </Directory>

    <Directory "/home/vhosts/as3lang.org/www/htdocs/">
      Options Indexes FollowSymLinks MultiViews Includes
      AllowOverride All
      Require all granted
      Options +ExecCGI
      AddHandler cgi-script .abc
    </Directory>

</VirtualHost>

You will also need an .htaccess file in your web root (htdocs)

ErrorDocument 500 /500.html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.abc [QSA,L]
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]

note: this one is pretty basic, se more details next paragraph.

More htaccess

Remember, we use CGI to test/experiment/prototype what AS3 can do on the server side, and in the process we use also some Apache "tricks" to avoid to have to implement everything.

ErrorDocument 500 /500.html
ErrorDocument 503 /maintenance.html

RewriteEngine On

#RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^.*$ /maintenance.html [R=503,L]

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.abc [QSA,L]

ErrorDocument 500 /500.html
Our Gateway will catch errors and also output 500 pages, here we just make the default Apache 500 page nicer, but remember if this page shows up that means some fatal error happened and the Gateway did not catch it (take a look a the final templates you will see that we differentiate between Gateway generated 500 and Gateway generated 500 pages).

ErrorDocument 503 /maintenance.html
same as the 500 pages, there could be some cases where we need to work on updating/testing the server without providing access to the public.

#RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
Allows to define a static IP that can access the pages even if the maintenance mode is enabled.

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
Checks if the static maintenance page exists.

RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
Checks if the static maintenance.enable file exists.

RewriteCond %{SCRIPT_FILENAME} !maintenance.html
Checks if we are nto accessing directly the maintenance page.

RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
Allows to fetch some files extensions without redirection.

RewriteRule ^.*$ /maintenance.html [R=503,L]
Here how the maintenance page works, if the server find the static file maintenance.enable it will redirect any requests to the static maintenance.html page.
By default we have a file maintenance.disable, we just need to rename it to maintenance.enable to activate the "maintenance mode".

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
Apache CGI filters out Authorization headers, so basically we intercept them and reinject them as "HTTP_AUTHORIZATION" environment variables.

RewriteCond %{REQUEST_FILENAME} !-f
Checks if the filepath exists.

RewriteRule ^ index.abc [QSA,L]
if the filepath does not exists pass trough the index.abc file and because we have AddHandler cgi-script .abc then this file is executed as CGI script.

How it works

Thanks to binfmt_misc our .abc files are executable

so adding

Options +ExecCGI
AddHandler cgi-script .abc

is enough to run any .abc files as CGI

You will have to make the file executable (eg. chmod +x)
otherwise when reaching for the file the server will return
an HTTP Error 500.

The rewrite rules are basically this

  • if the file does not exists
  • redirect everything towards the file index.abc
  • the E=REMOTE_USER:%{HTTP:Authorization is to correct
    a problem with Apache which does not pass trough
    authentification credentials under CGI

This is one way to do it, here we took the choice to compile all AS3 source code to one index.abc file and to redirect all requests to that unique file.

This has the advantage to keep things simple, for compilation and deployement on the server, and also allow you to mix static files which will be served directly by Apache instead of being routed to your index.abc.

Clone this wiki locally