使用Apache和mod_wsgi向Django部署设置寻求建议

时间:2014-09-11 19:30:36

标签: python django apache mod-wsgi

我已经使用Apache和mod_wsgi在官方文档和其他帖子之后部署了Django。当我的网站工作时,我担心我的设置可能有问题。我想对我的设置有一些建议,是否遵循最佳实践。如果您发现此设置存在问题,请与我们联系。谢谢,李

wsgi.py

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

settings.py

  ...
    ALLOWED_HOSTS = ['DjangoProject.example.com']
    STATIC_ROOT = "/var/www/DjangoProject/static/"
    STATIC_URL = '/static/'
    ....

/etc/apache2/apache2.conf - 其他设置在此行

之上
...
WSGIPythonPath /var/www/DjangoProject/DjangoProject:/var/www/DjangoProject/env/lib/python2.6/site-packages

/etc/apache2/httpd.conf - 没有其他设置,但已部署此行

WSGIPythonPath /var/www/DjangoProject:/var/www/DjangoProject/DjangoProject:/var/www/DjangoProject/env/lib/python2.6/site-packages

的/ etc / apache2的/位点可用/默认

NameVirtualHost *:8080
<VirtualHost *:8080>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                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 ${APACHE_LOG_DIR}/error.log

        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
##############################
## DjangoProject WSGI         ##
##############################

ServerName DjangoProject.example.com
Alias /favicon.ico /var/www/DjangoProject/DjangoProject/static/favicon.ico
AliasMatch ^/([^/]*\.css) /var/www/DjangoProject/MyApp/static/MyApp/css/$1

Alias /media/  /var/www/DjangoProject/DjangoProject/media/
Alias /static/ /var/www/DjangoProject/MyApp/static/

<Directory /var/www/DjangoProject/MyApp/static>
Order deny,allow
Allow from all
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 10080 minutes"
  ExpiresByType image/jpeg "access plus 10080 minutes"
  ExpiresByType image/png "access plus 10080 minutes"
  ExpiresByType text/css "access plus 60 minutes"
  ExpiresByType text/javascript "access plus 60 minutes"
  ExpiresByType application/x-javascript "access plus 60 minutes"
  ExpiresByType text/xml "access plus 60 minutes"
ExpiresByType text/xml "access plus 60 minutes"
</IfModule>
</Directory>


<Directory /var/www/DjangoProject/DjangoProject/media>
Order deny,allow
Allow from all
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 10080 minutes"
  ExpiresByType image/jpeg "access plus 10080 minutes"
  ExpiresByType image/png "access plus 10080 minutes"
  ExpiresByType text/css "access plus 60 minutes"
  ExpiresByType text/javascript "access plus 60 minutes"
  ExpiresByType application/x-javascript "access plus 60 minutes"
  ExpiresByType text/xml "access plus 60 minutes"
</IfModule>
</Directory>


WSGIDaemonProcess DjangoProject.example.com processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup DjangoProject.example.com


WSGIScriptAlias /MyApp /var/www/DjangoProject/DjangoProject/wsgi.py
WSGIScriptAlias / /var/www/DjangoProject/DjangoProject/wsgi.py


<Directory /var/www/DjangoProject/DjangoProject>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
<IfModule mod_expires.c>
  ExpiresActive On
ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 10080 minutes"
  ExpiresByType image/jpeg "access plus 10080 minutes"
  ExpiresByType image/png "access plus 10080 minutes"
  ExpiresByType text/css "access plus 60 minutes"
  ExpiresByType text/javascript "access plus 60 minutes"
  ExpiresByType application/x-javascript "access plus 60 minutes"
  ExpiresByType text/xml "access plus 60 minutes"
</IfModule>
</Directory>


AddType audio/mpeg .mp1 .mp2 .mp3 .mpg .mpeg
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

环境:

DocumentRoot /var/www

因为你有一个危险的开始。

您永远不应将DocumentRoot目录设置为Django项目存储位置的父目录。如果你填充配置的其他部分,可能导致你的Django设置文件可以下载,包括任何数据库密码。