We have used:
python version: python3
machine: linux
django version: django-2.x
Project Structure
Lets assume that you have a django project and the folder structure looks like this:
About structure
mysite/ is our django actual project
config/ folder has config.json file which contains all our configuration file.
mysite/settings/ folder contains main setting of our application.
Setting is seprated in base.py, development.py and production.py.
If we want to run in development mode we need to pass the settings file.
base.py is common settings file and development.py is for development mode.
For running app in development we do: python3 manage.py runserver –settings=mysite.settings.development.
Requiremnts/ folder has our requirement files
base.txt– all pacakges in our app
development.txt – packages only used in development(e.g django-debug-toolbar)
production.txt – package used in production environment but not in development.
mysite/ contains main djago apps.
mysite_apps/ contains all our apps(note this folder is a python package)
Requirements
nginx: Nginx is used as a server to serve static files in our project to the user. gunicorn: This is an used as an application server to serve our application. In development mode we can do python3 manage.py runserver and it will run the app. If we want to run the app using gunicorn we need to configure gunicorn in our application. We will see the configuration later. supervisor: This tool is used to run gunicorn. Setting supervisor will help our application to restart in case of any problem. Supervisor is configured to handle running of gunicorn.
What is the flow of application:
user request —> nginx server—> gunicorn(served by supervisor)—>our django app
Steps:
- Install requirements
- Setup virtualenvironment for our application
- Clone the project from github or other version control system
- Install requirements of our application(i.e. packages used by our app)
- Setting up postgres database
- Run migration and test application by running it
- Configure gunicorn
- Configure nginx
- Configure supervisord (or simply supervisor)
- Test everything is running fine
1. Install requirements:
Here we assume that we are using python3 for our application and we are deploying on linux machine. We need to install python3 using pip, virtualenv. Enough talk lets dive into it: Log into the vps machine using the credentials given by your vps provide. Once you are in we go through following steps: First lets update our machine and install python3 and virtualenv:
2. Setup virtualenvironment for our application:
Lets create a virutal environment and start the virtualenv
3. Clone the project from github or other version control system
Now we clone our project from github:
4. Install requirements of our application(i.e. packages used by our app)
Once the app is downloaded and virtualenv is running lets install the requirements. All our project dependent packages are in base.txt file and we use production.txt file to install from base.txt and also other packages from production.txt. We go to folder where manage.py is and run following command:
5. Setting Up postgres database
- first go to postgres as:
- Then create database and user and set password in user:
- Grant permission:
- After granting permission log out from psql using ‘\q’ symbolizing quite. Then change the postgres user to root user as:
6. Run migration and test application by running it
After testing everything stop the server.
7. Configure gunicorn
Lets install gunicorn:
After installing gunicorn lets run our application using gunicorn for testing
Note: This is similar to saying ./manage.py runserver –settings=mysite.settings.production in development.
We can also pass workers to gunicorn using workers parameter as:
Once we verify everything is working fine we move on to configuring nginx.
8. Configure nginx
Lets install nginx:
After installation lets configure nginx file in /etc/nginx/sites-available/ Lets create a file name mysite in /etc/nginx/sites-available/mysite and add following configuation:
Here in place ipaddress(e.g.x.x.x.x) use the ipaddress provided by your host provider.
Now, check if our configuration file was correctly written and start our nginx:
9. Configure supervisord (or simply supervisor)
Installation:
Now, let’s create mysite.conf in /etc/supervisor/conf.d/ folder:
and configure our project:
Now lets start our app uing supervisor
Now our app should be running on the ipaddress configured by you check for further details.