配置 Apache WSGI 和 Django 总是出现“500 Internal Server Error” - Apache

Config Apache WSGI and Django always getting `500 Internal Server Error` - Apache

我正在尝试使用 Apache2 为 Django 应用程序 (helios) 提供服务,但我总是在尝试访问服务器时收到此消息“There was an error while handling your request.。服务器具有以下命名 debian.serverdomain 我尝试使用 http://debian.serverdomain http://debian.serverdomain:80http://<hostname ip>:80 进行访问,但没有成功。

apache 配置没有显示错误并且 wsgi 出现在 apache 状态。

apache 配置文件:helios.conf

Alias /favicon.ico /var/www/helios-server/sitestatic/favicon.ico

AliasMatch ^/([^/]*\.css) /var/www/helios-server/static/styles/

Alias /media /var/www/helios-server/sitestatic
Alias /static /var/www/helios-server/sitestatic
Alias /booth /var/www/helios-server/sitestatic/booth
Alias /verifier /var/www/helios-server/sitestatic/verifier


<VirtualHost *:80>

    DocumentRoot /var/www/helios-server
    <Directory /var/www/helios-server>
            Options All
            AllowOverride All
            Require all granted
    </Directory>

    <Directory /var/www/helios-server/static>
            Options All
            AllowOverride All
            Require all granted
    </Directory>

    <Directory /var/www/helios-server/media>
            Options All
            AllowOverride All
            Require all granted
    </Directory>

    <Directory /var/www/helios-server>
            Options All
            AllowOverride All
            Require all granted
           <Files wsgi.py>
                    Order deny,allow
                    Allow from all
                    Require all granted
            </Files>
    </Directory>

    ErrorLog /var/www/helios-server/apache.error.log
    CustomLog /var/www/helios-server/access.log combined
</VirtualHost>

WSGIApplicationGroup %{GLOBAL}

WSGIDaemonProcess helios-server display-name=%{GROUP} python-home=/var/www/helios-server/.venv python-path=/var/www/helios-server:/.venv/lib/python2.7:/.venv/lib/python2.7/site-packages
WSGIProcessGroup helios-server
WSGIScriptAlias / /var/www/helios-server/wsgi.py

Apache 错误日志

