Search code examples
pythonapache.htaccesspyramidfastcgi

Pyramid set up on a shared host with fastcgi gives the .fcgi file back to the browser


Can anyone help me with the procedure of setting up the production server on a shared host with pyramid? I searched for a whole day trying to make this work but nothing works.

I'm having trouble on writing the .htaccess and index.fcgi files. I tried to combine these tutorials; 1, 2, 3, 4 to figure it out but when I visit the website I see the contents of index.fcgi instead of the application. I've done these steps;

  1. Created a virtual environment for python in the home directory and activated it:

     mkdir temp; cd temp
     curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz
     gzip -cd virtualenv-12.0.7.tar.gz |tar xf -
     cd virtualenv-12.0.7
     python2.7 setup.py install --user
     cd ~
     ~/.local/bin/virtualenv pyramid --python=python2.7
     source ~/pyramid/bin/activate
    
  2. Installed pyramid in the virtual environment.

    pip install pyramid
    
  3. Created a test project;

    pcreate -s starter myProject
    cd myProject
    python setup.py install
    
  4. Installed flup

    pip install flup
    
  5. Created an index.fcgi file in my public_html folder with this content:

    #!/home3/reyhane/pyramid/bin/python
    import os
    import sys 
    
    myapp = '/home3/reyhane/myProject'
    inifile = 'production.ini'
    sys.path.insert(0, myapp )
    
    from paste.deploy import loadapp
    wsgi_app = loadapp('config:' + myapp + '/' + inifile)
    if __name__ == '__main__':
        from flup.server.fcgi import WSGIServer
        WSGIServer(wsgi_app).run()
    
  6. Made index.fcgi executable;

    cd public_html
    chmod +x index.fcgi 
    

    Its permission is 0755.

  7. Modified .htaccess file in public_html folder to:

    AddHandler fastcgi-script .fcgi
    DirectoryIndex index.fcgi
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.fcgi$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.fcgi/$1 [L]
    AddType "text/html; charset=UTF-8" html
    AddType "text/plain; charset=UTF-8" txt 
    AddCharset UTF-8 .html
    AddDefaultCharset UTF-8
    

So my directory looks like this:

    home3/reyhane/
    |-- pyramid
    |-- myProject
    |   |-- myProject
    |   |-- production.ini
    |-- public_html/
    |   |-- index.fcgi
    |   |-- .htaccess

It seems that .htaccess file is doing its job because the page is redirected to index.fcgi but there must be a problem with index.fcgi.


Solution

  • I have following htaccess file working fine:

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

    And FCGI file:

    #$HOME/YOUR_APP_NAME/bin/python
    import sys
    from paste.deploy
    import loadapp
    from flup.server.fcgi_fork import WSGIServer
    app = loadapp('config:$HOME/YOUR_APP_NAME/src/production.ini')
    server = WSGIServer(app)
    server.run()
    

    I described Pyramid deployment in such case in article 'Run Pyramid on Shared Hosting'. I hope it will be usefull for you.