Deploy django project using NGINX and Gunicorn
Marwan Fazora
2 min read
Deploy django project using NGINX and Gunicorn
Requirements:
- NGINX
- Gunicorn
Prepare environment:
- Install pip:
sudo apt-get install python3-pip
- Install virtualenv:
sudo pip3 install virtualenv
- Create directory for the project:
mkdir project_name cd project_name
- Now create a virtual environment
virtualenv project_venv -p 3
- Now Activate it:
source project_venv/bin/activate
Prepare project files and dependencies:
- Upload project to the folder that you just created "project_name"
- Upload static folder to project_name
. └── project_name/ ├── project/ │ ├── requirements.txt │ ├── ... │ └── project/ │ ├── ... │ ├── settings.py │ ├── urls.py │ ├── wsgi.py │ └── ... ├── project_venv/ │ └── bin/ │ ├── activate │ └── ... └── static/ └── ...
- Install requirements:
pip3 install -r project/requirements.txt
Prepare Gunicorn server:
- Install Gunicorn:
pip3 install gunicorn
- Create Configuration file
mkdir conf touch conf/gunicorn.py
Paste this configuration into conf/gunicorn.conf
command = '/path_to_project/project_venv/bin/gunicorn' #example: '/home/user/project_name/venv/bin/gunicorn' pythonpath = '/path_to_project' #example: '/home/user/project_name' bind = '127.0.0.1:8000' workers = 3
- Now run Gunicorn server:
gunicorn -c conf/gunicorn.py project.wsgi
- Output should be like:
[2022-12-05 10:44:14 +0000] [3053259] [INFO] Starting gunicorn 20.1.0 [2022-12-05 10:44:14 +0000] [3053259] [INFO] Listening at: http://127.0.0.1:8000 (3053259) [2022-12-05 10:44:14 +0000] [3053259] [INFO] Using worker: sync [2022-12-05 10:44:14 +0000] [3053261] [INFO] Booting worker with pid: 3053261 [2022-12-05 10:44:14 +0000] [3053262] [INFO] Booting worker with pid: 3053262 [2022-12-05 10:44:15 +0000] [3053263] [INFO] Booting worker with pid: 3053263
- Now your project structure should be like this:
. └── project_name/ ├── conf/ │ └── gunicorn.py ├── project/ │ ├── requirements.txt │ ├── ... │ └── project/ │ ├── ... │ ├── settings.py │ ├── urls.py │ ├── wsgi.py │ └── ... ├── project_venv/ │ └── bin/ │ ├── activate │ └── ... └── static/ └── ...
Prepare NGINX:
Install NGINX:
# update system sudo apt update # install nginx sudo apt install nginx # check if nginx installed successfully sudo systemctl status nginx.service # you should see active (running)
Create NGINX config file:
sudo -i cd /etc/nginx/sites-available touch "project_name"
Paste this configuration into "project_name"
server { server_name server_ip_or_domain; location /static/ { alias path_to_static_dir; # example: /home/user/project_name/static/ } location / { proxy_pass http://127.0.0.1:8000; # bind from gunicorn conf } }
Activate this project:
cd /etc/nginx/sites-enabled ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/project_name
Restart NGINX:
systemctl restart nginx.service
Finally you now have an active django website deployed with NGINX and gunicorn :)
0
Subscribe to my newsletter
Read articles from Marwan Fazora directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Marwan Fazora
Marwan Fazora
I am Marwan Fazora, a proficient full-stack web developer with a strong command of Python and Django. Possessing expertise in both front-end and back-end development, I specialize in crafting secure and high-performance web applications. I am eager to collaborate and contribute to transforming conceptual ideas into tangible solutions.