亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

第一次訪問 django 服務

本小節會創建我們的第一個 Django 工程以及第一個應用,接下來的所有示例將會在這個工程基礎上進行演示。

1. 創建第一個Django應用程序

在創建第一個 Django 應用程序之前,我們需要使用 pyenv 工具創建相應的虛擬環境,操作如下:

新建一個統一的目錄,用于存放 Django 工程代碼:

[root@server ~]# mkdir django-manual
[root@server ~]# cd django-manual/

進入虛擬環境,然后建立 django-manual 虛擬環境。一般而言每個 Django 工程會創建一個虛擬環境,這樣避免各個 Python 項目之間發生包沖突。建立好虛擬環境之后,激活虛擬環境。操作如下:

[root@server django-manual]# pyenv versions
  system
* 3.8.1 (set by /root/.pyenv/version)
  3.8.1/envs/env-3.8.1
  env-3.8.1
 
# 新建django-manual虛擬環境
[root@server django-manual]# pyenv virtualenv 3.8.1 django-manual
Looking in links: /tmp/tmpllz1yd5e
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.1/envs/django-manual/lib/python3.8/site-packages (41.2.0)
Requirement already satisfied: pip in /root/.pyenv/versions/3.8.1/envs/django-manual/lib/python3.8/site-packages (19.2.3)
# 手動新建的虛擬環境
[root@server django-manual]# pyenv activate django-manual
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(django-manual) [root@server django-manual]#

接下來,我們需要安裝 Django 2.2.11 版本(提示: django 3.0 最近發布了,但是還處于初步完善階段,所以本次介紹以 Django 2.2.11 版本為準):

(django-manual) [root@server django-manual]# pip install django==2.2.11 -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting django==2.2.11
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/be/76/7ccbcf52366590ca76997ce7860308b257b79962a4e4fada5353f72d7be5/Django-2.2.11-py3-none-any.whl (7.5MB)
     |████████████████████████████████| 7.5MB 71kB/s 
Requirement already satisfied: sqlparse in /root/.pyenv/versions/3.8.1/envs/django-manual/lib/python3.8/site-packages (from django==2.2.11) (0.3.1)
Requirement already satisfied: pytz in /root/.pyenv/versions/3.8.1/envs/django-manual/lib/python3.8/site-packages (from django==2.2.11) (2019.3)
Installing collected packages: django
Successfully installed django-2.2.11
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

(django-manual) [root@server django-manual]# python -c "import django; print(django.__version__)"
2.2.11

這樣子,虛擬環境中就安裝好了 Django 2.2.11。Django 提供 django-admin 命令來幫助我們創建項目和應用,我們只需要使用 django-admin 命令即可快速創建我們的第一個 Django 項目:

(django-manual) [root@server django-manual]# django-admin startproject first_django_app
(django-manual) [root@server django-manual]# (django-manual) [root@server django-manual]# tree .
.
└── first_django_app
    ├── first_django_app
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── manage.py

2 directories, 5 files

Tips:盡量在 Linux 平臺上完成實驗,在 Windows 下操作在安裝 mysqlclient 模塊是會稍微有些工作要做。

Django 項目可以由多個應用(app)組成,每個應用是一個邏輯上劃分,即將某一個功能模塊劃歸到這個應用。創建一個應用使用 django-admin starapp 應用名即可:

(django-manual) [root@server django-manual]# cd first_django_app/
(django-manual) [root@server first_django_app]# django-admin startapp hello_app
(django-manual) [root@server first_django_app]# tree .
.
├── first_django_app
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── hello_app
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

3 directories, 12 files

可以看到,在使用 django-admin 執行創建 hello_app 應用后,該命令給我們生成了 hello_app 以及若干代碼文件。為了能讓 Django 項目運行起來,我們需要調整下 settings.py 文件中的配置:

# settings.py 中默認使用 sqlite3
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
...

# 現在調整成 mysql 數據庫,讀者需要自行準備mysql服務
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',   
        'NAME': 'django_manual',     
        'USER': 'store',             
        'PASSWORD': 'xxxxxxxxx',
        'HOST': '180.76.152.113',    
        'PORT': '9000',
    }
}

有了數據庫支持,還需要在 Django 那邊安裝 mysql 相關的模塊包。通常安裝的是 mysqlclient 模塊:

# 安裝相應的依賴包
(django-manual) [root@server first_django_app]# yum install mysql-devel -y
(django-manual) [root@server first_django_app]# pip install mysqlclient -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB)
     |████████████████████████████████| 92kB 22.2MB/s 
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-1.4.6

最后一件事情,在啟動 Django 服務之前,必須要先創建數據庫。Django 服務默認并不會幫我們創建好數據庫,我們必須手工建好數據庫,然后再啟動 Django 服務:

[root@server ~]# mysql -u store -pxxxxxxxxx -h 180.76.152.113 -P9000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 37328
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> CREATE DATABASE IF NOT EXISTS django_manual DEFAULT CHARSET utf8;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| alarms             |
| dashboard          |
| django_manual      |
| graph              |
| mysql              |
| performance_schema |
| sys                |
| uic                |
+--------------------+
15 rows in set (0.00 sec)
MySQL [(none)]> exit;
Bye
# ---------------------------------------------------------------------------------------

(django-manual) [root@server first_django_app]# python manage.py runserver 0.0.0.0:8888
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 13, 2020 - 07:29:06
Django version 2.2.11, using settings 'first_django_app.settings'
Starting development server at http://0.0.0.0:8888/
Quit the server with CONTROL-C.

在完成上述這些步驟后,基本的工程就搭建起來了。接下來我們從外面訪問這個端口結果如下:

圖片描述

第一次訪問 django 服務

這個是 Django 在配置中做的一個白名單機制,它有一個 ALLOWED_HOSTS 配置參數,它用來設置訪問服務的白名單。如果想要允許任何主機訪問,直接設置如下:

(django-manual) [root@server first_django_app]# cat first_django_app/settings.py
...
DEBUG = True
ALLOWED_HOSTS = ['*']
...

另外,默認 setting.py 中的 DEBUG 參數為 True。正因為如此,請求報錯才會有如此詳細的提示。在正真上線部署時候,這個參數一定要關閉。如果我設置如下參數再次從外部請求該 Django 服務時,瀏覽器的輸出結果如下圖所示??梢钥吹?,除了顯示一個冷冰冰 400 錯誤,無任何提示。這樣屏蔽錯誤信息,防止有人從錯誤結果中推斷服務漏洞,達到滲透的目的。

(django-manual) [root@server first_django_app]# cat first_django_app/settings.py
...
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
...

圖片描述

設置 Debug=False 的錯誤輸出

我們重新設置好 DEBUG 和 ALLOWED_HOSTS 參數后,再次請求 Django 服務,可以得到 Dajngo 內置的歡迎頁面,提示我們服務已經正常啟動和運行。

圖片描述

正常訪問 Django 服務

現在,我們寫一個最簡單的 Hello, World 字符串輸出到頁面上。改動 first_django_app/first_django_app/url.py 文件,這個文件是所有 url 請求路由的入口,所有的映射關系都會先通過這里:

(django-manual) [root@server first_django_app]# pwd
/root/django-manual/first_django_app
(django-manual) [root@server first_django_app]# cat first_django_app/urls.py 
"""
注釋性文本,省略
"""
from django.contrib import admin
from django.urls import path
## 新導入模塊
from django.http import HttpResponse

## 視圖函數
def hello_world(*args, **kwargs):
    return HttpResponse("Hello, world.", content_type="text/plain")

urlpatterns = [
    path('admin/', admin.site.urls),
    ####添加的url映射,由上面的hello_world()函數處理
    path('hello/', hello_world),
]

