Deploying django by python manage.py runserver to production on VPS
Asked Answered
N

2

14

I've bought a VPS for my django app. I've never ever before deployed django, or any other application. I've read a few tutorials on how to deploy django using apache and wsgi and some other options gunicorn, ngix. To me it all seems very frustrating and hard to understand. I was wondering what would happen if I deployed my application by changing debug to false, creating a database, plugging it in and then simply running python manage.py runserver my-ip. Is this a bad practice ?

Nucleonics answered 19/4, 2014 at 23:6 Comment(3)
There's a lot of hate here, but I think it's a valuable question - the documentation I've read doesn't explain why you need a web server, gunicorn etc, and since I can serve my dev server without one it's interesting to know what they are providing that is so necessary.Ensoul
I also don't understand why no one can provide an adequate answer beyond "just don't". Has accepting dictums without understanding the underlying rationale ever been a good idea?Alternator
The main reason is that the Django server is not considered secure. Generally-speaking data needs protection even within a protected environment, which makes the manage.py runserver unsuitable for production.Keenankeene
D
6

It is easy to learn how to deploy a Django project.

At first, you should know how to install Apache and mod_wsgi

if you use Ubuntu

sudo apt-get install apache2 libapache2-mod-wsgi

or Fedora (red hat) (without test)

yum install httpd mod_wsgi

Then, you should know how to associate Apache2 with your Django project

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin [email protected]

    Alias /media/ /home/tu/blog/media/

    <Directory /home/tu/blog/media>
        Require all granted
    </Directory>

    WSGIScriptAlias / /home/tu/blog/blog/wsgi.py

    <Directory /path/to/django/project/wsgifile>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

The sentence WSGIScriptAlias associate Apache2 configuration with your Django project in Django wsgi.py file, you will see project.settings included, that is how it works

The following may be easy to understand how it works

*.conf --> wsgi.py --> settings.py --> urls.py and apps

just search in Google like ubuntu django server mod_wsgi and learn it yourself!

Deoxyribose answered 20/4, 2014 at 4:54 Comment(0)
I
3

Do not do use the dev server in production. Just don't.

At the very least serve it off gunicorn:

$ pip install gunicorn
$ cd your_project
$ gunicorn project.wsgi  # gunicorn now runs locally on port 8000

And have nginx (or apache) as a reverse proxy:

server {
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

Deploying Django (just like any other application) is a world in and of itself, and mastering it takes time. But don't ever run the development server in production.

Insolate answered 19/4, 2014 at 23:8 Comment(5)
Could you please explain why I need gunicorn and nginx or apache? I take your word on it that I do, but I'd like to understand what they're providing me.Ensoul
Why not use development server in production in very small scale scenarios? Add explanation please. See serverfault.com/q/717568/134848.Wisner
Please provide explanation why not to use dev server in production?Ankeny
@Ankeny - from the Django docs: "DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests."Insolate
Well, you would only need nginx as a reverse proxy if you serve static files, in case your django app is an API, you only would need gunicorn.Townsville

© 2022 - 2024 — McMap. All rights reserved.