Using (Fast)CGI for non-PHP websites: Difference between revisions

From ULYSSIS documentation
Line 12: Line 12:


In order to use FastCGI, you will generally need a starter script specific to your framework and application. Just do a web search for "''framework'' mod_fcgid", where you ''framework'' is the framework you're using, and you will find good instructions. There's a Django example below.
In order to use FastCGI, you will generally need a starter script specific to your framework and application. Just do a web search for "''framework'' mod_fcgid", where you ''framework'' is the framework you're using, and you will find good instructions. There's a Django example below.
You'll want to treat this starter script as a <tt>mod_fcgid</tt> script, and usually, you'll want to redirect everything to this script, so you can use an <tt>.htaccess</tt> file that looks like this:
<pre>Options +ExecCGI
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ starter.fcgi/$1 [QSA,L]</pre>
Replace ''starter.fcgi'' with the name of your starter script.


===Restarting your application===
===Restarting your application===
Line 17: Line 25:
If you've changed your application, you will need to restart it for these changes to take effect. In order to do that, you have to set the modification date of the starter script to a more recent date:
If you've changed your application, you will need to restart it for these changes to take effect. In order to do that, you have to set the modification date of the starter script to a more recent date:
<pre>touch starter.fcgi</pre>
<pre>touch starter.fcgi</pre>
Replace ''starter.fcgi'' with the name of your starter script.


===Example: Django===
===Example: Django===
Line 47: Line 56:
<li>
<li>
Put an <tt>.htaccess</tt> file in your site's directory, e.g. the <tt>www</tt> folder:
Put an <tt>.htaccess</tt> file in your site's directory, e.g. the <tt>www</tt> folder:
<pre>
<pre>Options +ExecCGI
Options +ExecCGI
AddHandler fcgid-script .fcgi
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteEngine On

Revision as of 18:04, 30 August 2014

You're not stuck with PHP if you want to make a dynamic website. You can use all sorts of frameworks and programming languages with ULYSSIS, as long as it has CGI or FastCGI support.

If you want to use a certain programming language or framework, and you can't get it to work, don't hesitate to contact us at ulyssis@ulyssis.org.

CGI

CGI is a very easy and simple way to create a dynamic website. We use Apache's mod_cgid to provide support for CGI.

FastCGI

We use Apache's mod_fcgid to provide support for FastCGI.

In order to use FastCGI, you will generally need a starter script specific to your framework and application. Just do a web search for "framework mod_fcgid", where you framework is the framework you're using, and you will find good instructions. There's a Django example below.

You'll want to treat this starter script as a mod_fcgid script, and usually, you'll want to redirect everything to this script, so you can use an .htaccess file that looks like this:

Options +ExecCGI
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ starter.fcgi/$1 [QSA,L]

Replace starter.fcgi with the name of your starter script.

Restarting your application

If you've changed your application, you will need to restart it for these changes to take effect. In order to do that, you have to set the modification date of the starter script to a more recent date:

touch starter.fcgi

Replace starter.fcgi with the name of your starter script.

Example: Django

If you want to make a website using Django, we recommend that you use a virtualenv. This tool is already preinstalled on our shell servers.

You can use the following steps to get up and running with Django at ULYSSIS:

  1. Log in to a shell server
  2. Create a new virtualenv, you can create one in ~/.venv, for example:
    virtualenv ~/.venv
  3. Activate this virtualenv:
    . ~/.venv/bin/activate
  4. Install Django inside of this virtualenv:
    pip install django
  5. Create a new Django project:
    django-admin.py startproject mysite

    Replace mysite with your project's desired name.

  6. Install flup for FastCGI:
    pip install flup
  7. Put an .htaccess file in your site's directory, e.g. the www folder:
    Options +ExecCGI
    AddHandler fcgid-script .fcgi
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
  8. Create the starter script (replace user with org if you are an organization, and username with your ULYSSIS username):
    #!/home/user/username/.venv/bin/python
    
    import sys, os
    
    sys.path.insert(0, '/home/user/username/mysite')
    
    os.chdir('/home/user/username/mysite')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")

    Call this script mysite.fcgi.

  9. Make the starter script executable:
    chmod +x mysite.fcgi
  10. Go to username.ulyssis.be (or whatever URL you chose to use), and it should show you the default "Welcome to Django" page.

This guide was based on, and you can find more information in the official Django documentation: