简体   繁体   English

远程服务器上的Django教程:如何在浏览器中查看?

[英]Django tutorial on remote server: how to view in my browser?

I'm getting started with a Django tutorial, and I've run into a snag. 我开始使用Django教程了,我遇到了麻烦。 Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. 在我的常用域上创建了示例“mysite”后,我希望能够在浏览器中显示它。 The tutorial points me to http://127.0.0.1:8000 . 教程指向http://127.0.0.1:8000 However, that's not going to work, as I'm doing this remotely. 但是,这不会起作用,因为我正在远程执行此操作。

[background information] [背景资料]

What I have done, apparently successfully, is django-admin.py startproject mysite (created mysite directory containing four files) python manage.py runserver (Validating models... 0 errors found, etc.) 我所做的,显然是成功的,是django-admin.py startproject mysite(创建包含四个文件的mysite目录)python manage.py runserver(验证模型...找到0错误等)

The absolute path is /home/toewsweb/public_html/pythonlab/mysite What URL should I be able to use to bring this up in my browser? 绝对路径是/ home / toewsweb / public_html / pythonlab / mysite我应该使用哪个URL在浏览器中显示它?

I also put mysite at /home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory) What URL should I be able to use in this case? 我还把mysite放在/ home / toewsweb / mysite上(因为它不应该放在可公开访问的目录中)在这种情况下我应该使用什么URL?

This is a virtual private server, so I have access to httpd.conf. 这是一个虚拟专用服务器,因此我可以访问httpd.conf。 I have downloaded and installed mod_wsgi and have added it to the Apache configuration. 我已经下载并安装了mod_wsgi并将其添加到Apache配置中。 I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; 我实际上设置了一个子文件,其DocumentRoot为/ home / toewsweb / public_html / pythonlab / mysite; however, when I point the browser to that subdomain, I just get the directory listing. 但是,当我将浏览器指向该子域时,我只是获取目录列表。

[/background information] [/背景资料]

Right now, I just want to know how to view what I'm working on in my browser. 现在,我只想知道如何查看我在浏览器中正在处理的内容。

Thanks! 谢谢!

For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). 出于开发目的,没有必要搞乱配置WSGI(虽然知道它是有用的,因为你需要为生产做这件事)。 Just start the dev server so that it listens to an external port: 只需启动开发服务器,以便它侦听外部端口:

./manage.py runserver 0:8000

This binds to the external IP address, so now you can access your Django site via port 8000 on that server: 这绑定到外部IP地址,所以现在您可以通过该服务器上的端口8000访问您的Django站点:

http://whatever.my.ip.is:8000

You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. 您需要在VirtualHost设置apache WSGIScriptAlias指令才能正确加载python和您的站点。 Django's docs have a great explanation on what you need to do. Django的文档对你需要做的事情有很好的解释。

Basic configuration 基本配置

Once you've got mod_wsgi installed and activated, edit your httpd.conf file and add: 一旦安装并激活了mod_wsgi,编辑httpd.conf文件并添加:

WSGIScriptAlias / /path/to/mysite/apache/django.wsgi

The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. 上面的第一位是您希望在(/表示根URL)上为您的应用程序提供服务的URL,第二位是您的系统上的“WSGI文件”的位置 - 见下文 - 通常在您的项目中。 This tells Apache to serve any request below the given URL using the WSGI application defined by that file. 这告诉Apache使用该文件定义的WSGI应用程序提供给定URL下的任何请求。

Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add: 接下来我们需要实际创建这个WSGI应用程序,所以创建WSGIScriptAlias第二部分中提到的文件并添加:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

If your project is not on your PYTHONPATH by default you can add: 如果您的项目默认情况下不在您的PYTHONPATH上,您可以添加:

path = '/path/to/mysite'
if path not in sys.path:
    sys.path.append(path)

just below the import sys line to place your project on the path. 在导入系统行的下方,将项目放在路径上。 Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location. 记得用正确的设置文件替换'mysite.settings',用你自己项目的位置替换'/ path / to / mysite'。

OR 要么

The other option is to run the dev server so it's accesible externally like so: 另一种选择是运行开发服务器,以便它可以在外部访问,如下所示:

python manage.py runserver 0.0.0.0:80

though please DO NOT use this in production. 但请不要在生产中使用它。 The dev server is single-threaded, and has not been auditing for security. 开发服务器是单线程的,并未进行安全审核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM