django deploy - ubuntu 14.04和apache2

时间:2016-08-03 16:32:52

标签: python django apache ubuntu mod-wsgi

https://www.sitepoint.com/deploying-a-django-app-with-mod_wsgi-on-ubuntu-14-04/

https://www.youtube.com/watch?v=hBMVVruB9Vs

这是我第一次部署网站。这些是我遵循的教程。

现在我可以从其他机器访问服务器(通过键入10.231.XX.XX)并查看Apache2 Ubuntu默认页面。

然后我尝试访问我的django项目。我跑:

  

python manage.py runserver 8000   验证模型......

     

0错误发现2016年8月3日 - 09:44:20 Django版本1.6.1,使用   设置'设置'启动开发服务器   http://127.0.0.1:8000/使用CONTROL-C退出服务器。

然后我键入10.231.XX.XX:8000尝试访问django页面。但我失败了。 它说:

  

无法访问此网站

     

10.231.XX.XX拒绝连接。在Google上搜索231 8000 ERR_CONNECTION_REFUSED

我已经尝试了所有我能做到的事情,但仍然无法理解为什么。 (如网站https://www.sitepoint.com/deploying-a-django-app-with-mod_wsgi-on-ubuntu-14-04/) 我在mysite文件夹中有apache文件夹,在override.py:

from mysite.settings import *

    DEBUG = True
    ALLOWED_HOSTS = ['10.231.XX.XX']

在wsgi.py中:

import os, sys
# Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)

# Add the path to 3rd party django application and to django itself.
sys.path.append('/home/zhaojf1')
os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

和__init__py为空。

/etc/apache2/sites-enabled/000-default.conf中的

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

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

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
WSGIScriptAlias /msa.html /home/zhaojf1/Web-Interaction/apache/wsgi.py

<Directory "/home/zhaojf1/Web-Interaction-APP">
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

在我做完所有事情后,我也重启了apache。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

连接拒绝错误可能会归结为Apache未正确配置VirtualHost或您访问错误的端口。您的wsgi.py文件中也存在其他基本错误。

wsgi.py文件开始,DJANGO_SETTINGS_MODULE值错误:

os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override'

该值意味着是Python模块路径。拥有IP地址看起来非常错误,不太可能产生你需要的东西。

接下来是sys.path的更改。您可以通过Apache配置文件中的mod_wsgi选项更好地完成项目的位置和激活任何Python虚拟环境。

您在路径中添加主目录也是您可能遇到的潜在其他问题的标志。具体而言,Apache经常运行的用户无法读入主目录,因为其他人无法读取/访问主目录。您可能需要将项目移出主目录。

对于Apache配置,您的VirtualHost缺少ServerName指令。如果这是您添加的另一个VirtualHost而不是默认值(解析时出现在Apache配置中的第一个),它将被忽略,所有请求都将转到第一个VirtualHost。你确实在默认的站点文件中显示它,所以可能你没事。

即便如此,VirtualHost设置为在端口80上列出。您正在尝试连接到端口8000,因此不会有任何监听。

下一期是WSGIScriptAlias行。

WSGIScriptAlias /msa.html /home/zhaojf1/Web-Interaction/apache/wsgi.py

msg.html作为挂载点很奇怪,因为它使得它看起来好像是在访问单个HTML页面,但是它将它映射到整个Django项目。如果您正在访问主机的根目录,它也不会映射到Django应用程序,因为它已将其挂载到子URL。因此可能需要使用:

WSGIScriptAlias / /home/zhaojf1/Web-Interaction/apache/wsgi.py

下一个问题是Directory指令中指定的目录与您在wsgi.py中存在WSGIScriptAlias文件的位置不匹配。他们应该匹配。所以也许你的意思是:

<Directory /home/zhaojf1/Web-Interaction/apache>

即便如此,apache目录的来源也不合适。路径中的最后一个目录通常应该是Django项目的名称。

最后,您可能还需要更改ALLOWED_HOSTS。如果您发现开始收到错误的请求错误,则可能无法正确匹配。将其更改为['*']以查看是否有帮助。

很多小事都错了。

建议是: