首页 > 系统相关 >10_How deploy a Django application using Nginx & Gunicorn in Production

10_How deploy a Django application using Nginx & Gunicorn in Production

时间:2023-06-07 22:11:06浏览次数:82  
标签:10 gunicorn projectdir nginx sudo Django How Copy Gunicorn

 

地址:https://www.codewithharry.com/blogpost/django-deploy-nginx-gunicorn/

 

 

How to host Django Application using gunicorn & nginx in Production

In this post, we will see how to use nginx with gunicorn to serve django applications in production. 

Django is a very powerful web framework and ships with a server which is able to facilitate development. This development server is not scalable and is not suited for production. Hence we need to configure gunicorn to get better scalability and nginx can be used as a reverse proxy and as a web server to serve static files. Let's get started 

Before you follow the steps outlined below, I will assume that you have already configured your Ubuntu server with a non root user and firewall as outlined here.

 

 

 

Step 1 - Installing python and nginx

Let's update the server's package index using the command below:

sudo apt update
sudo apt install python3-pip python3-dev nginx

This will install python, pip and nginx server

Step 2 - Creating a python virtual environment 

sudo pip3 install virtualenv

This will install a virtual environment package in python. Let's create a project directory to host our Django application and create a virtual environment inside that directory.

mkdir ~/projectdir
cd ~/projectdir
virtualenv env

A virtual environment named env will be created. Let's activate this virtual environment:

source env/bin/activate

Step 3 - Installing Django and gunicorn

pip install django gunicorn

This installs Django and gunicorn in our virtual environment

 

 

 

Step 4 - Setting up our Django project

At this point you can either copy your existing Django project into the projectdir folder or create a fresh one as shown below:

django-admin startproject textutils ~/projectdir

Add your IP address or domain to the ALLOWED_HOSTS variable in settings.py.

If you have any migrations to run, perform that action:

~/projectdir/manage.py makemigrations
~/projectdir/manage.py migrate

Let's test this sample project by running the following commands:

sudo ufw allow 8000

This opens port 8000 by allowing it over the firewall. Let's start our Django development server to test the setup so far:

~/projectdir/manage.py runserver 0.0.0.0:8000

 

 

 

Step 5 - Configuring gunicorn

Lets test gunicorn's ability to serve our application by firing the following commands:

gunicorn --bind 0.0.0.0:8000 textutils.wsgi

This should start gunicorn on port 8000. We can go back to the browser to test our application. Visiting http://<ip-address>:8000 shows a page like this:

Deactivate the virtualenvironment by executing the command below:

deactivate

Let's create a system socket file for gunicorn now:

sudo vim /etc/systemd/system/gunicorn.socket

Paste the contents below and save the file

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Next, we will create a service file for gunicorn

sudo vim /etc/systemd/system/gunicorn.service

 

 

 

Paste the contents below inside this file:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=harry
Group=www-data
WorkingDirectory=/home/harry/projectdir
ExecStart=/home/harry/projectdir/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          textutils.wsgi:application

[Install]
WantedBy=multi-user.target

Lets now start and enable the gunicorn socket

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

Step 6 - Configuring Nginx as a reverse proxy

Create a configuration file for Nginx using the following command

sudo vim /etc/nginx/sites-available/textutils

Paste the below contents inside the file created

server {
    listen 80;
    server_name www.codewithharry.in;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/harry/projectdir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Activate the configuration using the following command:

sudo ln -s /etc/nginx/sites-available/textutils /etc/nginx/sites-enabled/

Restart nginx and allow the changes to take place.

sudo systemctl restart nginx

Your Django website should now work fine! Happy Coding

 

 

 

 

 

 

 

 

 

 

标签:10,gunicorn,projectdir,nginx,sudo,Django,How,Copy,Gunicorn
From: https://www.cnblogs.com/hechunfeng/p/17464721.html

相关文章

  • 4_How to Host Multiple Websites on One Server
     地址:https://www.codewithharry.com/blogpost/host-multiple-websites-ubuntu-vps/  HowtoHostMultipleWebsitesonUbuntuVPS?Anapache2webserverprovidesrobustnessandscalabilityforhostingmultiplewebsitesonyourUbuntuVPS.Thismeansyoucan......
  • 小白,尝试用while,for ,if语句表示1到100相加的和。
    #include<stdio.h>intmain(){ inta=1; intsum=0; while(a<=100) { sum=sum+a; a++; } printf("总和=%d\n",sum); return0;}#include<stdio.h>intmain(){ inta=0; intsum=0; { for(a=1;a<=......
  • Windows10 家庭版开启远程桌面
    Windows10家庭版启用远程桌面1、win10家庭版升级专业版输入产品密钥(最好在无网络的时候操作):VK7JG-NPHTM-C97JM-9MPGT-3V66T(我用的这个)W269N-WFGWX-YVC9B-4J6C9-T83GXNYW94-47Q7H-7X9TT-W7TXD-JTYPMNJ4MX-VQQ7Q-FP3DB-VDGHX-7XM87MH37W-N47XK-V7XM9-C7227-GCQG9升级后......
  • 软测5班Loadrunner阶段性考试(2019-10-19)
    试题1:用你在Loadrunner中所学习的知识,将“欢迎来到然学科技”保存为一个变量,并且在日志中打印输出(10分)。答案:lr_save_string("欢迎来到科技","ranther");lr_output_message("你好:%s",lr_eval_string("{ran}"));试题2:Loadrunner中如何保持每次参数取值的唯一性(2分)?Unique+Once(保持......
  • 软测5班jmeter笔记(2019-10-29)
    接口测试理论自动化测试的金字塔模型硬件接口:比如usb接口,电源接口、耳机接口...软件接口:数据系统访问接口、http请求接口...为什么要做接口测试Web前端:指用户可以直观操作和看到的界面。html,Css样式,javascript脚本。android和ios等。web后端:是指与数据库交互进行处理响应的业务......
  • Win10 powershell提示符修改
    在“文档”目录下新建“Microsoft.PowerShell_profile.ps1”,#只显示当前文件夹名称functionprompt{$p=pwd|Split-Path-Leaf"$p:"}#下边是conda的配置命令(&"C:\Users\hello\anaconda3\Scripts\conda.exe""shell.powershell""hook")......
  • 我在<电脑编程技巧与维护>2010年8月刊上写的文章
    ......
  • 性能测试-批量向数据库中构造数据(10万条)
    通过存储过程向数据库sys_member表中插入10万条数据:向苏汽系统中Mysql插入批量数据(构造10万条数据):第1种方法:通过存储过程实现DROPPROCEDUREIFEXISTSmy_insert;CREATEPROCEDUREmy_insert()BEGINDECLAREnintDEFAULT0;loopname:LOOPinse......
  • day10-Spring Cloud Alibaba Nacos-服务注册与配置中心
    SpringCloudAlibabaNacos-服务注册与配置中心官网:https://github.com/alibaba/nacos,Nacos官方中文手册Nacos:SpringCloudAlibaba服务注册与配置中心(非常详细)(biancheng.net)SpringCloudAlibaba系列-一文读懂Nacos原理-掘金(juejin.cn)1.什么是NacosNacos/nɑ:k......
  • win10 共享文件夹给本地打印机
    在“启用活关闭Windows功能”里,找到“SMB1.0/CIFS文件共享支持”,勾选开启;建议在系统属性里,把计算机名设置成简单的字母,比如hi,然后重启电脑;在  防火墙和网络保护→允许应用通过防火墙,里面找到“文件与打印机共享”,后面两个复选框全部勾选;在任意磁盘新建一个文件夹,我这新建......