Setting up Bootstrap
POSSIBLE OUTDATED INFORMATION!
The information on this page may not function or work as intended. DreamHost support is unable to assist with any customizations. Use at your own risk!
The following is a quick procedure for establishing a simple FCGI application running Python.
- Navigate to the Manage Domains page.
- Click the Edit link under the domain you wish to edit, and scroll down to the Web Options section.
- Enable your domain for FastCGI support.
- Download fcgi.py, and save it in your PYTHONPATH.
- Create your default page handler, dispatch.fcgi:
- You must use dispatch.fcgi or you will find that the process is killed in the middle of execution. You can find more information in the Django troubleshooting article.
- Make sure you use the path #!/home/username/bin/python not #!/usr/bin/env python with Pylons.
#!/usr/bin/env python def myapp(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello World!\n'] if __name__ == '__main__': from fcgi import WSGIServer WSGIServer(myapp).run()
- Make dispatch.fcgi executable. Be sure that it is not group-writable, or a 500 Internal Error may occur:
[server]$ chmod 755 dispatch.fcgi
- Create an .htaccess file to redirect all requests to this script:
RewriteEngine On RewriteBase / RewriteRule ^dispatch\.fcgi/ - [L] RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
- After it updates, refresh the page with touch dispatch.fcgi.
Sample setup
The following is a sample setup for getting FCGI and Python to work together:
- Download Virtual Python:
[server]$ wget https://peak.telecommunity.com/dist/virtual-python.py
virtualenv is preferred to virtual-python, as it automates a lot of this process. See Installing and using virtualenv with Python 3 for further details.
- Create a directory called packages into which to install things:
[server]$ mkdir ~/packages
- View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. Add the following code to it:
export PYTHONPATH="$HOME/packages/lib/python2.3/site-packages" export LD_LIBRARY_PATH="$HOME/packages/lib" export PATH="$HOME/packages/bin:$PATH"
- Save and close the file and return to your shell.
- Run virtual python's installer and tell it to install into "packages" rather than your home directory:
[server]$ python virtual-python.py --prefix packages/
- Alternatively, install easy_install:
[server]$ wget https://peak.telecommunity.com/dist/ez_setup.py [server]$ python ez_setup.py
- Download fcgi.py and place it into ~/packages/lib/python2.3/site-packages. You can alternately use Flup, as shown in the following section.
fcgi.py serves full Python Tracebacks in HTML by default, which is a security issue. You must override the [WSGI]Server.error() method.
- To test the virtual python and fcgi installation, follow the instructions from the Django article.
- Save the following code in a file called dispatch.fcgi in ~/example.com:
#!/home/my-username/packages/bin/python from fcgi import WSGIServer def test_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) yield 'Hello, world!\n' WSGIServer(test_app).run()
- Make it executable and create an .htaccess file in the same directory as dispatch.fcgi:
[server]$ chmod a+x ~/example.com/dispatch.fcgi
- .htaccess:
RewriteEngine On RewriteBase / RewriteRule ^dispatch\.fcgi/ - [L] RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
- Wait a few minutes, and then test https://www.example.com/dispatch.fcgi.
Using Flup
As an alternate to using fcgi.py, you can run Flup to use FastCGI on DreamHost. If you choose to do so, you don't need to download or install fcgi.py. Instead, simply install Flup with easy_install:
[server]$ easy_install Flup
If you are planning to use a locally installed version of Python 3, then use the version of Flup that is compatible with Python 3. Simply use Mercurial to fetch the source:
[server]$ hg clone https://hg.saddi.com/flup-py3.0 cd flup-py3.0 # make sure setup.py is executed by your local version of Python python setup.py install
You also must add import sys to the dispatch file. The new dispatch.fcgi file should look like the following:
#!/home/my-username/packages/bin/python import sys from flup.server.fcgi_fork import WSGIServer def test_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) yield 'Hello, world!\n' WSGIServer(test_app).run()
No changes are required to your .htaccess file or anything else. Once the 'Hello World" works, you're ready to start using Python. Simply install what you need via easy_install and proceed from there.
Troubleshooting
You can diagnose a lot of errors by running dispatch.fcgi from the command line:
[server]$ ./dispatch.fcgi
You can ignore WSGIServer: missing FastCGI param errors.
If a 500 Internal Error occurs, try changing the permissions so that dispatch.fcgi is not group-writable. Here is an example:
[server]$ chmod 755 dispatch.fcgi
See also
- Python
- FastCGI
- Django — Python Web Application Framework often deployed via FastCGI
- Passenger WSGI — An alternative to using FastCGI on DreamHost