基于virtualen的mod\wsgi嵌入式模式

2024-09-30 14:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题的结论

一开始我想做什么。在

我想只允许Django的一些用户上传文件。我可以在Django web框架中编写代码来实现这一点。但是,如果您将upload folder设置为r\uuuPermissions,则不能首先上载文件。我只需将上传文件夹设置为drwxrwxrwx777权限,让Django中的用户可以上传文件。出于安全原因,将文件夹保留为777 permission似乎是个坏主意。我想把我的媒体文件夹设置为774或764。在

我搜索apache身份验证和mod wsgi是从我错误的理解开始的。我想我可以允许django用户在ubuntu中拥有所有者或组权限。例如,如果您在Django中的用户位于名为“tester”的组中,则可以访问具有“drwxrwxr_uxtester”的文件夹。在

我认为只有嵌入式模式才有可能使用apache身份验证。但那是我的误解。在

总之,我可以通过在apache默认用户或使用WSGIDaemonProcess选项设置的(更安全的)用户/组之后设置所有者和组权限来解决我的问题。然后在Django中管理哪些用户可以上载媒体文件中的文件(我将使用PermissionMixin类)

另外,这也是我在这里理解的。在

1)大多数情况下建议使用WSGIDaemon模式。check Graham Dumpleton's article in here。如果您只使用简单的代码,那么嵌入式模式可能会更高效。在

2)'you can expect to see slowdowns once you get above a few hundred entries, and may wish to consider a different authentication method at that time'。这种减速是在将htpasswd文件与apacheauthuserfile一起使用时导致的。但是如果让Django进行身份验证,它将绕过对平面文件的扫描。因此,在mod wsgi中钩住Django身份验证时就不是这样了。在

下面,我将链接how solve when you have a error when you have a error is the version of mod unwsgi and version of python引起,并解释了我如何通过@Graham Dumpleton修复代码来设置嵌入式模式。在

设置嵌入式模式时如何解决错误的过程

首先,我在使用mod\wsgi的守护程序模式时没有问题。在

我想在virtualenv中使用mod\wsgi的嵌入式模式。我想使用嵌入式模式的原因是为了在mod\wsgi中使用apachebasic或摘要式身份验证。根据this'默认情况下,身份验证提供者在Python创建的第一个解释器的上下文中执行。例如,'%{GLOBAL}'并且总是在Apache子进程中,从不在守护进程'中。我知道我可以使用apachebasic或digest身份验证,只使用mod\wsgi的嵌入式模式。在

1.错误,因为mod\wsgi的低版本

如果您在ubuntu14或16中通过apt get安装libapache2-mod-wsgi-py3,那么它的版本是3.4 根据这个link,python3.4需要mod\wsgi版本4.2+。我按照那个链接中的说明安装了mod_wsgi4.5.7。没有更多的错误。在

2.WSGIPythonPath错误

我遵循了django doc中的说明。我一直在犯错误,我发现它不适用于virtualenv。我找到了这个article。我以为这是解决我问题的方法,但还是没用。我不明白为什么它不起作用,也找不到比我提到的链接更好的资源。

<VirtualHost *:80>

WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py

WSGIPythonPath home/cango/myvenv/lib/python3.5/site-packages
# I also tried /home/~ I put / at very beginning. It didn't work too.

        <Directory /home/cango/myproject/myproject>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
</VirtualHost> 

但如果我稍微改变一下使用下面的守护程序模式,它就可以工作了!在

^{pr2}$

完全相同,除了守护程序模式的几行。在

-------------------当我根据评论申请时-------------------

1.WSGIPythonPath和WSGIPythonHome都不应该位于虚拟主机内部。

Python补充道wsgi.conf文件在/etc/apache2/mods下可用

在wsgi.conf文件在

<IfModule mod_wsgi.c>
    WSGIPythonHome /usr/local/pythonenv
    #WSGIPythonPath /usr/local/pythonenv/lib/python3.5/site-packages
    #From comment, I don't need to add WSGIPythonPath, WSGIPythonHome is enough to set

我留下来了wsgi.load文件在/etc/apache2/模式下可用,如下所示

在wsgi.load文件在

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so

我删除了VirtualHost设置中的WSGIPythonPath。在

000个-默认.conf在

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py

        <Directory /home/cango/myproject/myproject>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
</VirtualHost>

现在我没有得到一个在'WSGIPythonPath something error'之前的错误。但是当我点击我的IP地址时,它显示500个内部服务器错误。如果我用python测试管理.py运行服务器0.0.0.0:8000并输入我的IP地址:8000/,我可以看到Django页面。在

apache2ctl-M我可以看到wsig_模块

Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_event_module (shared)
 negotiation_module (shared)
 setenvif_module (shared)
 status_module (shared)
 wsgi_module (shared)

服务apache2状态

apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Sat 2016-11-19 03:19:36 UTC; 2min 25s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 19444 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 18407 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
  Process: 19471 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 55
   Memory: 43.2M
      CPU: 768ms
   CGroup: /system.slice/apache2.service
           ├─19488 /usr/sbin/apache2 -k start
           ├─19491 /usr/sbin/apache2 -k start
           └─19492 /usr/sbin/apache2 -k start

Nov 19 03:19:36 server4.cango.com systemd[1]: Stopped LSB: Apache2 web server.
Nov 19 03:19:36 server4.cango.com systemd[1]: Starting LSB: Apache2 web server...
Nov 19 03:19:36 server4.cango.com apache2[19471]:  * Starting Apache httpd web server apache2
Nov 19 03:19:36 server4.cango.com apache2[19471]:  *
Nov 19 03:19:36 server4.cango.com systemd[1]: Started LSB: Apache2 web server.

如果我命令a2dismod wsgi,然后服务apache2重新启动,则会导致错误。由于我设置了WSGIScriptAlias,我似乎无法重新启动禁用wsgi_mod

Nov 19 03:22:59 server4.cango.com apache2[19746]:  * Starting Apache httpd web server apache2
Nov 19 03:22:59 server4.cango.com apache2[19746]:  *
Nov 19 03:22:59 server4.cango.com apache2[19746]:  * The apache2 configtest failed.
Nov 19 03:22:59 server4.cango.com apache2[19746]: Output of config test was:
Nov 19 03:22:59 server4.cango.com apache2[19746]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-def
Nov 19 03:22:59 server4.cango.com apache2[19746]: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a
Nov 19 03:22:59 server4.cango.com apache2[19746]: Action 'configtest' failed.
Nov 19 03:22:59 server4.cango.com apache2[19746]: The Apache error log may have more information.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Control process exited, code=exited status=1
Nov 19 03:22:59 server4.cango.com systemd[1]: Failed to start LSB: Apache2 web server.
-- Subject: Unit apache2.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit apache2.service has failed.
--
-- The result is failed.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Unit entered failed state.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Failed with result 'exit-code'.
Nov 19 03:23:50 server4.cango.com sudo[19822]:     root : TTY=pts/1 ; PWD=/home/cango/jaemyun ; USER=root ; COMMAND=/bin
Nov 19 03:23:50 server4.cango.com sudo[19822]: pam_unix(sudo:session): session opened for user root by root(uid=0)

我想知道我犯了什么错误。仍然有500个内部服务器错误。

2.没有什么可以阻止您在使用authnz处理程序的同时对WSGI应用程序使用守护程序模式。

在我前面提到的链接中,它说“如果身份验证检查使用了某些Python web框架的内部,建议使用该web框架的应用程序也以嵌入式模式运行,并且是同一个应用程序组”

所以我想我需要在Django框架中使用嵌入式模式。在

-------------------------已由另一条注释修复-------------------------------

我检查了apache中的错误日志。出现了一个错误,在我的django项目中加载wsgi脚本似乎有问题。好吧,在上面,我把文件夹名“jaemyun”改为“myproject”。有一个wsgi.py由Django在/home/cango/jaemyun/jaemyun/path下自动生成的文件。为什么它说它不能加载它???在

No module named 'jaemyun', referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.525510 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] mod_wsgi (pid=11755): Target WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py' cannot be loaded as Python module.
[Sun Nov 20 10:37:24.525600 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] mod_wsgi (pid=11755): Exception occurred processing WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py'.
[Sun Nov 20 10:37:24.525801 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] Traceback (most recent call last):
[Sun Nov 20 10:37:24.525866 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/home/cango/jaemyun/jaemyun/wsgi.py", line 16, in <module>
[Sun Nov 20 10:37:24.525872 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     application = get_wsgi_application()
[Sun Nov 20 10:37:24.525880 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Sun Nov 20 10:37:24.525885 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     django.setup(set_prefix=False)
[Sun Nov 20 10:37:24.525893 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
[Sun Nov 20 10:37:24.525909 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Nov 20 10:37:24.525917 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
[Sun Nov 20 10:37:24.525921 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     self._setup(name)
[Sun Nov 20 10:37:24.525928 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
[Sun Nov 20 10:37:24.525932 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     self._wrapped = Settings(settings_module)
[Sun Nov 20 10:37:24.525939 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
[Sun Nov 20 10:37:24.525943 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Nov 20 10:37:24.525950 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "/usr/local/pythonenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Sun Nov 20 10:37:24.525954 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]     return _bootstrap._gcd_import(name[level:], package, level)
[Sun Nov 20 10:37:24.525961 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Nov 20 10:37:24.525968 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Nov 20 10:37:24.525975 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Sun Nov 20 10:37:24.525982 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Sun Nov 20 10:37:24.525988 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Nov 20 10:37:24.525995 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Nov 20 10:37:24.526002 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Sun Nov 20 10:37:24.526023 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] ImportError: No module named 'jaemyun'
[Sun Nov 20 10:37:24.564690 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] mod_wsgi (pid=11755): Target WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py' cannot be loaded as Python module., referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564729 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] mod_wsgi (pid=11755): Exception occurred processing WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py'., referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564889 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] Traceback (most recent call last):, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564947 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/home/cango/jaemyun/jaemyun/wsgi.py", line 16, in <module>, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564953 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     application = get_wsgi_application(), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564961 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564972 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     django.setup(set_prefix=False), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564981 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564985 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564992 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564997 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     self._setup(name), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565004 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565008 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     self._wrapped = Settings(settings_module), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565015 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565019 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     mod = importlib.import_module(self.SETTINGS_MODULE), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565026 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "/usr/local/pythonenv/lib/python3.5/importlib/__init__.py", line 126, in import_module, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565030 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]     return _bootstrap._gcd_import(name[level:], package, level), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565037 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565044 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565052 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565059 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565066 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565073 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565080 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565097 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] ImportError: No module named 'jaemyun', referer: http://169.56.70.181/

最后一次改变。在

在wsgi.conf文件在

<IfModule mod_wsgi.c>
    WSGIPythonHome /usr/local/pythonenv
    WSGIPythonPath /home/cango/jaemyun

我必须设置WSGIPythonPath并直接指向我的django项目路径。现在它起作用了!在


Tags: inclienthttpwsgilineerrorpidnov