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

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

一. 安装环境:

#apache
sudo apt-get install apache2

# Python 2
sudo 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, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os, sys
sys.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_application

os.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

原文地址:https://www.cnblogs.com/llguanli/p/7249888.html