再次啟動 Django 服務,訪問 8888 端口的 /hello/ 路徑,可以看到頁面出現 “Hello, world.” 這樣的字符,說明我們的第一個 URL 接口完成。
圖片描述

頁面輸出 Hello,World.

2. Django應用線上部署

對于 Django 應用的線上部署,往往有以下兩種方案:

2.1 使用 gunicorn 工具

gunicorn 是一個 Unix 上被廣泛使用的高性能的 Python WSGI UNIX HTTP Server。和大多數的 Web 框架兼容,并具有實現簡單,輕量級,高性能等特點。用它部署 Flask/Django 這樣的 Python Web 項目再合適不過了。它的安裝和使用都十分方便,安裝直接在虛擬環境下執行: pip install gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple,使用有兩種方式:

直接使用

# 參數說明:
#    -b: 啟動綁定ip和端口,0.0.0.0 是指運行外面的所有機器訪問
#    -w: 啟動 worker 進程數
#    --daemon: 后臺啟動
(django-manual) [root@server first_django_app]#  gunicorn first_django_app.wsgi -b 0.0.0.0:8888 --daemon -w 4

配置文件使用

(django-manual) [root@server first_django_app]# cat gunicorn_config.py
# gunicorn_config.py
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing

bind = '0.0.0.0:8888'
# backlog: 服務器中在pending狀態的最大連接數,即client處于waiting的數目。超過這個數目, client連接會得到一個error
backlog = 512
timeout = 30    
workers = multiprocessing.cpu_count() * 2
threads = 2
loglevel = 'info' 
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' 
accesslog = "/var/log/django-manual/gunicorn_access.log"     
errorlog = "/var/log/django-manual/gunicorn_error.log"

(django-manual) [root@server first_django_app]# gunicorn first_django_app.wsgi -c gunicorn_config.py --daemon

關閉進程

# 簡單粗暴的關閉方式
(django-manual) [root@server first_django_app]# killall gunicorn

2.2 使用 uwsgi 工具部署和安裝 Django 服務

安裝 uwsgi : pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple。使用 uwsgi 啟動 Django 服務,同樣有直接使用命令行啟動和使用配置文件啟動兩種方式:

# 新建uwsgi的pid保存目錄
(django-manual) [root@server first_django_app]# mkdir uwsgi/
(django-manual) [root@server first_django_app]# cat uwsgi.ini
[uwsgi]
# socket = 0.0.0.0:8888
# 使用http協議訪問
http = 0.0.0.0:8888
# 指定執行的目錄
chdir = /root/django-manual/first_django_app
# 非常重要,指定執行的wsgi.py文件
module = first_django_app.wsgi                

master = true
processes = 5
threads = 5
vacuum = true
stats=%(chdir)/uwsgi/uwsgi.status
pidfile=%(chdir)/uwsgi/uwsgi.pid

# 啟動 django 服務 
(django-manual) [root@server first_django_app]# uwsgi -d --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini

(django-manual) [root@server first_django_app]# ps -ef | grep uwsgi
root     10250     1  4 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10282 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10283 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10284 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10285 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10286 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10287 10250  0 19:32 ?        00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.ini
root     10344  4650  0 19:33 pts/3    00:00:00 grep --color=auto uwsgi

# 停止進程用--stop,重載用--reload
(django-manual) [root@server first_django_app]# uwsgi --stop uwsgi/uwsgi.pid 
(django-manual) [root@server first_django_app]# ps -ef | grep uwsgi
root     10566  4650  0 19:35 pts/3    00:00:00 grep --color=auto uwsgi

Tips:如果在配置文件中使用 socket 監聽端口,則需要使用 nginx 轉發 http 協議為 uwsgi 協議 才行在瀏覽器中訪問。

3. 小結

本小節我們詳細介紹了如何創建一個最簡單的 Django 應用程序,然后講解了由 django-admin 工具生成的所有目錄與文件,最后講解了 Django 應用在線上的部署過程。