代码之家  ›  专栏  ›  技术社区  ›  spraff

apache2上的Python索引页

  •  0
  • spraff  · 技术社区  · 3 年前

    我想发球 index.py 在Ubuntu 21.04上使用apache2作为索引页。

    我有这个Dockerfile

    FROM ubuntu/apache2:2.4-21.04_beta
    
    RUN rm -rf /var/www/html/*
    ADD html /var/www/html
    RUN chmod +x /var/www/html/*py
    ADD virtual-hosts.conf /etc/apache2/sites-available/000-default.conf
    

    这个 html 目录包含 index.py 这个 RUN chmod 行没有效果(!)但如果我 chmod 在容器运行时手动操作,这并不能解决问题。

    virtual-hosts.conf 基于 this answer (如果这很重要的话,那就是OSX)

    <VirtualHost *:80>
        ServerName pilights
    
        DocumentRoot /var/www/html
    
        LogLevel info ssl:warn
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        AddHandler cgi-script .py
    
        <Directory "/var/www/html">
            Options Indexes MultiViews FollowSymLinks ExecCGI
            AddHandler cgi-script .cgi
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    

    上面链接的答案还说我需要取消注释 LoadModule cgi_module libexec/apache2/mod_cgi.so 在“http配置”中,我将其解释为 /etc/apache2/apache2.conf ,但该文件不包含要取消注释的类似行,添加该行不会有任何作用。

    以上设置不起作用。我浏览到 http://localhost 并获取一个目录索引,其中显示 index.py 但是不执行脚本。

    0 回复  |  直到 3 年前
        1
  •  0
  •   mondayrris    2 年前

    确保以下设置正确。 sudo su 如果您不是root用户

    • 使用python3作为默认值
    python --version
    
    # run only if the result saying the version is not python 3 
    rm -rf /usr/bin/python
    
    # create a symlink
    ln -s /usr/bin/python3 /usr/bin/python
    
    # test
    python --version
    
    apt-get update
    apt-get install python3-pip
    
    • 杂项设置
    # disable multi-threading processes
    a2dismod mpm_event
    
    # give apache explicit permission to run python scripts
    a2enmod mpm_prefork cpi
    
    • 在您的站点conf中应用相同的方法
    <Directory your-absolute-site-directory-of-which-index.py-can-be-found>
        Options +ExecCGI
        DirectoryIndex index.py
        Require all granted
        # Allow local .htaccess to override Apache configuration settings
        AllowOverride all
    </Directory>
    
    AddHandler cgi-script .py
    
    • 然后运行
    service apache2 restart
    
    • 添加 #!/usr/bin/python index.py

    • 导航到您的网站。如果问题仍然存在,触摸 .htaccess nano 其内容如下:

    Options +ExecCGI
    AddHandler cgi-script .py
    DirectoryIndex index.py
    
    • 再次浏览网站,你就可以继续了

    如需更详细的解释,您可以前往 here .