Django + Apache + wsgi配置和环境搭建(ubuntu)

来源:互联网 发布:修改照片拍摄时间软件 编辑:程序博客网 时间:2024/05/23 16:21

上一篇写了Django + nginx + uwsgi配置和环境搭建(ubuntu)
因为公司服务器环境问题,又配置了apache的环境,记录如下:

一. 安装环境:

#apachesudo apt-get install apache2# Python 2sudo apt-get install libapache2-mod-wsgi

二. django:
2.1 保证网站能运行:
根目录执行:python manage.py runserver 0.0.0.0:1111
能在0.0.0.0:1111访问到,说明正常

2.2 static和media文件
setting.py中增加:STATIC_ROOT = os.path.join(BASE_DIR, "static/")
执行python manage.py collectstatic

三. apache:
/etc/apache2/sites-available/aaaa.conf中配置

<VirtualHost *:2222>        # The ServerName directive sets the request scheme, hostname and port that        # the server uses to identify itself. This is used when creating        # redirection URLs. In the context of virtual hosts, the ServerName        # specifies what hostname must appear in the request's Host: header to        # match this virtual host. For the default virtual host (this file) this        # value is not decisive as it is used as a last resort host regardless.        # However, you must set it for any further virtual host explicitly.        #ServerName www.example.com        ServerName localhost        ServerAlias domain.com        ServerAdmin webmaster@localhost        DocumentRoot /home/moma/Documents/domain_seo_tool        Alias /media/ /home/moma/Documents/domain_seo_tool/media/        Alias /static/ /home/moma/Documents/domain_seo_tool/static/        WSGIScriptAlias / /home/moma/Documents/domain_seo_tool/domain_seo_tool/wsgi.py        <Directory /home/moma/Documents/domain_seo_tool/media/>                Require all granted        </Directory>        <Directory /home/moma/Documents/domain_seo_tool/static/>                Require all granted        </Directory>        WSGIDaemonProcess domain_seo_tool user=moma group=moma processes=2 threads=25 python-path=/usr/local/lib/python2.7/site-packages        WSGIProcessGroup domain_seo_tool        #某些版本下不需要一下5条,但某些会apache出现forbidden现象,加上保证运行,apache error log中出现client denied by server configuration        <Directory /home/moma/Documents/domain_seo_tool/domain_seo_tool>               <Files wsgi.py>                       Require all granted               </Files>       </Directory>        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,        # error, crit, alert, emerg.        # It is also possible to configure the loglevel for particular        # modules, e.g.        #LogLevel info ssl:warn        ErrorLog ${APACHE_LOG_DIR}/error.log        CustomLog ${APACHE_LOG_DIR}/access.log combined        # For most configuration files from conf-available/, which are        # enabled or disabled at a global level, it is possible to        # include a line for only one particular virtual host. For example the        # following line enables the CGI configuration for this host only        # after it has been globally disabled with "a2disconf".        #Include conf-available/serve-cgi-bin.conf</VirtualHost>

注意:

上述配置中标记的地方,但某些会apache出现forbidden现象,加上保证运行,apache error log中出现client denied by server configuration,百度上的一些答案都不靠谱,解决问题还是得靠stackover flow

django:
wsgi.py 文件
修改成:

"""WSGI config for domain_seo_tool project.It exposes the WSGI callable as a module-level variable named ``application``.For more information on this file, seehttps://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/"""import os, syssys.path.append('/var/www/domain_seo_tool_apache/domain_seo_tool')#增加的sys.path.append('/var/www/domain_seo_tool_apache/domain_seo_tool /domain_seo_tool')#增加的from django.core.wsgi import get_wsgi_applicationos.environ.setdefault("DJANGO_SETTINGS_MODULE", "domain_seo_tool.settings")application = get_wsgi_application()

需要增加系统路径,否则报错:

Target WSGI script
‘/var/www/domain_seo_tool_apache/domain_seo_tool/wsgi.py’ cannot be
loaded as Python module
以及
ImportError: Could not import settings ‘domain_seo_tool.settings’ (Is it on sys.path? Is there an import error in the settings file?): No module named domain_seo_tool.settings

0 0