使用 apache 和 mod_wsgi 部署 Django 站点
我一直在尝试自学如何创建和部署 Django 应用程序。我创建了一个测试项目,我可以使用 Django 测试服务器浏览它。现在我想使用 apache 和 mod_wsgi 来部署它。
我按照快速安装指南中的安装说明进行操作,并安装了 mod_wsgi。然后,我浏览了快速配置指南中的示例,并能够成功连接到我的浏览器中的示例输出。
我现在已经转到与 Django 集成部分,但我不能取得任何进展。每当我尝试浏览为项目设置的 URL 时,我只会看到默认的 apache 索引页面。我查看了 apache 错误日志,其中没有任何消息(当我调试第一个示例时,我收到了一些消息,所以我知道我正在查看正确的日志)。我尝试了 django 项目文件夹中的初始示例 wsgi 应用程序,但我得到了相同的默认索引页面。我什至设置了“LogLevel info”来尝试获取更多详细信息,但没有。
有人有什么建议吗?
这是我的 apach2.conf:
LockFile ${APACHE_LOCK_DIR}/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
Include mods-enabled/*.load
Include mods-enabled/*.conf
Include httpd.conf
Include ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
Include conf.d/
Include sites-enabled/
这是我的 httpd.com:
ServerName HomeServer
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
这是 VitualHost:
<VirtualHost *:80>
ServerName ppbase.homeserver
ServerAdmin [email protected]
DocumentRoot /var/projects/ppbase/ppbase
<Directory /var/projects/ppbase/ppbase>
Order allow,deny
Allow from all
</Directory>
LogLevel info
WSGIScriptAlias / /var/projects/ppbase/django.wsgi
<Directory /var/projects/ppbase>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
这是 django.wsgi:
import os
import sys
projectpath = '/var/projects/ppbase'
projectapppath = '/var/projects/ppbase/ppbase'
if projectpath not in sys.path:
sys.path.append(projectpath)
if projectapppath not in sys.path:
sys.path.append(projectapppath)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ppbase.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've been trying to teach myself to create and deploy Django apps. I've created a test project and I can browse it using the Django test server. Now I want to deploy it using apache and mod_wsgi.
I followed the installation instructions at the Quick Installation Guide and got mod_wsgi installed. I then went through the example in the Quick Configuration Guide and was able to successfully connect to the example output in my browser.
I've now moved on to the Integration With Django section and I can't make any progress with it. Any time I try to browse to the URL I've set up for my project I just see the default apache index page. I've looked at the apache error log and there are no messages in it (I got a few when I was debugging the first example so I know I'm looking at the correct log). I tried the initial example wsgi application in the django project folder, but I got the same default index page. I even set 'LogLevel info' to try and get more detail, but there was none.
Does anyone have any suggestions?
Here is my apach2.conf:
LockFile ${APACHE_LOCK_DIR}/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
Include mods-enabled/*.load
Include mods-enabled/*.conf
Include httpd.conf
Include ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
Include conf.d/
Include sites-enabled/
Here is my httpd.com:
ServerName HomeServer
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
Here is the VitualHost:
<VirtualHost *:80>
ServerName ppbase.homeserver
ServerAdmin [email protected]
DocumentRoot /var/projects/ppbase/ppbase
<Directory /var/projects/ppbase/ppbase>
Order allow,deny
Allow from all
</Directory>
LogLevel info
WSGIScriptAlias / /var/projects/ppbase/django.wsgi
<Directory /var/projects/ppbase>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here is the django.wsgi:
import os
import sys
projectpath = '/var/projects/ppbase'
projectapppath = '/var/projects/ppbase/ppbase'
if projectpath not in sys.path:
sys.path.append(projectpath)
if projectapppath not in sys.path:
sys.path.append(projectapppath)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ppbase.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您得到“它有效!”的提示。页面上您在日志中看不到任何“错误”。
假设您正在使用虚拟主机,这仅意味着默认的 apache 配置比您的 django 虚拟主机“更匹配”。
您可以在虚拟主机配置中为您的应用程序定义更好的规则,或者简单粗暴地禁用默认的 apache 站点(假设您使用的是 *NIX)
If you get the "It works!" page there is nothing you can see "wrong" in the logs.
Assuming that you're working with virtual hosts, It just means that the default apache config "is a better match" than your django virtual host.
You can define a better rule for your app in the virtual host configuration or, rough and easy, disable the default apache site with (assuming you're on *NIX)
忘了它。我真是个白痴。我设置了我的虚拟主机文件,但我没有在 apache 中启用它。
Forget it. I was being a total idiot. I'd set up my vhost file, but I hadn't enabled it in apache.