What Should You Know While Deploying Django Website on Linux Server?
Django is a powerful web framework of python known to build APIs, Websites, Web Apps, etc. It has a much bigger community due to its easy backend - python. Developing a django app is easy but configuring it for deployment is bit confusing. We spent a lot of time looking for a solution while deploying so here's quick guide to check if the issue is in code or in server...
Type of Server Errors
Here is a quick list of server errors you may encounter while deploying the website -
Code | Error | Description |
---|---|---|
502 | Bad Gateway | Either the failure of gunicorn service due to some reason or gunicorn timeout which default is 30 seconds. |
500 | Internal Server | Code Error |
504 | Timeout | Nginx Timeout Error |
404 | Not Found | File/Source Not Found |
403 | Forbidden | Request Failed (Solution-Retry) |
First of All
You should be familar with django behaviours i.e. what error means what it will help you a lot to search for solution more specific. Use the Error Table to check the type of error then debug. You should also be familiar with the database you are using. Sometimes changing datatypes of some fields can cause error so you should have proper knowledge of your database.
Recommended Django Setup For Deployment
Starting New Project:
1) Add * to Allowed Hosts
2) Add admin & other dependices statics to your_static_path .
3) Check Database configuration
4) Must Push the code from that directory where your project, apps and manage.py exists. or .git folder must be in directory where your manage.py is located.
5) Create Requirements.txt
6) Add .pyc, media, virtualenv or files that you don't want to push in git version control to .gitignore
to avoid future conflicts.
Updating Code:
1) Check DB.
2) Update req.txt if any new installation.
3) Check Full Project Once.
DB Errors
In the journey of deployment, you might face many db errors
but make sure to take a backup of db before making any critical changes to it. You can do this by this snippet:
python3 manage.py dumpdata > datadump.json
Now you are good to make any changes in your database. In case, you have to load your original db back use this snippet:
python3 manage.py migrate --run-syncdb
python3 manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
python3 manage.py loaddata datadump.json
Debugging Print Statements in Server
Yes, It's true that you can see print statement outputs in production as well! To see print statements in server you need to check logs of your gunicorn service by using:
However, it will be little messy because it contains all logs of your service so, but it's still very helpful.
Additional Tip
RUN_MAIN
Variable doesn't work in servers so if you have any process under this variable make sure to get an alternate of this. This variables only gets true when you run command:
Related Posts
You may like these post too
Comments on this post
0 comments
Leave a comment
it’s easy to post a comment