Apache CGI in user directory “End of script output before headers”

后端 未结 8 747
心在旅途
心在旅途 2021-02-04 17:10

I know there are some questions about this topic, but none seems to solve my issue. See this or this or this.

I\'m on Linux, Fedora21, and I\'m trying to enable per user

8条回答
  •  走了就别回头了
    2021-02-04 17:28

    Try running your script as the apache user.

    There are a number of reasons this could be happening. @JimB mentions problem with the shebang. @premganz mentions problems with debug print statements that fire before your Content-Type string is written. In my case it was a db connection failure. It could be any other problem.

    I found the best way to debug this is to run the script directly as the apache user on the command line, and see what errors it gives.

    How to run your script as the Apache user

    Assuming you're running apache2, and the apache user is www-data, and your cgi script is /myapp/myreport.py - you can do the following.

    1. Allow logins as the www-data user, by changing its default shell. Edit /etc/passwd (temporarily)

      sudo su - 
      cd /etc
      cp -p passwd passwd.2017-10-22
      
      emacs passwd     # edit file - use your favorite editor
      

      Change:

      www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
      

      To:

      www-data:x:33:33:www-data:/var/www:/bin/bash
      
    2. Log in as the www-data user

      sudo su - www-data
      
    3. Set any environment vars. Convert your apache SetEnv statements to export statements. In my case, my apache config directory settings has these vars set

      SetEnv PYTHONPATH /myapp/lib
      SetEnv VCONF /myapp/conf/prod.yaml
      

      Run them as

      export PYTHONPATH=/myapp/lib
      export VCONF=/myapp/conf/prod.yaml
      
    4. Then try your cgi script

      cd /myapp
      ./myreport.py
      
    5. You should see whatever error Apache is experiencing. FIX THEM.

    6. Set the www-data user's default shell back to /usr/sbin/nologin

提交回复
热议问题