所以我一直在试图弄清楚这个问题。我在Ubuntu 16.04上,有两个不同的django项目。这里的目的是在同一服务器上的两个不同域上并排运行两个域,使用“sites-(enabled | available)”文件夹中的两个不同文件。一个工作正常(已经有一段时间了,第二个网站是新的)。第二个不是。我猜这与我的wsgi设置有关。为了使事情复杂化,我也使用了let-encrypt。我确信这不是问题所在,但我会发布他们的混蛋以确定。
项目一位于目录/home/arlyon/arlyon
,nr2位于目录/home/arlyon/threeswords
。
000-default.conf
<VirtualHost *:80>
ServerName www.site1.com
ServerAlias site1.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/arlyon
WSGIScriptAlias / /home/arlyon/arlyon/arlyon/apache/wsgi.py
WSGIDaemonProcess arlyon python-home=/home/arlyon/arlyon/venv python-path=/home/arlyon/arlyon
WSGIProcessGroup arlyon
Alias /static/ /home/arlyon/arlyon/static/
<Directory /home/arlyon/arlyon/static>
Require all granted
</Directory>
Alias /media/ /home/arlyon/arlyon/media/
<Directory /home/arlyon/arlyon/media>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
000 - 缺省 - LE-SSL
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.site1.com
ServerAlias site1.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/arlyon
# user/project/projectapp
# project and home dir are the same
WSGIScriptAlias / /home/arlyon/arlyon/arlyon/apache/wsgi.py
Alias /static/ /home/arlyon/arlyon/static/
<Directory /home/arlyon/arlyon/static>
Require all granted
</Directory>
Alias /media/ /home/arlyon/arlyon/media/
<Directory /home/arlyon/arlyon/media>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/www.site1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.site1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
site2.conf
<VirtualHost *:80>
ServerName www.site2.com
ServerAlias *.site2.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/threeswords
WSGIScriptAlias / /home/arlyon/threeswords/threeswords/wsgi.py
WSGIDaemonProcess threeswords python-path=/home/arlyon/threeswords python-home=/home/arlyon/threeswords/venv
WSGIProcessGroup threeswords
<Directory "/home/arlyon/threeswords/threeswords/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /home/arlyon/threeswords/static/
<Directory /home/arlyon/threeswords/static>
Require all granted
</Directory>
Alias /media/ /home/arlyon/threeswords/media/
<Directory /home/arlyon/threeswords/media>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/ts_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site2.no [OR]
RewriteCond %{SERVER_NAME} =*.site2.no
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
threeswords勒ssl.conf中
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.site2.no
ServerAlias *.site2.no
ServerAdmin arlyon@me.com
DocumentRoot /home/arlyon/threeswords
WSGIScriptAlias / /home/arlyon/threeswords/threeswords/wsgi.py
Alias /static/ /home/arlyon/threeswords/static/
<Directory /home/arlyon/threeswords/static>
Require all granted
</Directory>
Alias /media/ /home/arlyon/threeswords/media/
<Directory /home/arlyon/threeswords/media>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/ts_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site2.no
# Some rewrite rules in this file were were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
SSLCertificateFile /etc/letsencrypt/live/www.site1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.site1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
我的wsgi配置存在一些问题导致服务器无法导入myproject。但是,我不知道为什么会这样。这是违规的 wsgi.py 。
from __future__ import absolute_import, unicode_literals
import site
import os
from django.core.wsgi import get_wsgi_application
site.addsitedir('/home/arlyon/threeswords/venv/lib/python3.5/site-packages')
os.environ["DJANGO_SETTINGS_MODULE"] = "threeswords.settings.dev"
application = get_wsgi_application()
venv正指向正确的地方。尝试设置模块时,会抛出导入错误。最后这是错误:
[Mon Nov 07 23:27:29.484760 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod_wsgi (pid=30535): Target WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py' cannot be loaded as Python module.
[Mon Nov 07 23:27:29.484844 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod_wsgi (pid=30535): Exception occurred processing WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py'.
[Mon Nov 07 23:27:29.485206 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] Traceback (most recent call last):
[Mon Nov 07 23:27:29.485321 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/home/arlyon/threeswords/threeswords/wsgi.py", line 19, in <module>
[Mon Nov 07 23:27:29.485349 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] application = get_wsgi_application()
[Mon Nov 07 23:27:29.485369 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Mon Nov 07 23:27:29.485379 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] django.setup()
[Mon Nov 07 23:27:29.485397 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup
[Mon Nov 07 23:27:29.485407 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Nov 07 23:27:29.485425 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__
[Mon Nov 07 23:27:29.485435 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] self._setup(name)
[Mon Nov 07 23:27:29.485452 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup
[Mon Nov 07 23:27:29.485462 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] self._wrapped = Settings(settings_module)
[Mon Nov 07 23:27:29.485480 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__
[Mon Nov 07 23:27:29.485489 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon Nov 07 23:27:29.485507 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Mon Nov 07 23:27:29.485517 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] return _bootstrap._gcd_import(name[level:], package, level)
[Mon Nov 07 23:27:29.485534 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485554 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485573 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485592 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon Nov 07 23:27:29.485612 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485631 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485650 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485669 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon Nov 07 23:27:29.485688 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485707 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485736 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485774 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] ImportError: No module named 'threeswords'
[Mon Nov 07 23:27:29.700491 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod_wsgi (pid=30535): Target WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py' cannot be loaded as Python module., referer: https://www.site2.no/
[Mon Nov 07 23:27:29.700569 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod_wsgi (pid=30535): Exception occurred processing WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py'., referer: https://www.site2.com/
[Mon Nov 07 23:27:29.700934 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] Traceback (most recent call last):, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701050 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/home/arlyon/threeswords/threeswords/wsgi.py", line 19, in <module>, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701061 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] application = get_wsgi_application(), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701081 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701091 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] django.setup(), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701110 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701120 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701139 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701149 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] self._setup(name), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701183 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701193 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] self._wrapped = Settings(settings_module), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701212 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701222 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod = importlib.import_module(self.SETTINGS_MODULE), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701240 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701250 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] return _bootstrap._gcd_import(name[level:], package, level), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701285 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701306 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701326 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701346 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701366 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701386 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701405 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701425 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701445 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701465 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701484 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701523 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] ImportError: No module named 'threeswords', referer: https://www.site2.com/
到目前为止,我没有尝试过(我的谷歌是紫色链接的黑暗海洋)已经设法为我解决了这个问题。它不是权限,它不是组。它可以很好地导入到python中。我不明白是什么导致它。
提前谢谢大家。
答案 0 :(得分:3)
您的443个WSGIProcessGroup
条目中都缺少VirtualHost
指令。对于这两个请求,请求不被委托给正确的mod_wsgi守护程序进程组。由于Python模块搜索路径是作为守护程序进程组定义的一部分设置的,因此它将无法找到您的应用程序代码。