You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

lighttpd+web.py+FastCGI部署异常求助(Python3/Python2均出错)

Troubleshooting Your web.py + LightTPD + FastCGI Deployments

Let's tackle your issues one at a time, starting with the Python 2 deployment since that's where you're currently stuck getting a 500 error.

Python 2 Deployment: 500 Error with No Logs

First, let's clarify: you don't need to manually start the FastCGI process—LightTPD is designed to spawn and manage these processes automatically based on your config. Here's how to fix your setup:

1. Update Your web.py Script for FastCGI

Your current script only runs the built-in web.py server when executed directly (python code.py), but LightTPD needs it to run as a FastCGI server. Modify your code to handle both cases:

#!/usr/bin/env python
import web
from flup.server.fcgi import WSGIServer  # Add this import

urls = ('/(.*)', 'Index')
application = web.application(urls, globals())
web.config.debug = True

class Index:
    def GET(self, name=''):
        return 'Hello World'
    def POST(self, name=''):
        return 'Hello World'

if __name__ == '__main__':
    application.run()  # Runs built-in server for testing
else:
    # Runs FastCGI server when invoked by LightTPD
    WSGIServer(application.wsgifunc()).run()

2. Fix LightTPD Config Issues

Your fastcgi.server config has a few syntax and logic issues. Update it to this:

server.modules = ( 
    "mod_access", 
    "mod_alias", 
    "mod_accesslog", 
    "mod_compress",
    "mod_fastcgi", 
    "mod_rewrite"
)

server.port = 8081
server.document-root = "/python/metrics_interlayer/"
server.errorlog = "/python/metrics_interlayer/light_error.log"
accesslog.filename = "/python/metrics_interlayer/light_access.log"
server.debug = 2  # Enable debug logging temporarily

fastcgi.server = ( "/code.py" =>
    ( "python-fcgi" =>
        (
            "socket" => "/tmp/fastcgi.python.socket",
            "bin-path" => "/python/metrics_interlayer/code.py",
            "max-procs" => 1,
            "bin-environment" => (
                "PYTHONPATH" => "/python/metrics_interlayer/"
            ),
            "check-local" => "disable",  # Critical: Skip file existence check
            "uid" => "www-data",  # Replace with your LightTPD user (e.g., _lighttpd on macOS)
            "gid" => "www-data"
        )
    )
)

url.rewrite-once = ( "^/(.*)$" => "/code.py/$1" )

Key fixes here:

  • Added check-local => "disable": LightTPD won't check if the requested path exists on disk (since we're rewriting everything to code.py).
  • Added bin-environment: Ensures your script can find any local dependencies.
  • Added uid/gid: Ensures the FastCGI process runs with the same permissions as LightTPD (adjust to match your system's user/group).
  • Enabled server.debug to get more detailed logs if issues persist.

3. Verify Permissions

  • Make your script executable: chmod +x /python/metrics_interlayer/code.py
  • Ensure LightTPD's user has read access to your script directory and write access to the error log directory. For testing, you can temporarily set the error log to /tmp/light_error.log which is usually world-writable.

4. Restart LightTPD

After making these changes, restart LightTPD to apply the config. Check the debug logs for any new errors—this should help pinpoint if there's still an issue with process spawning or permissions.


Python 3 Deployment: TypeError: str expected, not bool

The error you're seeing is due to outdated web.py code that doesn't handle Python 3's os.environ correctly (Python 3 requires all environment keys/values to be strings, but older web.py versions pass a boolean here). Here's how to fix it:

  1. Upgrade web.py: Install the latest development version (which has better Python 3 support):
    pip3 install web.py==0.40.dev0
    
  2. Use flup3 for Python 3: The original flup package only supports Python 2. Install the Python 3 compatible version:
    pip3 install flup3
    
  3. Adjust your script for Python 3: Similar to the Python 2 fix, update your script to use flup3 and handle FastCGI:
    #!/usr/bin/env python3
    import web
    from flup.server.fcgi import WSGIServer
    
    urls = ('/(.*)', 'Index')
    application = web.application(urls, globals())
    web.config.debug = True
    
    class Index:
        def GET(self, name=''):
            return 'Hello World'
        def POST(self, name=''):
            return 'Hello World'
    
    if __name__ == '__main__':
        application.run()
    else:
        WSGIServer(application.wsgifunc()).run()
    
  4. Update LightTPD config for Python 3: Adjust the bin-path to point to your Python 3 script, and ensure the bin-environment uses the correct Python 3 path if needed.

内容的提问来源于stack exchange,提问作者Alexander Rainchik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:24:09