django 部署 - ubuntu 14.04 和 apache2

django deploy - ubuntu 14.04 and apache2

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

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

这是我第一次部署 website.And 这些是我遵循的教程。

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

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

python manage.py runserver 8000 Validating models...

0 errors found August 03, 2016 - 09:44:20 Django version 1.6.1, using settings 'settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

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

This site can’t be reached

10.231.XX.XX refused to connect. Search Google for 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。

感谢帮助

连接被拒绝错误可能归结为 Apache 为 VirtualHost 配置不正确或您访问了错误的端口。您的 wsgi.py 文件中还有其他基本错误。

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

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

该值是一个 Python 模块路径。把 IP 地址放在那里看起来很不对劲,不太可能产生你需要的东西。

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

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

关于 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。如果你发现你开始收到错误的请求错误,它可能没有正确匹配。将其更改为 ['*'] 以查看是否有帮助。

很多小事都错了。

建议是: