嵌入式mod_wsgi与virtualenv

时间:2016-11-18 07:50:33

标签: python django apache ubuntu mod-wsgi

这个问题的结论

1.首先我想做什么。

我想只允许Django中的某些用户上传文件。我可以在Django Web框架中编写代码来做到这一点。但是如果您将上传文件夹设置为r__ permssions,则无法首先上传文件。我只是将我的上传文件夹设置为drwxrwxrwx 777 permssions,让我的Django用户可以上传文件。因为出于安全原因,将你的文件夹保留为777 permssion似乎是个坏主意。我想将我的媒体文件夹设置为774或764。

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

我认为只有嵌入式模式才有可能使用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'。使用带有Apache AuthUserFile的htpasswd文件时会导致这种减速。但是如果你让Django进行身份验证,它就会绕过平面文件的扫描。因此,当您在mod wsgi中挂钩Django身份验证时,情况并非如此。

在下面,我把链接如何解决当你有一个由mod_wsgi版本和python版本引起的错误时,并解释了我如何通过@Graham Dumpleton修复我的代码来设置嵌入模式。

设置嵌入模式时如何解决错误的步骤

首先,使用mod_wsgi的守护进程模式我没有遇到任何问题。

我想在virtualenv中使用mod_wsgi的嵌入模式。我想使用嵌入式模式的原因是使用mod_wsgi进行apache基本或摘要式身份验证。根据{{​​3}},'默认情况下,auth提供程序是在Python创建的第一个解释器的上下文中执行的。即。,'%{GLOBAL}'并始终在Apache子进程中,从不在守护进程中。我知道我可以使用apache基本或摘要身份验证,只使用mod_wsgi的嵌入模式。

1.error因为低版本的mod_wsgi

如果你在ubuntu 14或16中通过apt-get安装libapache2-mod-wsgi-py3,它的版本是3.4 根据这个this,python 3.4需要mod_wsgi版本4.2+。我按照该链接中的说明安装了mod_wsgi 4.5.7。没有更多的错误。

2.使用WSGIPythonPath

的错误

我按照link中的说明操作。我一直犯错,我发现它与virtualenv不起作用。我找到了django doc。我认为这是解决我的问题的一种方法,但它仍然无法正常工作。 我无法理解为什么它不起作用,找不到比我提到的链接更好的资源。

<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> 

但如果我改变一点点使用如下所示的守护进程模式,它就可以了!

<VirtualHost *:80>

WSGIDaemonProcess cango python-path=/home/cango/myproject:home/cango/myvenv/lib/Python3.5/site-packages
WSGIProcessGroup cango
WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py

#WSGIPythonPath home/cango/myvenv/lib/python3.5/site-packages

        <Directory /home/cango/myproject/myproject>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
</VirtualHost>
除了守护进程模式的几行之外,

完全相同。

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

1. WSGIPythonPath和WSGIPythonHome都不应该在VirtualHost中。

我在/ etc / apache2 / mods-available

下的wsgi.conf中添加了WSGIPythonPath

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

我在/ etc / apache2 / mode下保留了wsgi.load-可用,如下所示

wsgi.load

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

我在VirtualHost设置中删除了WSGIPythonPath。

000-default.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>

现在我没有收到我之前遇到的错误&#39; WSGIPythonPath错误&#39;。但当我点击我的IP地址时,它显示500内部服务器错误。如果我使用python manage.py runserver 0.0.0.0:8000测试并输入我的IP地址:8000 /,我可以看到Django页面。

apache2ctl -M我可以看到wsig_module

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)

service apache2 status

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 restart,则会导致错误。我似乎无法通过禁用wsgi_mod重新启动,因为我设置了WSGIScriptAlias

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应用程序中使用守护进程模式。

从我上面提到的链接,它说&#34;如果身份验证检查是利用某些Python Web框架的内部,建议使用该Web框架的应用程序也可以运行嵌入模式和相同的应用程序组&#34;

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

--------------------------由其他评论修正-------------- --------------

我检查了apache中的错误日志。有一个错误,似乎在我的django项目中加载wsgi脚本有问题。好吧,上面,我更改了文件夹名称&#39; jaemyun&#39;到了我的项目&#39;。有一个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项目路径。现在它有效!

0 个答案:

没有答案