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

From ULYSSIS documentation
No edit summary
No edit summary
Line 1: Line 1:
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.
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 [mailto:ulyssis@ulyssis.org ulyssis@ulyssis.org]. We will of course not write any code for you, but we can give you some pointers of course.
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 [mailto:ulyssis@ulyssis.org ulyssis@ulyssis.org]. We will of course not write any code for you, but we can give you some pointers or directions.


==CGI==
==CGI==

Revision as of 23:18, 28 April 2018

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. We will of course not write any code for you, but we can give you some pointers or directions.

CGI

CGI is a simple way to create a dynamic website. We use Apache's mod_cgid to provide support for CGI. Note, however, that CGI is inefficient and in general we recommend you to use a framework together with FastCGI. There are many frameworks, ranging from very simple and lightweight, to more complex and feature rich.

If, for example, you want Apache to interpret all files with the extension .cgi as CGI scripts, use the following .htaccess file (to be placed in your www folder):

Options +ExecCGI
AddHandler cgi-script .cgi

Here's an example of a very simple Python CGI script (called hello.cgi):

#!/usr/bin/env python

print 'Content-Type: text/plain\n'
print 'Hello world!'

CGI scripts need to be executable, otherwise they won't work:

chmod +x hello.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. Don't forget to make your starter script executable:

chmod +x starter.fcgi

The starter script must be located in the same folder as the .htaccess file.

Warning: Do not place sensitive files inside the web accessible folder.Make sure they are located in a folder outside the document root, or they will be downloadable through the webserver.

It is adviced to only place the fcgi starter script, .htaccess and static files (images, stylesheets) inside the document root.

Restarting your application

If you've changed your application, you will need to restart it for these changes to take effect. However, because you're editing the application on a different server than were it is running, you do not have access to the running process. You'll have to create some sort of mechanism to restart the process.

In the Django example below, the starter script has been written so that when it is changed, the application is automatically restarted within 1 second. The same mechanism can be applied to other Python sites. You can change the modification date without actually changing the contents of the starter script by using touch:

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

    Or, for Python 3:

    virtualenv ~/.venv --python=python3
  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 flup6
  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, os.path
    from threading import Thread
    
    this_file = os.path.realpath(__file__)
    
    site_dir = '/home/user/username/mysite'
    sys.path.insert(0, site_dir)
    os.chdir(site_dir)
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    
    def stat_thread():
        import time, os, signal
        start_mtime = os.stat(this_file).st_mtime
        while True:
            cur_mtime = os.stat(this_file).st_mtime
            if cur_mtime != start_mtime:
                os.kill(os.getpid(), signal.SIGTERM)
            time.sleep(1)
    
    Thread(target=stat_thread).start()
    
    from django.core.servers.basehttp import get_internal_wsgi_application
    from flup.server.fcgi import WSGIServer
    WSGIServer(get_internal_wsgi_application(), debug=False).run()
    

    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: