douren6035 2018-11-18 20:27
浏览 97

linux ubuntu 18.04灯php和python cgi

operating system linux ubuntu 18.04 now I've installed the lamp and can run php codes seamlessly. the directory I'm running / var / www / html. but what i want to do is run python cgi. I have reviewed several sources but I have not succeeded.

  • 写回答

1条回答 默认 最新

  • dprxj1995 2018-12-05 16:02
    关注

    I've had some success with the following:

    1). Install libapache2-mod-python

    $ sudo apt install libapache2-mod-python 
    $ sudo a2enmod python 
    

    2). Add this to a new file in /etc/apache2/sites-enabled/ -- changing the relevant parts to whatever suits you

    <VirtualHost *:80>
        # 
        ServerName yourpythonserver.com
    
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/py
    
        <Directory /var/www/py>
            AddHandler mod_python .py
            PythonHandler index
            PythonDebug On
            DirectoryIndex index.py
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    

    3). As you can see from the config above, this will serve requests directed to yourpythonserver.com from /var/www/py - defaulting to index.py

    4). Create the file /var/www/py/index.py with the following:

    #!/usr/bin/env python3
    
    from mod_python import apache
    
    
    def handler(req): 
        ret = ""
        req.content_type = "text/html"
        req.write("<h1>Hello World!</h1>")
        return apache.OK
    

    5). Finally,

    sudo systemctl reload apache2 
    

    This is where it starts to get fuzzy for me, but perhaps someone else out there can help us both out with this... I'm not sure why the name of the function needs to be handler, and I'm not sure how to get things PHP normally provides such as $_SERVER variable. But either way, this works and will serve python files using apache.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献