Django EC2 deployment with wsgi and Apache

跟風遠走 提交于 2019-12-13 01:15:05

问题


I have a micro instance on EC2 with django installed there. I've also installed mod-wsgi, postgresql etc following several tutorials. Finally I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is defauld Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.

my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This is my .conf file stored in /etc/apache2/httpd.conf:

Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/

<VirtualHost *:80>
    DocumentRoot /home/ubuntu/my_project
    ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
    ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
    ErrorLog /home/ubuntu/my_project/apache/error_log
    CustomLog /home/ubuntu/my_project/access_log_common

    WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
    Alias /static/ /home/ubuntu/my_project/static/

    <Directory /home/ubuntu/my_project/static>
        Require all granted
    </Directory>

    <Directory /home/ubuntu/my_project>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>

When I'm trying to

/etc/init.d/apache2 stop 

and instead:

python manage.py runserver 0.0.0.0:80

It works fine and I'm able to connect to my app from browser with IP as url. It gives me understanding that setting.py file is fine (am I right?) and problem is or in wsgi.py or in httpd.conf.

When I stop my development server and start Apache again, I get defauld Apache page. Can you help me please to find whats wrong with my files?

Permissions for apache:

ubuntu@ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/

回答1:


You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.

You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.

It is bad security practice to set DocumentRoot to be where your source code for your application is.

Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.

Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.



来源:https://stackoverflow.com/questions/23876947/django-ec2-deployment-with-wsgi-and-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!