How to deploy a django server?
Table of contents
Introduction
First of all, before getting into manner let's know a bit more what is django and why use it.
According django official webpage:
Well, we need to explain what a framework is to know this point better:
To understand what Django is actually for, we need to take a closer look at the servers. The first thing is that the server needs to know that you want it to serve you a web page.
Imagine a mailbox (port) which is monitored for incoming letters (requests). This is done by a web server. The web server reads the letter and then sends a response with a webpage. But when you want to send something, you need to have some content. And Django is something that helps you create the content.
The description above is a little bit simplified, but you don't need to know all the technical things yet. Having a general idea is enough.
Now you have the basics to start learning django, but if you already uploaded a project to github/gitlab and you're willing to deploy it in a VPS, let's dive into it.
According django official webpage:
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.By this point, why django and not the main language(python)?
Well, we need to explain what a framework is to know this point better:
To understand what Django is actually for, we need to take a closer look at the servers. The first thing is that the server needs to know that you want it to serve you a web page.
Imagine a mailbox (port) which is monitored for incoming letters (requests). This is done by a web server. The web server reads the letter and then sends a response with a webpage. But when you want to send something, you need to have some content. And Django is something that helps you create the content.
The description above is a little bit simplified, but you don't need to know all the technical things yet. Having a general idea is enough.
Now you have the basics to start learning django, but if you already uploaded a project to github/gitlab and you're willing to deploy it in a VPS, let's dive into it.
Installation
First of all, we're going to install all dependencies, this will be a bit boring but it is mostly copy pasting commands:
Python:
sudo apt-get install build-essential python-dev python-setuptools
Pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
Virtualenv:
pip install virtualenv
WSGI mod:
sudo apt-get install libapache2-mod-wsgi
Python:
sudo apt-get install build-essential python-dev python-setuptools
Pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
Virtualenv:
pip install virtualenv
WSGI mod:
sudo apt-get install libapache2-mod-wsgi
Deploy
Now that all needed technologies are installed, it's time to start.
First, we will do a clone of our project, in my case I'll use an example app from a MIT licensed project:
This clone will be done inside the virtualhost folder, in this case chaferser.tk.
git clone https://github.com/gothinkster/django-realworld-example-app.git
cd django-realworld-example-app/
After the clone change your location inside the cloned folder and proced with the virtualenv creation:
virtualenv productionready
To activate this one once it is createad, cd yourself inside the folder and proceed with the following command:
cd productionready source bin/activate
You can deactivate it at any time using command: deactivate.
After that, return to the parent folder and install the requirements.txt using pip.
cd ..
pip install -r requirements.txt
Finally do the migrations for the database and we're ready to configure apache2.
python manage.py makemigrations
python manage.py migrate
First, we will do a clone of our project, in my case I'll use an example app from a MIT licensed project:
This clone will be done inside the virtualhost folder, in this case chaferser.tk.
git clone https://github.com/gothinkster/django-realworld-example-app.git
cd django-realworld-example-app/
After the clone change your location inside the cloned folder and proced with the virtualenv creation:
virtualenv productionready
A Virtual Environment avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way.
To activate this one once it is createad, cd yourself inside the folder and proceed with the following command:
cd productionready source bin/activate
You can deactivate it at any time using command: deactivate.
After that, return to the parent folder and install the requirements.txt using pip.
cd ..
pip install -r requirements.txt
Finally do the migrations for the database and we're ready to configure apache2.
python manage.py makemigrations
python manage.py migrate
Apache2 Configuration
Finally, let's move to apache2 configuration files and do the following modification to the chaferser.tk.conf file.
sudo nano /etc/apache2/sites-available/chaferser.tk
After that is saved just enable wsgi mod and restart apache2.
a2enmod wsgi
sudo systemctl restart apache2
PD: If it doesn't allow you to access the domain, It could be that you're not an allowed host, change settings.py inside the project folder and change allowed hosts var to '*'.
Here is a final look at our django rest framework working on the given domain:
sudo nano /etc/apache2/sites-available/chaferser.tk
After that is saved just enable wsgi mod and restart apache2.
a2enmod wsgi
sudo systemctl restart apache2
PD: If it doesn't allow you to access the domain, It could be that you're not an allowed host, change settings.py inside the project folder and change allowed hosts var to '*'.
Here is a final look at our django rest framework working on the given domain:
What do you think? Will you try it?
Thanks for reading the entry!
Comentarios
Publicar un comentario