python CGI 编程环境搭建

来源:互联网 发布:现在淘宝什么最赚钱 编辑:程序博客网 时间:2024/04/23 16:04


1. 安装apr-1.4.6 进入安装目录 解压apr-1.4.6.tar.gz
cd /opt/apr-1.4.6/ 
安装及编译
./configure --prefix=/usr/local/apr
make && make install


2. 安装apr-util-1.5.4 解压:tar -jxvf apr-util-1.5.4.tar.bz2
 
后面可能还会出现找不到apr-util
进入安装目录
cd /opt/apr-util-1.5.4/
安装及编译
./configure --prefix=/usr/local/apr-util -with- apr=/usr/local/apr/bin/apr-1-config
make && make install
  
3. 安装pcre-8.38 解压:tar -jxvf pcre-8.38.tar.bz2


后面可能还会出现找不到pcre
进入安装目录
cd /opt/pcre-8.38/
安装及编译


./configure --prefix=/usr/local/pcre
make && make install




4. 重新安装apache


进入安装目录
cd /opt/httpd-2.4.10/
安装及编译
需要用--with参数指定我们刚才安装的依赖包位置
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre
make && make install




5.安装mod_wsgi 


解压
./configure --with-python=/usr/bin/python --with-apxs=/usr/local/apache/bin/apxs
make &&make install






6.编译mod_cgi.so  因为要执行CGI程序,否则会一二进制的格式显示页面


在/opt/httpd-2.4.10/modules/generators 下,用命令:
/usr/local/apache/bin/apxs -i -a -c mod_cgi.c 来编译


7.编辑httpd.conf 配置文件


vim /usr/local/apache/conf/httpd.conf


a.引用mod_cgi.so 
  LoadModule cgi_module         modules/mod_cgi.so


b.配置服务ip和端口
  ServerName 192.168.19.110:80


c.指定cgi的执行文件目录
  ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"


d.指定权限
<Directory "/usr/local/apache/cgi-bin/">
   Options Indexes MultiViews
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all
   AddHandler cgi-script .cgi .pl .py
</Directory>


8.到/usr/local/apache/cgi-bin/目录下建立自己的.py文件


如:vim test.py,内容如下:


#!/usr/bin/python
# -*- coding: UTF-8 -*-


print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'


9.给test.py添加执行权限:chmod o+x /usr/local/apache/cgi-bin/test.py


10.重启apace:/usr/local/apache/bin/apachectl restart


11.在浏览器输入:http://localhost:80/cgi-bin/test.py,将会显示html经过解析之后的页面
0 0