我正在尝试将 Django 应用程序设置为使用 modwsgi 在 Apache 下运行。我定义了以下虚拟主机:
<VirtualHost *>
ServerName www.domain.com
ServerAlias domain.com
WSGIScriptAlias / /home/domain/apache/django.wsgi
<Directory /home/domain/apache>
Order deny,allow
Allow from all
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L,R=301]
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
最后的重写规则是为了使 www.domain.com 成为规范的站点名称。但还是有什么问题。当我浏览到 > http://domain.com时,我被重定向到 > http://www.domain.com/django.wsgi/。浏览 > http://www.domain.com效果很好。
我的配置中是否缺少某些内容?谢谢。
更新:我将 RewriteRule 更改为
RewriteRule ^django.wsgi/(.*)$ domain.com/$1 [L,R=301].
Run Code Online (Sandbox Code Playgroud)
此更改解决了该问题。如果有人能详细说明为什么 /django.wsgi/ 部分最终出现在 URL 中,我将不胜感激。谢谢!
我正在使用 apache 网络服务器和 mod_wsgi 将请求传输到 django。
$ apache2ctl -v
Server version: Apache/2.4.10 (Raspbian)
Server built: Sep 17 2016 16:40:43
Run Code Online (Sandbox Code Playgroud)
我正在使用这个 apache 站点来声明 django 应用程序:
ServerName example.com
DocumentRoot /srv/webapps/myapp
<Directory /srv/wepapps/myapp/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess example.com python-path=/srv/webapps/myapp:/usr/local/lib/python3.4/dist-packages:/usr/lib/python3/dist-packages
WSGIProcessGroup example.com
WSGIScriptAlias / /srv/webapps/myapp/mysite/wsgi.py
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但如果引发异常,它会显示错误 500,但我的example.com_error.log. 如果我将设置修改为 set Debug = True,我可以在 Web 浏览器中看到错误,但在我的 /var/log/apache2 文件中也可以看到错误。但我真的不想在我的生产环境中保留这个设置。
你知道为什么我必须Debug = True允许 django 写入系统日志吗?
在此先感谢您的回答,如果我在英语方面犯了一些错误,我深表歉意;-)
我已经意识到我的 django 应用程序中的打印语句导致 wsgi 出错并失败。无需遍历我的所有代码并删除/注释掉打印语句,有没有一种方法可以在运行 wsgi 时禁用它们。它需要尽可能简单。
谢谢
我有两个网络应用程序。一个是使用 Django 1.0 开发的,另一个是使用 Django 1.4 开发的。如何使用两个版本的 django 在同一个 apache2 服务器中运行这两个应用程序?有人告诉我一些关于 virtualenv 的事情......我正在使用 mod_wsgi。
谢谢
我在 redhat 上的同一个 apache 实例下配置了几个虚拟主机:
对于每个虚拟主机WSGIScriptAlias设置都指向激活虚拟环境的 python 文件:
activate_this = '/path_to_the_virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
Run Code Online (Sandbox Code Playgroud)
现在,我计划将其中一个项目升级到 python-2.7,另一个升级到 python-3.x。我知道我可以有不同的虚拟环境,单独的 python 沙箱。因此,python 方面的一切都很好。
问题是:在同一个apache+mod_wsgi实例下,不同的apache虚拟主机可以使用不同的python版本吗?
如果没有,继续进行的最佳选择是什么?
有一个相关的WsgiPythonHome设置,但它是在“服务器配置”上下文中全局定义的,而不是每个虚拟主机。另外,mod_wsgi 是为特定的 python 版本编译的,所以我不确定它是否可以处理这种情况。
我使用 Homebrew 来构建和安装 mod_wsgi:
$ brew install mod_wsgi
==> Downloading http://modwsgi.googlecode.com/files/mod_wsgi-3.2.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/mod_wsgi/3.2 --disable-debug --disabl
==> make install
==> Caveats
NOTE: "brew install -v mod_wsgi" will fail! You must install
in non-verbose mode for this to succeed. Patches to fix this
are welcome (and should be sent upstream too.)
* You must manually edit /etc/apache2/httpd.conf to load
/usr/local/Cellar/mod_wsgi/3.2/libexec/mod_wsgi.so
* On 10.5, you must run Apache in 32-bit mode:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX
==> Summary
/usr/local/Cellar/mod_wsgi/3.2: 3 files, 344K, built in …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Apache 和 mod_wsgi 的 django 网站。自过去 4-5 天以来,我的管理员电子邮件中不断出现此类错误。
OperationalError: (1040, 'Too many connections')
我已经安装了 django-debug-toolbar,每个页面平均有 100-150 个查询,响应时间不到 300 毫秒。我不认为我的 MySql 服务器超载。另一方面,这发生在我尝试重新配置我的httpd.conf设置后一段时间。
我的网站每天收到 800-1000 次点击。
这些是我的httpd.conf设置
KeepAlive Off
Listen 13646
MaxSpareThreads 15
MinSpareThreads 8
MaxClients 20
ServerLimit 20
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
MaxRequestsPerChild 1000000
StartServers 20
WSGIDaemonProcess <project> processes=7 threads=12 python-path=<path>
WSGIProcessGroup <project>
WSGIRestrictEmbedded off
WSGILazyInitialization On
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
附注。我正在使用共享托管计划。我的托管计划有 512MB RAM,其中 300MB 是免费的。
我对所有服务器技术都比较陌生,我按照本教程使用 mod_wsgi 在 Apache Web 服务器上部署我的 Django 应用程序:
http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
这是我的虚拟主机文件:
<VirtualHost *:80>
ServerName www.abcxyz.org
ServerAlias abcxyz.org
WSGIScriptAlias / /var/www/abcxyz/django/abcxyz/wsgi_prod.py
Alias /static/ /var/www/abcxyz/static/
<Location "/static/">
Options -Indexes
</Location>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
据我了解,wsgi_prod.py文件应该只在服务器启动时执行一次(或者当它收到第一个请求时 - 我对此不太确定)。但在我的应用程序中,当向服务器发送请求时,它会执行多次。
它不是对所有请求执行,而是对其中一些请求执行。还有一些请求,会触发执行wsgi_prod.py只是有时会触发执行。
这是我的 wsgi_prod.py 文件:
import os
import sys
import site
import thread
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/etc/Envs/abcxyz/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/abcxyz/django')
sys.path.append('/var/www/abcxyz/django/abcxyz')
os.environ['DJANGO_SETTINGS_MODULE'] = 'abcxyz.settings.production'
# Activate your virtual env
activate_env=os.path.expanduser("/etc/Envs/abcxyz/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env)) …Run Code Online (Sandbox Code Playgroud) 所以我在403: Forbidden我的 apache2.4 虚拟主机之一上遇到错误。
有趣的是,/var/log/apache2/error.log报告:
authz_core:error] [pid 4878:tid 140394394269440] [client 10.214.154.19:33009] AH01630: client denied by server configuration
而且....虽然我确实有其他使用 authz 的 Virtualhosts(主要用于 subversion 托管),但我禁用了所有这些,除了我遇到问题的那个,重新启动了 Apache,并且没有明显的区别。
这是我的 Apache2 站点可用文件,尽管我已禁用所有其他主机配置,并将我的配置降至最低,但仍然拒绝访问。
<VirtualHost *:443>
WSGIScriptAlias /example /data/example/example.wsgi
<Directory /data/example>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
Require all granted
</Directory>
LogLevel info
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.pem
SSLCertificateKeyFile /etc/ssl/private/example.key
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
此外,为了验证这不是我的 wsgi 脚本,我将脚本替换为:
def application(environ, start_response):
start_response('200 OK',[('Content-type','text/html')])
return ['<html><body>Hello World!</body></html>']
Run Code Online (Sandbox Code Playgroud)
而这并没有任何明显的区别。
有任何想法吗?
我正在尝试将 Apache 配置为托管 Django 网络服务和 PHP 网站。
具有该模式的所有 URLwww.mysite.com/api都应定向到 Django 服务。所有其他 URL(例如www.mysite.com)都应该指向 PHP 网站。
我的虚拟主机配置如下所示:
...
WSGIScriptAlias /api /var/www/mysite.com/apache/django.wsgi
<Directory /var/www/mysite.com/apache>
Order allow,deny
Allow from all
</Directory>
Alias / /var/www/mysite.com/apache/php/
<Directory /var/www/mysite.com/apache/php>
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
...
Run Code Online (Sandbox Code Playgroud)
这会将每个请求定向到正确的处理程序。但是,我的 Django urls.py看起来像这样:
...
api_patterns = patterns('',
url(r'^api/1.0/$', views.api_root),
url(r'^api/1.0/oauth/',
include('oauth2_provider.urls',
namespace='oauth2_provider')),
...
Run Code Online (Sandbox Code Playgroud)
这无法匹配任何 URL,因为它期望它们以api. 当然,这个前缀在它到达这里之前被剥离(当请求到达这里时,它们是格式1.0/oauth等)。
有没有办法将完整的 URL 传递给 Django?
mod_wsgi 的作者 Graham Dumpleton 提出了几种解决此问题的方法,这两种方法都有效(请参阅下面已接受的答案)。
我选择了他在 …
mod-wsgi ×10
apache-2.2 ×7
django ×5
python ×4
apache-2.4 ×2
wsgi ×2
linux ×1
logging ×1
mac-osx ×1
mysql ×1
redhat ×1
rewrite ×1
virtualhost ×1