|
| 1 | +# Running a Python CGI server |
| 2 | + |
| 3 | +In order to run a Python CGI server, you can start by creating a new workspace. It doesn't have to be a Python/Django workspace, even a Custom workspace would do. You would need to tell the Apache2 server already installed on the workspace to allow CGI scripts and to make sure it knows where to look for them. |
| 4 | + |
| 5 | +Lets start by creating a very simple python CGI script: |
| 6 | + |
| 7 | +1. Create a folder within your workspace, name it `cgi-bin` (the name doesn't matter, although it is customary to use `cgi-bin`). |
| 8 | +2. Within the `cgi-bin` folder, create a new file. Lets name it `test.py` for now. |
| 9 | +3. Paste in the following: |
| 10 | + |
| 11 | +```python |
| 12 | +#!/usr/bin/env python |
| 13 | + |
| 14 | +print "Content-type: text/html" |
| 15 | +print "" |
| 16 | + |
| 17 | +print """ |
| 18 | +<html> |
| 19 | +
|
| 20 | +<head><title>Sample CGI Script</title></head> |
| 21 | +
|
| 22 | +<body> |
| 23 | + <h3> Sample CGI Script </h3> |
| 24 | +</body> |
| 25 | +</html> |
| 26 | +""" |
| 27 | +``` |
| 28 | + |
| 29 | +Now, once we have a file we want to serve, lets start up the Apache web server. We can do that by creating a new runner. You can create a new runner by clicking on the + icon on the right of the tabs like shown below: |
| 30 | + |
| 31 | +[]{: #NewRunConfiguration} |
| 32 | + |
| 33 | +then select Apache httpd (PHP, HTML) |
| 34 | + |
| 35 | +[]{: #ApacheRunConfiguration} |
| 36 | + |
| 37 | +You can save this configuration by entering a name to the right of the `Command:` label like shown below. |
| 38 | + |
| 39 | +[]{: #SaveRunConfiguration} |
| 40 | + |
| 41 | + |
| 42 | +Click on the Run button and the apache server is ready to go. Click on the link that reads something like: `https://<workspace-name>-<your-username>.c9.io/` and you should see a browser come up with your folder listing. Clicking on the `cgi-bin` link and then the `test.py` link just shows up the text of the python file we wrote. Now that we have the server running, lets enable CGI on it. |
| 43 | + |
| 44 | +The first thing we want to do is to enable CGI mode. We can do that by typing the following within the Terminal: |
| 45 | + |
| 46 | +```bash |
| 47 | +sudo a2enmod cgi |
| 48 | +``` |
| 49 | + |
| 50 | +Next, we need to tell Apache where our CGI files are present. We can do that by editing the `serve-cgi-bin.conf` file. Do the following: |
| 51 | + |
| 52 | +```bash |
| 53 | +sudo vi /etc/apache2/conf-available/serve-cgi-bin.conf |
| 54 | +``` |
| 55 | + |
| 56 | +The file should read something like this: |
| 57 | + |
| 58 | +``` |
| 59 | +<IfModule mod_alias.c> |
| 60 | + <IfModule mod_cgi.c> |
| 61 | + Define ENABLE_USR_LIB_CGI_BIN |
| 62 | + </IfModule> |
| 63 | +
|
| 64 | + <IfModule mod_cgid.c> |
| 65 | + Define ENABLE_USR_LIB_CGI_BIN |
| 66 | + </IfModule> |
| 67 | +
|
| 68 | + <IfDefine ENABLE_USR_LIB_CGI_BIN> |
| 69 | + ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ |
| 70 | + <Directory "/usr/lib/cgi-bin"> |
| 71 | + AllowOverride None |
| 72 | + Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch |
| 73 | + Require all granted |
| 74 | + </Directory> |
| 75 | + </IfDefine> |
| 76 | +</IfModule> |
| 77 | +``` |
| 78 | + |
| 79 | +We just need to modify the following section from: |
| 80 | + |
| 81 | +``` |
| 82 | +<IfDefine ENABLE_USR_LIB_CGI_BIN> |
| 83 | + ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ |
| 84 | + <Directory "/usr/lib/cgi-bin"> |
| 85 | + AllowOverride None |
| 86 | + Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch |
| 87 | + Require all granted |
| 88 | + </Directory> |
| 89 | +</IfDefine> |
| 90 | +``` |
| 91 | + |
| 92 | +to |
| 93 | + |
| 94 | +``` |
| 95 | +<IfDefine ENABLE_USR_LIB_CGI_BIN> |
| 96 | + ScriptAlias /cgi-bin/ /home/ubuntu/workspace/cgi-bin/ |
| 97 | + <Directory "/home/ubuntu/workspace/cgi-bin"> |
| 98 | + AllowOverride None |
| 99 | + Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch |
| 100 | + AddHandler cgi-script .cgi .py |
| 101 | + Require all granted |
| 102 | + </Directory> |
| 103 | +</IfDefine> |
| 104 | +``` |
| 105 | + |
| 106 | +Save this file and restart Apache. |
| 107 | + |
| 108 | +Once Apache restarts, navigate to `https://<workspace-name>-<your-username>.c9.io/cgi-bin/test.py` (replacing `<workspace-name>` and `<your-username>` with your workspace name and your username of course) and see what happens? In case you're seeing something like **Internal Server Error** we need to make the files under cgi-bin to be executable. |
| 109 | + |
| 110 | +You can do that by typing |
| 111 | + |
| 112 | +```bash |
| 113 | +chmod +x -R /home/ubuntu/workspace/cgi-bin |
| 114 | +``` |
| 115 | + |
| 116 | +Restart Apache one last time and you should be able to see **Sample CGI Script**. |
0 commit comments