Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit f1032f5

Browse files
author
Ivar Pruijn
committed
Merge pull request #169 from c9/python-cgi
Add howto for creating CGI apps using python
2 parents 370477b + 566986a commit f1032f5

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed

out/writing_a_php_app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<li class="nav-submenu-item"><a href="writing_a_python_app.html">Writing a Python App</a>
122122
<ul>
123123
<li class="nav-submenu-item"><a href="installing_python_packages.html">Installing Python Packages</a></li>
124+
<li class="nav-submenu-item"><a href="running_python_cgi.html">Running a Python CGI Server</a></li>
124125
</ul>
125126
</li>
126127
<li class="nav-submenu-item"><a href="writing_a_ruby_app.html">Writing a Ruby App</a>

recentFiles.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"filename":"faq_general","mtime":1422453385000,"pageTitle":"FAQ: General"},{"filename":"setting_up_postgresql","mtime":1422453385000,"pageTitle":"PostgreSQL"},{"filename":"picking_a_plan","mtime":1422453317000,"pageTitle":"Picking a Plan"},{"filename":"frameworks_wordpress","mtime":1421859765000,"pageTitle":"Getting Started with Wordpress"},{"filename":"deploying_code","mtime":1421769253000,"pageTitle":"Deploying Your Code"}]
1+
[{"filename":"running_python_cgi","mtime":1424257208000,"pageTitle":"Running a Python CGI server"},{"filename":"ide_preferences","mtime":1422860320000,"pageTitle":"IDE Preferences"},{"filename":"faq_python","mtime":1422860320000,"pageTitle":"FAQ: Python"},{"filename":"faq_ssh","mtime":1422860320000,"pageTitle":"FAQ: SSH workspaces"},{"filename":"faq_php","mtime":1422860320000,"pageTitle":"FAQ: PHP"}]
38.2 KB
Loading
30.5 KB
Loading
25.7 KB
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
[![New Run Configuration](./resources/images/newRunConfiguration-new.png)]{: #NewRunConfiguration}
32+
33+
then select Apache httpd (PHP, HTML)
34+
35+
[![Select Apache Run Configuration](./resources/images/apacheRunConfiguration.png)]{: #ApacheRunConfiguration}
36+
37+
You can save this configuration by entering a name to the right of the `Command:` label like shown below.
38+
39+
[![Save Run Configuration](./resources/images/saveRunConfiguration.png)]{: #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**.

templates/default/toc.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
ul
136136
li.nav-submenu-item
137137
a(href="installing_python_packages.html") Installing Python Packages
138+
li.nav-submenu-item
139+
a(href="running_python_cgi.html") Running a Python CGI Server
138140
li.nav-submenu-item
139141
a(href='writing_a_ruby_app.html') Writing a Ruby App
140142
ul

0 commit comments

Comments
 (0)