Python 80端口上的Django项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15689641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django project on 80 port?
提问by Dimitris
Ι have developed a Django project and uploaded it to a cloud VΜ. Currently i have access to it through 8080 port.
我已经开发了一个 Django 项目并将其上传到云 VM。目前我可以通过 8080 端口访问它。
python manage.py runserver 0.0.0.0:8080
If i enter the url without the 8080 port, it shows the "it works" page. How can i set my Django project to run by default on 80 port?
如果我输入没有 8080 端口的 url,它会显示“它有效”页面。如何将我的 Django 项目设置为默认在 80 端口上运行?
I am using Ubuntu 12.04 server
我正在使用 Ubuntu 12.04 服务器
回答by Silas Ray
回答by boatcoder
Here is the kind of file you will need to put in /etc/apache2/sites-enabled, you need to adjust the paths in it. You also need to load mod_wsgi which you can do via apt-get.
这是您需要放入 /etc/apache2/sites-enabled 中的文件类型,您需要调整其中的路径。您还需要加载 mod_wsgi,您可以通过 apt-get 来完成。
<VirtualHost 192.168.1.14:80>
ServerAdmin [email protected]
ServerName www.whatever.com
ServerAlias whatever.com
Alias /robots.txt /home/dimitris/Python/mysite/site_media/robots.txt
Alias /favicon.ico /home/dimitris/Python/mysite/site_media/favicon.png
Alias /static/ /home/dimitris/Python/mysite/site_media/static
WSGIDaemonProcess mysite user=dimitris processes=1 threads=5
WSGIProcessGroup mysite
WSGIScriptAlias / /home/dimitris/Python/mysite/deploy/mysqite_wsgi.py
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/mysite.error.log
CustomLog ${APACHE_LOG_DIR}/mysite.access.log combined
ServerSignature Off
</VirtualHost>
Most of this assumes you are running in a virtualenv, that means you will need a wsgi file to get things running. The above file sets up apache to run your "wsgi" file which looks something like this:
其中大部分假设您在 virtualenv 中运行,这意味着您需要一个 wsgi 文件才能运行。上面的文件设置 apache 来运行你的“wsgi”文件,它看起来像这样:
import os
from os.path import abspath, dirname, join
import sys
with open("/tmp/mysite.sys.path", "w") as f:
for i in sys.path:
f.write(i+"\n")
#redirect sys.stdout to sys.stderr for libraries that use
#print statements for optional import exceptions.
sys.stdout = sys.stderr
sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
sys.path.insert(0, abspath(join(dirname(__file__), "../../lib/python2.7/site-packages/")))
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
mod_wsgi, then opens this one file and executes it. application is the part that waits for requests from the webserver, and the rest behaves just like runserver except it can be multi-process and multi-threaded.
mod_wsgi,然后打开这个文件并执行它。application 是等待来自 webserver 的请求的部分,其余的就像 runserver 一样,只是它可以是多进程和多线程的。