[Wed Mar 03 20:58:02.008182 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582] 2021-03-03 20:58:01,962 ERROR Internal Server Error: /
[Wed Mar 03 20:58:02.008234 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582] Traceback (most recent call last):
[Wed Mar 03 20:58:02.008239 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
[Wed Mar 03 20:58:02.008242 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     response = get_response(request)
[Wed Mar 03 20:58:02.008245 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
[Wed Mar 03 20:58:02.008249 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     response = self.process_exception_by_middleware(e, request)
[Wed Mar 03 20:58:02.008252 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
[Wed Mar 03 20:58:02.008255 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Wed Mar 03 20:58:02.008258 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/server_ui/views.py", line 26, in home
[Wed Mar 03 20:58:02.008260 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     user = get_user(request)
[Wed Mar 03 20:58:02.008263 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/helios_auth/security/__init__.py", line 96, in get_user
[Wed Mar 03 20:58:02.008266 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     if not request.session.has_key('csrf_token') or (type(request.session['csrf_token']) != str and type(request.session['csrf_token']) != unicode):
[Wed Mar 03 20:58:02.008269 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 125, in has_key
[Wed Mar 03 20:58:02.008272 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     return key in self._session
[Wed Mar 03 20:58:02.008275 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 207, in _get_session
[Wed Mar 03 20:58:02.008278 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self._session_cache = self.load()
[Wed Mar 03 20:58:02.008280 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 35, in load
[Wed Mar 03 20:58:02.008283 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     expire_date__gt=timezone.now()
[Wed Mar 03 20:58:02.008286 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
[Wed Mar 03 20:58:02.008289 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     return getattr(self.get_queryset(), name)(*args, **kwargs)
[Wed Mar 03 20:58:02.008292 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
[Wed Mar 03 20:58:02.008302 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     num = len(clone)
[Wed Mar 03 20:58:02.008304 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
[Wed Mar 03 20:58:02.008307 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self._fetch_all()
[Wed Mar 03 20:58:02.008310 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 1121, in _fetch_all
[Wed Mar 03 20:58:02.008313 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self._result_cache = list(self._iterable_class(self))
[Wed Mar 03 20:58:02.008316 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
[Wed Mar 03 20:58:02.008318 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
[Wed Mar 03 20:58:02.008321 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 887, in execute_sql
[Wed Mar 03 20:58:02.008324 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     cursor = self.connection.cursor()
[Wed Mar 03 20:58:02.008327 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
[Wed Mar 03 20:58:02.008329 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     return self._cursor()
[Wed Mar 03 20:58:02.008332 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
[Wed Mar 03 20:58:02.008335 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self.ensure_connection()
[Wed Mar 03 20:58:02.008338 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
[Wed Mar 03 20:58:02.008341 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self.connect()
[Wed Mar 03 20:58:02.008343 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
[Wed Mar 03 20:58:02.008346 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     six.reraise(dj_exc_type, dj_exc_value, traceback)
[Wed Mar 03 20:58:02.008349 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
[Wed Mar 03 20:58:02.008352 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self.connect()
[Wed Mar 03 20:58:02.008354 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
[Wed Mar 03 20:58:02.008357 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     self.connection = self.get_new_connection(conn_params)
[Wed Mar 03 20:58:02.008362 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
[Wed Mar 03 20:58:02.008365 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     connection = Database.connect(**conn_params)
[Wed Mar 03 20:58:02.008368 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]   File "/var/www/helios-server/.venv/lib/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
[Wed Mar 03 20:58:02.008370 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582]     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
[Wed Mar 03 20:58:02.008373 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582] OperationalError: FATAL:  role "www-data" does not exist
[Wed Mar 03 20:58:02.008376 2021] [wsgi:error] [pid 19986:tid 140210251364096] [remote 2001:690:2100:606::1007:36582] 

'deploy'bash脚本:

#!/bin/bash
if [[ `uname` == 'Linux' ]]; then
cp /var/www/helios-server/deploy/apache/helios.conf /etc/apache2/sites-available
sudo apachectl configtest
sudo chmod 644 wsgi.py
sudo a2enmod rewrite
sudo a2dissite 000-default.conf
sudo a2ensite helios.conf
sudo a2enmod wsgi
sudo systemctl reload apache2
sudo chown www-data -R /var/www/helios-server
sudo chown www-data:www-data /var/www/helios-server -R
sudo chmod -R 750 /var/www/helios-server/*
fi


输出(似乎没问题):

(.venv) debian@debian:/var/www/helios-server$ sudo bash deploy/apache/apache2-service.sh
Syntax OK
Module rewrite already enabled
Site 000-default already disabled
Site helios already enabled
Module wsgi already enabled

sudo ufw status                                             
Status: active

To                         Action      From
--                         ------      ----
80,443,8000/tcp            ALLOW       Anywhere                   
22/tcp                     ALLOW       Anywhere                   
80,443,8000/tcp (v6)       ALLOW       Anywhere (v6)              
22/tcp (v6)                ALLOW       Anywhere (v6)       


(.venv) debian@debian:/var/www/helios-server$ systemctl status apache2.service                            
● apache2.service - The Apache HTTP Server                                                                
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)                  
   Active: active (running) since Wed 2021-03-03 15:23:20 WET; 1h 40min ago                               
     Docs: https://httpd.apache.org/docs/2.4/                                                             
  Process: 27240 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)                  
 Main PID: 10932 (apache2)                                                                                
    Tasks: 77 (limit: 4700)                                                                               
   Memory: 29.7M                                                                                          
   CGroup: /system.slice/apache2.service                                                                  
           ├─10932 /usr/sbin/apache2 -k start                                                             
           ├─27247 (wsgi:helios-serv -k start                                                             
           ├─27248 /usr/sbin/apache2 -k start                                                             
           └─27249 /usr/sbin/apache2 -k start   

项目树:

(.venv) debian@debian:/var/www/helios-server$ tree
.
├── CHANGELOG.md
├── CONTRIBUTORS.txt
├── INSTALL.md
├── LICENSE
├── Procfile
├── README.md
├── access.log
├── apache.error.log
├── build-helios-main-site-js.txt
├── deploy
│   ├── DEPLOY.md
│   ├── apache
│   │   ├── apache2-service.sh
│   │   └── helios.conf
│   └── supervisor
│       ├── DEPLOY.md
│       ├── celery-worker.conf
│       └── supervisor-service.sh
├── deploy-staging.sh
├── email_debug.py
├── extract-passwords-for-email.py
├── favicon.ico
├── helios [error opening dir]
├── helios-install.sh
├── helios_auth 
├── heliosbooth 
├── heliosverifier 
├── manage.py
├── migrate-to-3.5.sql
├── migrate-to-3.5.txt
├── requirements.txt
├── reset.sh
├── runtime.txt
├── selenium 
├── server_ui 
├── settings.py
├── settings.pyc
├── templates 
├── urls.py
├── urls.pyc
└── wsgi.py

``

[解决]问题是数据库权限,我没有创建apache用户www-data。好像是个老问题了,不过命令还是有的:

sudo -u postgres createuser www-data
sudo -u postgres psql -c 'alter user "www-data" with createdb;' postgres
sudo -u postgres psql helios -c 'GRANT ALL PRIVILEGES ON ALL TABLES in SCHEMA public to "www-data";' postgres
sudo -u postgres psql helios -c 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to "www-data";' postgres
sudo -u postgres psql helios -c 'GRANT ALL PRIVILEGES ON ALL FUNCTIONS  IN SCHEMA public to "www-data";' postgres