美文网首页我爱编程
Ubuntu下使用Apache和mod_wsgi配置Django

Ubuntu下使用Apache和mod_wsgi配置Django

作者: Yvonne_ft_more | 来源:发表于2017-01-10 17:00 被阅读123次
  • 安装
    apt-get install apache2
    apt-get install libapache2-mod-wsgi

  • apache目录
    --> apache.conf
    --> sites-available
    --> sites-enable
    ...

  • 创建属于自己的配置(虚拟主机)
    在sites-available中创建myapp.conf,输入以下:

<VirtualHost *:80>
  ServerAdmin xxxx@xx.com  # 管理人员的邮箱
  ServerName www.xxx.com   # 或者一个ip地址
  # 静态文件存放的路径
  Alias /static /var/www/myapp/static
  <Directory /var/www/myapp/static>
    Require all granted
  </Directory>
  # 项目中wsgi.py的路径
  <Directory /var/www/myapp/myapp>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
  # 所使用的python的路径,我是用的virtualenv
  WSGIDaemonProcess app python-path=/var/www python-home=/var/www/env
  WSGIProcessGroup app
  WSGIScriptAlias / /var/www/myapp/myapp/wsgi.py

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • 激活myapp.conf
    a2ensite mytest.conf

  • 加载配置文件并启动apache
    /etc/init.d/apache2 reload
    /etc/init.d/apache2 restart

  • 需要修改Django项目中的wsgi.py文件
    在wsgi.py中添加以下代码:

root = os.path.join(os.path.dirname(__file__), '..')    # add parent path
sys.path.insert(0, root)    # add to sys 
pathos.environ.setdefault("DJANGO_SETTINGS_MODULE", "Rainbow_B.settings")
  • 如何加载最新代码
    需要加载最新的代码,使用touch wsgi.py即可使代码更新为最新修改。这样不用频繁重启apache。
矢志不渝的愚者

相关文章

网友评论

    本文标题:Ubuntu下使用Apache和mod_wsgi配置Django

    本文链接:https://www.haomeiwen.com/subject/wfakbttx.html