使用 mod_wsgi 在 Apache2 上部署 Django - Django 项目的正确位置? [英] Deploying Django on Apache2 with mod_wsgi - Correct location for Django project?

查看:22
本文介绍了使用 mod_wsgi 在 Apache2 上部署 Django - Django 项目的正确位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网络服务器上部署我的第一个 Django 项目.我对服务器配置和 Django 都是新手,所以我很难找到我的错误.

I am trying to deploy my first Django project on my webserver. I am both new to server configuration and Django so I have a hard time finding my errors.

在我在网上看到的大多数教程中,生产服务器上的 Django 项目是在/var/www/myproject 中创建的 - 但是,出于安全原因,Django 文档建议将代码放在/var/www/之外(请参阅:https://docs.djangoproject.com/en/1.9/intro/tutorial01/).

In most tutorials I have seen online, the Django project on the production server is created in /var/www/myproject - however, the Django documentation recommends putting the code outside of /var/www/ for security reasons (see: https://docs.djangoproject.com/en/1.9/intro/tutorial01/).

因此,我将我的 Django 项目放在我的主文件夹的子目录中,Apache 无法访问该子目录.我没有向/var/www 添加文件,因为没有教程提到这是必要的.

So, I put my Django project in a subdirectory of my home folder, which is not accessible to Apache. I added no file to /var/www as no tutorial mentions that this would be necessary.

根据这些教程使用 mod_wsgi 设置 Django 后:
(https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/ ,
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 ,
http://tutorial.djangogirls.org/en/deploy/)
当我通过浏览器访问我的网络服务器时,我看到的只是默认的 index.html 站点.从我的网络服务器运行和访问 Django 的开发服务器也不起作用.在这两种情况下,当我尝试访问管理页面时,都会收到标准的 404 错误.

After setting up Django with mod_wsgi according to these tutorials:
(https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/ ,
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 ,
http://tutorial.djangogirls.org/en/deploy/)
when I access my webserver via browser all I see is the default index.html site. Running and accessing the development server of Django from my webserver does not work either. In both cases, when I try to access the admin page I get a standard 404 Error.

毕竟我应该把一些文件放到/var/www/中吗?

Am I supposed to put some files into /var/www/ after all?

我使用 Let'sEncrypt 证书并且在使用 auto-certbot 的设置过程中选择自动将 http 重定向到 https 会不会是一个问题?

Could it be an issue that I use a Let'sEncrypt certificate and during the setup with auto-certbot chose to automatically redirect http to https?

我的一些配置文件(我省略了我没有更改/与 Django 无关的部分):

Some of my configured files (I left out parts that I didn't change/have nothing to do with Django):

/etc/apache2/apache2.conf

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

/etc/apache2/sites-available/000-default.conf

    DocumentRoot /var/www/html

    Alias /static /home/me/django/mysite/app/static
    <Directory /home/me/django/mysite/app/static>
            Require all granted
    </Directory>

    <Directory /home/me/django/mysite/mysite>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

    WSGIDaemonProcess mysite python-path=/home/me/django/mysite:/home/me/django/djangoenv/lib/python2.7/site-packages
    WSGIProcessGroup mysite
    WSGIScriptAlias / /home/me/django/mysite/mysite/wsgi.py

/home/me/django/mysite/mysite/wsgi.py

import os
import sys

path = '/home/sirhys/django/mysite'  
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())

/home/me/django/mysite/mysite/settings.py

DEBUG = False

ALLOWED_HOSTS = [
        'www.mysite.com',
        'mysite.com'
]

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(SITE_ROOT, '..', 'app/static')
STATIC_URL = '/static/'

谢谢!


最后,我发现问题出在服务器上不同的现成配置文件之间存在冲突.显然,Let's Encrypt SSL 配置文件覆盖了/etc/apache2/sites-available 中的所有其他文件.当我将 WSGI-config 行放在那里时,它就像一个魅力.
我现在在我的主目录中有我的代码,它正在工作.我决定通过 collectstatic 将我的静态文件放入/var/www/static 中.这对我有用,但似乎代码和静态文件的确切位置真的无关紧要.


In the end, I found the issue to be a conflict between the different out-of-the-box config files I have on my server. Apparently, the Let's Encrypt SSL config file was overriding all other files in /etc/apache2/sites-available. When I put the WSGI-config lines there, it worked like a charm.
I have my code in my home directory now, and it is working. I decided to put my static files into /var/www/static via collectstatic. This is working for me but it seems the exact location of code and static files is really irrelevant.

推荐答案

你的配置只针对一个网站,需要使用虚拟主机

Your config only for one website, need use virtual host

示例部署:

对于 python3 安装 mod_wsgi(基于 debian 的系统)

For python3 install mod_wsgi (debian based system)

apt-get install libapache2-mod-wsgi-py3

把你的项目放在标准的apache位置

Put your project in standard apache location

/var/www/myproj 

其中 myproj = 为 django 项目命名

where myproj = name you django project

检查项目

python3 manage.py check

或/并运行它

python3 manage.py runserver 0.0.0.0:8000

如果您的域是 example.com,则 http://example.com:8000/ 或使用服务器知识产权.

if your domain example.com, then http://example.com:8000/ or use server IP.

允许apache2读写内容

Allow apache2 read and write content

chown www-data:www-data /var/www/myproj -R

配置 apache 2.4

Config apache 2.4

cd /etc/apache2/sites-available
editor myproj.conf

和 Apache2.4 最小配置(许多站点)

and Apache2.4 minimal config (many sites)

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

DocumentRoot /var/www/myproj

WSGIDaemonProcess example.com python-path=/var/www/myproj

WSGIScriptAlias / /var/www/myproj/myproj/wsgi.py \
    process-group=example.com application-group=%{GLOBAL}

<Directory /var/www/myproj/myproj>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

# static
Alias /media/ /var/www/myproj/media/
Alias /static/ /var/www/myproj/static/
Alias /robots.txt /var/www/myproj/robots.txt
# logs
ErrorLog /var/log/apache2/myproj-error.log
LogLevel warn
CustomLog /var/log/apache2/myproj-access.log combined

</VirtualHost>

Virtualenv:python路径+虚拟,例子

Virtualenv: python path + virtual, example

python-path=/path/www/myproj python-home=/path/to/venv

独占使用守护进程模式,添加到/etc/apache2/mods-available/wsgi.load

Using daemon mode exclusively, add to /etc/apache2/mods-available/wsgi.load

WSGIRestrictEmbedded On

启用配置

a2ensite myproj.conf

检查语法

apachectl -t

重启apache或重新加载

Restart apache or reload

service apache2 restart

<小时>

使用定义变量的示例配置 apache2.4.Django > 1.6.仅设置域、路径、名称项目.在目录/var/www/example.com/myproject/myproect/wsgi.py 的示例路径中,静态 url/media/和/static/


Example config apache2.4 with define variable. Django > 1.6. Set only domain, path, name project. In example path to dir /var/www/example.com/myproject/myproect/wsgi.py, static url /media/ and /static/

定义:

  1. 域 - 您的域名 (example.com)
  2. path - 项目的完整路径,manage.py 所在的文件夹 (/var/www/example.com/myproj)
  3. project - 命名项目,文件夹名称 wsgi.py (myproj)

https://gist.github.com/leotop/9a78c10c0961cdd2bbe9633d6ebf>8

<VirtualHost *:80>
Define domain example.com
Define path /var/www/${domain}/myproj
Define project myproj


ServerName ${domain}
ServerAlias www.${domain}

DocumentRoot ${path}

WSGIDaemonProcess ${domain} python-path=${path}

WSGIScriptAlias / ${path}/${project}/wsgi.py \
    process-group=${domain} application-group=%{GLOBAL}

<Directory ${path}/${project}>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>


Alias /media/ ${path}/media/
Alias /static/ ${path}/static/
Alias /robots.txt ${path}/static/robots.txt
Alias /favicon.ico ${path}/static/favicon.ico

<Location "/media/">
    Options -Indexes
</Location>

<Location "/static/">
    Options -Indexes
</Location>

ErrorLog /var/log/apache2/${domain}-error.log
LogLevel warn
CustomLog /var/log/apache2/${domain}-access.log combined

</VirtualHost>

这篇关于使用 mod_wsgi 在 Apache2 上部署 Django - Django 项目的正确位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