我已经有一个 Django Web 应用程序正在运行,我想让它通过 Internet 可见。我什至会考虑单独使用 Django,因为它只是我想向某人展示的原型,而且安全性现在不是我的首要任务。但是由于这看起来不可能实现(或者至少我无法做到),所以我正在转向 Apache。
问题是有很多教程,每个教程都做不同的事情。
到目前为止,我已经: - 安装了 apache(并且可以工作) - 安装了 mod_wsgi(并且模块已加载到 Apache 中) - 因为我的 Django Web 应用程序位于 /home/myuser/www/myapp/ 中(或者至少那是我存储的地方manage.py 文件,应用程序的其余部分在 /home/myuser/www/myapp/myapp/ 中),我试图让 apache 指向那里。所以我创建了 (home/myuser/www/myapp/apache.conf/web.wsgi ,内容如下:
import os, sys
sys.path.append('/home/myuser/www/myapp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
顺便说一句,我也有 wsgi Django 在 /home/myuser/www/myapp/myapp/wsgi.py 中创建新项目时自动生成。也许我应该使用这个,但到目前为止我找到的教程都没有提到这个文件。无论如何,它的内容是:
import os
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Apply WSGI middleware here.
# from myapp.wsgi import MyAppApplication
# application = MyAppApplication(application)
最后,我使用默认文件作为模板创建了文件 /etc/apache2/sites-available/myapp,如下所示:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/myuser/www/myapp/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py
PythonDebug On
</Directory>
<Directory /home/myuser/www/myapp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/myuser/www/myapp/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /home/myuser/www/myapp/access.log combined
</VirtualHost>
尽管描述了我的努力,但当我导航到 localhost 时,我只看到默认的 apache 网络,说“它有效!”。
请问有什么帮助吗?我错过了什么?