我是Django开发的新手。我创建了一个Django应用程序并在开发服务器中测试,即127.0.0.1:8080/mysite。然后我决定在Apache服务器2.4.9上运行这个应用程序。 众所周知,最好的选择是配置mod_wsgi。 我的问题是Apache服务器从未在配置为以下后运行:
我正在使用32位Python,Apache和mod_wsgi。为所有用户安装了Python。请帮帮我 -
答案 0 :(得分:0)
首先复制modules文件夹中的mod_wgi, 然后在httpd.conf模块列表中添加以下内容:
LoadModule wsgi_module modules/mod_wsgi.so
注意命名很重要(应该以_module为后缀)
将以下内容添加到httpd.conf
Include path_to_your_proj/django_wsgi.conf
django_wsgi.conf文件:
WSGIScriptAlias path_to/django.wsgi
<Directory project_path>
Allow from all
Order allow,deny
</Directory>
Alias /static path_to_static_files
django.wsgi文件:
import os
import sys
#Calculate the path based on the location of the WSGI script.
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PROJECT_ROOT = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))
SETTINGS_DIR = os.path.join(PROJECT_ROOT,'proj_dir_name')
if PROJECT_ROOT not in sys.path:
sys.path.append(PROJECT_ROOT)
if SETTINGS_DIR not in sys.path:
sys.path.append(SETTINGS_DIR)
os.chdir(SETTINGS_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'proj_name.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
如果你正在使用virtualenv(我建议),请执行以下操作:
httpd.conf文件:
# virtual env02
<VirtualHost ip-adddress_or_dns:port_if_other_than_80>
ServerName just_a_name # like : app01
ServerAlias just_an_alias # like : app01.mydomain.com
ErrorLog "logs/env02.error.log"
CustomLog "logs/env02.access.log" combined
WSGIScriptAlias / direct_path_to_you_wsgi_with_forward_slashes # like: C:/virtual/venv01/proj_name/wsgi.py
<Directory proj_dir_with_forward_slashed>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static path_to_static_folder_with_forward_slashes
<Directory path_to_static_folder_with_forward_slashes>
Require all granted
</Directory>
</VirtualHost>
# end virtual env02
wsgi.py文件:
activate_this = 'c:/virtual/env02/scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('c:/Organizer/virtual/env02/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('c:/virtual/proj_name')
sys.path.append('c:/virtual/proj_name/default_app_name')
os.environ['DJANGO_SETTINGS_MODULE'] = 'default_app_name.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "default_app_name.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()