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

查看:282
本文介绍了使用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.

根据这些教程设置Django与mod_wsgi:

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/ )< br>
当我通过浏览器访问我的网络服务器,我看到的是默认的index.html网站。
从我的Web服务器运行并访问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/'

谢谢!

编辑:

最后,我发现问题是我在我的服务器上的不同的现成配置文件之间的冲突。显然,让我们加密SSL配置文件覆盖/ etc / apache2 / sites-available中的所有其他文件。当我将WSGI-config行放在那里,它的工作就像一个魅力。

我现在有我的代码在我的主目录,它正在工作。我决定把静态文件放到/ var / www / static中,通过 collectstatic 。这对我来说很有用,但似乎代码和静态文件的确切位置确实是无关紧要的。


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的系统) p>

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/ 或使用服务器IP。

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 path + virtual,示例

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






使用define变量配置apache2.4。 Django> 1.6。只设置域,路径,名称项目。
例如路径到dir /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. 路径 - 完整路径project.py文件夹(/var/www/example.com/myproj)

  3. 项目 - 名称项目,文件夹名称其中wsgi.py(myproj)

https://gist.github。 com / leotop / 9a78c10c0961cdd2bbe9633d6ebf8a5b

<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天全站免登陆