Overview
When creating a Python script at DreamHost, there are certain rules that must be followed to ensure the file functions properly.
File extensions
All Python CGI scripts on DreamHost must end with the following extensions:
Extension | Use case |
---|---|
.fcgi | This extension uses FastCGI |
.py | This extension uses CGI |
.cgi | This extension uses CGI |
First line of the Python file
The very first line of the file could be the server location of Python 3.
#!/usr/bin/python3
However, if you've installed a custom version of Python, the first line in your file should point to that location instead. For example, after installing a custom version of Python, run which python. The output will look like this.
[server]$ which python /home/username/opt/python-3.8.5/bin/python3
The first line of your file should then look like this.
#!/home/username/opt/python-3.8.5/bin/python3
File permissions
The file's permissions must be set to 755. You can change this by running the following command via SSH.
[server]$ chmod 755
Line endings
Make sure to use UNIX style newlines, not Windows.
Viewing output
If you want to view printed output from your Python code, you must add this as the first line of output:
print("Content-type: text/html\n\n")
Make sure to use Python 3 parenthesis when creating a print statement.
Hello World! example
The code below is will display Hello World! in a web browser. Make sure to change username to your Shell user.
#!/home/username/opt/python-3.8.5/bin/python3
print("Content-type: text/html\n\n")
print("Hello World!")
Apache execution
If you don't want .py files to be executed by Apache add the following to your .htaccess file:
RemoveHandler .py
Unix group
At DreamHost, all Python CGI scripts AND their immediate parent directory must have their Unix user and Unix group left set to your domain's Unix user AND that user's DEFAULT Unix group (otherwise you will get an internal server error from Apache's suexec).