django生產環境搭建(uWSGI+django+nginx+python+MySQL)

1、系統環境,必要知識

#cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
#uname -r
3.10.0-862.3.2.el7.x86_64

暫時關閉防護墻,關閉selinux:

#systemctl stop firewalld.service
#setenforce 0
#getenforce 
Permissive

準備知識:

django:一個基於python的開源web框架。

uWSGI:一個基於自有的uwsgi協議,wsgi協議和http服務協議的web網關

nginx:高性能的代理web服務器

wsgi.py:django項目自帶的wsgi接口文件(位於:項目/項目名/wsgi.py)

整個項目流程:

首先客戶端通過瀏覽器訪問服務器資源;nginx作為對外服務的端口(80),nginx接收到客戶端http請求後會解包分析,如果是靜態文件就去配置的靜態文件中查找資源並返回給客戶端,如果是動態資源,nginx就通過配置文件將請求傳遞給uwsgi處理,並轉發給uwsgi,wsgi根據請求調用django工程的文件和函數,處理後django將返回值交給wsgi,wsgi將返回值進行打包,轉發給uWSGI,uWSGI接收到數據後轉發給nginx,最終返回給客戶端。

2、安裝python3.6.5

(1)安裝python依賴包

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel

(2)安裝python

#wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
#mkdir -p /usr/local/python356
#tar zxvf Python-3.6.5.tgz
#cd Python-3.6.5
#./configure --prefix=/usr/local/python356
#make
#make install
#ln -s /usr/local/python356/bin/python3 /usr/local/bin/python3
#ln -s /usr/local/python356/bin/pip3 /usr/local/bin/pip3
#pip3 install --upgrade pip  #更新pip
#pip3 install ipython  #安裝ipython方便調試

測試安裝是否正常:

#python3 -V
Python 3.6.5

#pip3 -V
pip 10.0.1 from /usr/local/python356/lib/python3.6/site-packages/pip (python 3.6)

3、安裝Django

#pip3 install django

#ln -s /usr/local/python356/bin/django-admin /usr/local/bin/django-admin

創建項目:

#django-admin startproject mysite

創建app:

#cd mysite/
#django-admin startapp app01

在項目路徑下創建模板文件目錄(templates)和靜態文件目錄(static),後面會用到

# cd mysite/
# mkdir templates static

配置setting.py文件,配置允許訪問主機名、將APP加入到項目、添加模板搜索路徑

# vim mysite/settings.py

ALLOWED_HOSTS = ["*"]  #允許本機所有地址訪問

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',      #加入app名
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],  #加入模板搜索路徑
......
]

在views.py文件中創建視圖函數

# vim app01/views.py
from django.shortcuts import render

def index(request):
    return render(request,"app01/index.html")

在templates目錄中新建測試模板文件

# mkdir -p templates/app01
# vim templates/app01/index.html

<h1>test django server</h1>

在urls.py文件中建立路由

# vim mysite/urls.py
from django.contrib import admin
from django.urls import path,re_path  #添加
from app01 import views  #添加

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^$',views.index,name='index'),  #添加的路由
]

檢查配置是否有錯誤:

# python3 manage.py check
System check identified no issues (0 silenced).

運行測試服務器,測試服務是否正常

# python3 manage.py runserver 0:8000
#在瀏覽器中測試是否正常顯示網頁內容
#註意防火墻和selinux是否關閉或允許相關端口訪問
#curl 127.0.0.1:8000
<h1>test django server</h1>

4、安裝uWSGI

使用python的pip工具包安裝:

#pip3 install uwsgi

#ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi  #建立軟鏈接
#uwsgi --version  #檢查安裝成功
2.0.17

先在項目目錄下建立一個測試文件:

# vim foobar.py
def application(env,start_response):
    start_response('200 ok',[('Content-Type','text/html')])
    return [b"Hello World"]

然後采用測試腳本方式啟動服務:

# uwsgi --http :9000 --wsgi-file foobar.py 
#通過瀏覽器訪問http://ip:9000能看到hello world說明成功
#然後停止服務

接下來配置uwsgi部署django服務:

#運行測試服務,--http指定服務端口,--chdir指定django項目目錄,--wsgi-file指定項目中的wsgi.py文件
# uwsgi --http :9000 --chdir /opt/mysite/ --wsgi-file mysite/wsgi.py --master --ocesses 4 --threads 2 --stats 127.0.0.1:9191

#通過瀏覽器http://ip:9000
#如能訪問到diango模板中的html文件中的內容就算成功
#我的上面django模板中配置的內容是
test django server

建立uWSGI的配置文件,在django項目中建立uwsgi.ini文件,並配置好django項目,此處使用socket鏈接,後面通過nginx反向代理

#註意此處的配置指定用戶nginx必須存在

[uwsgi]
socket = 127.0.0.1:9090
chdir = /djproject/mysite
module = mysite.wsgi    #這裡填的是相對路徑
master = true
processes = 2
threads = 2
max-requests = 2000
vacuum = true
daemonize = /djproject/mysite/uwsgi.log
stats = 127.0.0.1:9001
post-buffering = 65535
buffer-size = 65535
harakiri-verbose = true
harakiri = 300
uid = nginx
pidfile = /djproject/mysite/uwsgi.pid

參數說明:

socket:使用套接字鏈接

http:通過http加端口的方式

wsgi-file:指定加載的wsgi文件

chdir:指定工程的絕對路徑,如Django的項目路徑

module:指定web應用的入口模塊,如Django項目下的wsgi.py接口文件

master:啟動主進程

processes:啟動進程數

threads:啟動線程數

max-requests:最大請求數

daemonize:指定uWSGI日志文件路徑

stats:指定狀態查詢端口,如:127.0.0.1:9001

wsgi-file:指定啟動的文件

post-buffering:設置緩沖區

buffer-size:設置緩沖區文件大小

harakiri-verbose:設置超時true為開啟

harakiri:設置超時時間

uid、gid:設置用戶和組

pidfile:指定啟動時的pid文件路徑

vacuum = true #當服務器退出的時候自動刪除unix socket文件和pid文件

uwsgi信號控制:

HUP     :優雅的重新加載所有進程和配置,同(–reload)一樣

TERM  :強制重新加載主進程和配置

 INT  :立即殺死整個uWSGI;同於:–stop

QUIT  :立即殺死真個uWSGI

重新啟動實例:

#kill -HUP `cat /tmp/project-mast.pid`

#uwsgi --reload /tmp/project-master.pid

還可以在python程序中使用uwsgi.reload()重新加載

停止服務器:

#uwsgi --stop /tmp/project-master.pid

#kill -INT `cat /tmp/project-master.pid`

編寫啟動腳本:

#vim /etc/init.d/uwsgi 

#!/bin/bash
#this is uwsgi server script

. /etc/init.d/functions

uwsgi=/usr/local/bin/uwsgi
uwsgi_pid=/djangoproject/mysite/uwsig.pid
uwsgi_conf=/djangoproject/mysite/uwsigconf.ini
uwsgi_pn=`ps -ef|grep -v "grep"|grep -c "uwsgi"`
ERVAL=0
start(){
    $uwsgi --ini $uwsgi_conf >& /dev/unll
    ERVAL=$?
    if [ $ERVAL -eq 0 ];then
        action "uwsgid starting ..." /bin/true
    else
        action "uwsgid start is error" /bin/false
    fi
}

stop(){
    $uwsgi --stop $uwsgi_pid >& /dev/null
    ERVAL=$?
    if [ $ERVAL -eq 0 ];then
        action "uwsgid stoping ..." /bin/true
    else
        action "uwsgid stop is error" /bin/false
    fi
}


case "$1" in
    start)
        if [ $uwsgi_pn -ge 5 ];then
            action "uwsgi is running!" /bin/false
        else
            start
            ERVAL=0
        fi
        ;;
    stop)
        if [ $uwsgi_pn -ge 5 ];then
            stop
            ERVAL=0
        else
            action "uwsgi no running!" /bin/false
        fi
        ;;
    reload)
        if [ $uwsgi_pn -ge 5 ];then
            $uwsgi --reload $uwsgi_pid >&/dev/null
            ERVAL=$?
            [ $ERVAL -eq 0 ] && action "uwsgi reloading ..." /bin/true
        else
            action "uwsgi reload error" /bin/false
        fi
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        ERVAL=2
esac
exit $ERVAL

 centOS7 system系統服務腳本:

#cat /usr/lib/systemd/system/uwsgi.service
[Unit]
Description=uwsgi service
After=network.target

[Service]
Type=forking
PIDFile=/run/uwsgi.pid
ExecStartPre=/usr/bin/rm -f /run/uwsgi.pid
ExecStart=/usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

centOS7建立system啟動腳本後需要加載下:

systemctl daemon-reload

測試服務:

#uwsgi --ini /djproject/mysite/uwsgi.ini    #啟動服務

#ps -ef|grep "uwsgi"   #查看進程
root     103596      1  2 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini
root     103598 103596  0 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini
root     103599 103596  0 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini

#netstat -lntp|grep "uwsgi"
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      103596/uwsgi        
tcp        0      0 127.0.0.1:9001          0.0.0.0:*               LISTEN      103596/uwsgi 

看上面進程是少瞭一個主進程,通過systemctl 查看就清楚瞭:

#systemctl status -l uwsgi.service 
● uwsgi.service - uwsgi service
   Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; disabled; vendor preset: disabled)
   Active: active (running) since 五 2018-05-25 16:02:06 CST; 4min 14s ago
  Process: 103593 ExecStart=/usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini (code=exited, status=0/SUCCESS)
  Process: 103591 ExecStartPre=/usr/bin/rm -f /run/uwsgi.pid (code=exited, status=0/SUCCESS)
 Main PID: 103596 (uwsgi)
   CGroup: /system.slice/uwsgi.service
           ├─103596 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini
           ├─103598 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini
           └─103599 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini

5月 25 16:02:06 ZPY systemd[1]: Starting uwsgi service...
5月 25 16:02:06 ZPY uwsgi[103593]: [uWSGI] getting INI configuration from /djproject/mysite/uwsgi.ini
5月 25 16:02:06 ZPY systemd[1]: PID file /run/uwsgi.pid not readable (yet?) after start.
5月 25 16:02:06 ZPY systemd[1]: Started uwsgi service.

#通過查看uwsgi的日志,如沒報錯,就接著下一步,部署nginx

5、安裝nginx

這裡采用yum安裝nginx:

yum -y install nginx

配置nginx反向代理uwsgi服務:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

server {
    listen 80;
    server_name localhost;
    charset     utf-8;
    access_log      /var/log/nginx/nginx_access.log main;
    error_log       /var/log/nginx/nginx_error.log;
    client_max_body_size 75M;


    location /static {
        alias /djproject/mysite/static/ckeditor/;  #指定django的靜態文件
        }

    location / {
        include     /etc/nginx/uwsgi_params;  #加載uwsgi模塊
        uwsgi_pass  127.0.0.1:9090;   #所有請求轉到9090端口交給uwsgi處理
        }
    }
}

此處記錄下配置uwsgi和nginx套接字通信的坑:

第一個,nginx日志報錯:

connect() to unix:///tmp/uwsgi.sock failed (2: No such file or directory) while connecting to upstream ……

可以明明uwsgi配置sock套接字通信在/tmp目錄下明明有uwsgi.sock文件,而且/tmp目錄是777權限,為什麼nginx找不到套接字文件,經過google,百度查資料終於在stackoverflow上找到答案:是因為centOS7系統中/tmp臨時目錄,使用瞭命名空間的臨時目錄,對於每個服務隻能訪問自己創建的臨時文件,所以我們不應該將sock文件放入/tmp目錄,而因該放入/run目錄下

第二個,uwsgi日志報錯:

bind(): Permission denied [core/socket.c line 230]

經過第一個錯誤折騰,終於可以瞭,但放入/run目錄後,uwsgi啟動不瞭瞭,權限不夠,這個問題解決簡單,隻需給/run目錄,有uwsgi服務用戶也就是uid指定的用戶有寫入權限即可,或者直接給/run目錄777的權限但這樣不安全。

#chmod o+w /run    #我的解決方法

下面是uwsgi和nginx的配置:

#vim /uwsgi.ini

[uwsgi]
socket = /run/uwsgi.sock    #使用sock文件
chdir = /mysite/myblog
module = myblog.wsgi
master = true
processes = 2
threads = 2
max-requests = 2000
vacuum = true
home = /mysite
daemonize = /var/log/uwsgi/uwsgi.log
stats = 127.0.0.1:9001
post-buffering = 65535
buffer-size = 65535
harakiri-verbose = true
harakiri = 300
pidfile = /run/uwsgi.pid
vacuum = true

#vim /etc/ningx/nginx.conf
...
server {
       ......
        location /static {
        alias /opt/mysite/static;
        }
    location / {
        include /etc/nginx/uwsgi_params;
            uwsgi_pass unix:///run/uwsgi.sock;   #指定sock文件
    }
}
...

6、MySQL安裝配置

#下載二進制包安裝
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.32-linux2.6-x86_64.tar.gz
tar zxvf mysql-5.5.32-linux2.6-x86_64.tar.gz
mv mysql-5.5.32-linux2.6-x86_64 /usr/local/mysql-5.5.32
ln -s /usr/local/mysql-5.5.32 /usr/local/mysql
useradd -s /sbin/nologin -M mysql
mkdir /mysql/data -p
chown -R mysql.mysql /mysql/data
cd /usr/local/mysql
#添加配置文件和啟動腳本
cp support-files/my-small.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
#初始化數據庫
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data/
echo $?

#修改啟動腳本路徑
sed -i 's#^basedir=#basedir=/usr/local/mysql#g' /etc/init.d/mysqld
sed -i 's#^datadir=#datadir=/mysql/data#g' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
#啟動和關閉MySQL
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
#方法2:
/usr/local/mysql/bin/msyql_safe &    #後臺啟動
mysqladmin shutdown  #優雅關閉MySQL服務
#查看運行狀態
#netstat -lntup|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      70099/mysqld        

#添加系統自啟動
chkconfig --add mysqld
chkconfig --level 345 mysqld on
#添加環境變量
echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
source /etc/profile

#修改初始化密碼
mysqladmin -uroot password '123456'

#建立一個數據庫,後面要用到
MySQL [(none)]> create database django;
Query OK, 1 row affected (0.00 sec)

#centOS7下通過epel源yum安裝MySQL

# yum install epel-release
# yum -y install mariadb mariadb-devel mariadb-server
# systemctl enable mariadb
# systemctl start mariadb# mysql -e "select version();"
+----------------+
| version()      |
+----------------+
| 5.5.60-MariaDB |
+----------------+
#建立個數據庫後面要用到
MariaDB [(none)]> create database django charset 'utf8';
MariaDB [(none)]> grant all on django.* to 'admin'@'127.0.0.1' identified by '123.com';
MariaDB [(none)]> flush privileges;

(1)配置Django鏈接MySQL:

在setting中,Django默認使用的是sqlite數據庫:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

修改成MySQL數據庫配置:

DATABASES = {
        'default':{
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'root',
        'PASSWORD': '123.com',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        }
}

ENGINE : 指定數據庫驅動,不同的數據庫這個字段不同,下面是常見的集中數據庫的ENGINE的寫法:

django.db.backends.postgresql  # PostgreSQL  
django.db.backends.mysql       # mysql  
django.db.backends.sqlite3     # sqlite  
django.db.backends.oracle      # oracle 

NAME: 指定的數據庫名,如果是sqlite的話,就需要填數據庫文件的絕對位置
USER: 數據庫登錄的用戶名,mysql一般都是root
PASSWORD:登錄數據庫的密碼,必須是USER用戶所對應的密碼
HOST: 由於一般的數據庫都是C/S結構的,所以得指定數據庫服務器的位置,我們一般數據庫服務器和客戶端都是在一臺主機上面,所以一般默認都填127.0.0.1
PORT:數據庫服務器端口,mysql默認為3306
HOST和PORT都可以不填,使用默認的配置,但是如果你有更改默認配置的話,就需要填入更改後的
配置完這,下面就需要裝python連接mysql數據庫的驅動程序,首先,需要安裝mysql的開發包

#yum install mysql-devel   #安裝MySQL插件
#pip3 install mysqlclient    #安裝MySQL驅動
#測試django配置
# python3 manage.py check

環境搭建到此就完成瞭,下面開始開發項目

7、編寫一個簡單的博客系統

(1)數據建模,規劃博客內容字段

#首先裝兩個包django-ckeditor、pillow分別用來添加富文本和縮略圖用

# pip3 install django-ckeditor pillow

#vim app01/models.py

from django.db import models
from django.utils import timezone  #導入時間模板
from django.contrib.auth.models import User  #導入用戶
from ckeditor_uploader.fields import RichTextUploadingField  #導入富文本模板

class blog_articles(models.Model):
    title = models.CharField(max_length=300,verbose_name="文章標題")
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name="blog_posts",verbose_name="文章作者")
    body = RichTextUploadingField(verbose_name="文章內容")
    description = RichTextUploadingField(verbose_name="文章描述")
    publish = models.DateTimeField(default=timezone.now,verbose_name="發表時間")
    picture = models.ImageField(upload_to="images")  #圖片
    class Meta:
        ordering = ("-publish",)   #按時間倒序排列

    def __str__(self):
        return self.title   #顯示文章標題

(2)配置setting.py

#vim mysite/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
    'ckeditor',   #加入模塊到項目
    'ckeditor_uploader',  #加入模板
]

LANGUAGE_CODE = 'zh-hans'   #配置中文

TIME_ZONE = 'Asia/Shanghai'  #配置時區

USE_I18N = True

USE_L10N = True

USE_TZ = False #關閉UTC時間


STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,"static"),]  #配置靜態文件路徑

MEDIA_URL = '/media/'  #媒體文件路由
MEDIA_ROOT = os.path.join(BASE_DIR,"static")  #媒體文件路徑

STATIC_ROOT = os.path.join(BASE_DIR,"static/ckeditor") #配置收集靜態文件路徑

CKEDITOR_UPLOAD_PATH = 'uploads/'  #指定媒體上傳目錄的相對路徑
CKEDITOR_IMAGE_GACKEND = 'pillow'  #使用pillow模板啟用縮略圖功能

#具體的ckedito配置文檔:https://pypi.org/project/django-ckeditor/

(3)配置admin.py後臺

# vim app01/admin.py

from django.contrib import admin
from .models import blog_articles   #導入數據

admin.site.register(blog_articles)  #加載到後臺

(4)配置url.py路由

# vim mysite/urls.py
from django.contrib import admin
from django.urls import path,re_path,include
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^$',views.index,name='index'),
    path(r'ckeditor/',include('ckeditor_uploader.urls'))  #添加ckeditor路由
]

(5)收集靜態文件,生成數據並遷移

# python3 manage.py check
# python3 manage.py collectstatic
# python3 manage.py makemigrations
# python3 manage.py migrate
#執行此處會有一個警告信息,需要配置mysql嚴格模式,在setting.py文件中的DATABASES中的數據引擎中添加以下配置:
'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },

(5)創建後臺賬戶並驗證後臺添加文章

# python3 manage.py creatsuperuser
Unknown command: 'creatsuperuser'. Did you mean createsuperuser?
Type 'manage.py help' for usage.
[root@PJYC7 mysite]# python3 manage.py createsuperuser
用戶名 (leave blank to use 'root'): admin
電子郵件地址: [email protected]
Password: 
Password (again): 
密碼長度太短。密碼必須包含至少 8 個字符。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

#重啟uwsgi和nginx服務
# systemctl restart uwsgi
# systemctl restart nginx

(6)模板展示文章

修改views.py:

#vim app01/views.py 
from django.shortcuts import render,get_object_or_404
from .models import blog_articles

def index(request):    #主頁
    blogs = blog_articles.objects.all()
    return render(request,"app01/index.html",{"blogs":blogs})

def center(request,article_id):   #詳情頁
    article = get_object_or_404(blog_articles,id=article_id)
    pub = article.publish
    return render(request,"app01/content.html",{"article":article,"publish":pub})

建立模板文件:index.html,content.html

# vim templates/app01/index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}個人博客{% endblock %}</title>
<meta name="keywords" content="個人博客" />
<meta name="description" content="個人博客模板" />
<link href="/static/ckeditor/css/base.css" rel="external nofollow"  rel="stylesheet">
<link href="/static/ckeditor/css/index.css" rel="external nofollow"  rel="stylesheet">
<script src="/static/ckeditor/js/scrollReveal.js"></script>
</head>
<body>
 <div>
   <ul class="cbp_tmtimeline">
   {% for i in blogs %}
   <li>
   <time class="cbp_tmtime"><span>{{ i.publish.month }}-{{ i.publish.day }}</span><span>{{ i.publish.year }}</span></time>
   <div class="cbp_tmicon"></div>
   <div class="cbp_tmlabel" data-scroll-reveal="enter right over 1s" >
    <h2>{{ i.title }}</h2>
    <p><span class="blogpic"><a href="{{%20i.id%20}}" rel="external nofollow"  rel="external nofollow" ><img src="/static/{{%20i.picture%20}}"></a></span>{{ i.description|safe }}</p>
     <a href="{{%20i.id%20}}" rel="external nofollow"  rel="external nofollow"  target="_blank" class="readmore">閱讀全文>></a>
       </div>
       </li>
      {% endfor %}
       </ul>
  </div>



# vim templates/app01/content.html
<dev>
<h2>{{ article.title }}</h2>
<p><span class="blogpic"><img src="/static/{{%20article.picture%20}}"></span></p>
<span>{{ article.body|safe }}</span>
</dev>

修改路由:urls.py

from django.contrib import admin
from django.urls import path,re_path,include
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^$',views.index,name='index'),
    path(r'ckeditor/',include('ckeditor_uploader.urls')),
    re_path(r'(?P<article_id>\d)/$',views.center),  #詳情頁獲取ID顯示頁面
]
#重啟uwsgi和nginx服務....

#查看顯示頁:點擊詳情頁驗證!!!

添加後臺過濾器:

#vim app01/admin.py

from django.contrib import admin
from .models import blog_articles

class BlogArticlesAdmin(admin.ModelAdmin):
    list_display = ("title","author","publish")
    list_filter = ("publish","author")
    search_fields = ('title','body')
    raw_id_fields = ("author",)
    date_hierarchy = "publish"

admin.site.register(blog_articles,BlogArticlesAdmin)

(7)表單示例

通過template模版與MySQL實現簡單表單交互

在app目錄下的models文件中創建model類用於生成數據表:

#cat app01/models.py 
from django.db import models

# Create your models here.

class userinfo(models.Model):
    name = models.CharField(max_length=32)
    password = models.CharField(max_length=32)
    age = models.IntegerField()
    salary = models.IntegerField()

設置setting.py文件,將app加入到INSTALLED_APPS中:

INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app01'
        ]

根據model類創建數據庫表:

#cmd進入django項目路徑下
#python manage.py migrate #創建表結構,非model類的其他表,django所需要的
#python manage.py makemigrations app名 #做數據遷移的準備
如:python manage.py makemigrations app01 app01是項目中的app名字
#python manage.py migrate # 執行遷移,創建medel表結構

在templages下建立模版文件:

#cat templates/app01/home.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
    body{
        background-image: url('/static/78556.jpg');
    }
    </style>
</head>
<body>
<form action="" method="post">   #提交數據給自身
      <p><input type="text" name="username"/></p>
      <p><input type="text" name="password"/></p>
      <p><input type="text" name="age"/></p>
      <p><input type="text" name="salary"/></p>
      <p><input type="submit" value="提交"/></p>
</form>
<table border="1">
   <thead>
       <tr>
                <th>用戶名</th>
                <th>密碼</th>
                <th>年齡</th>
                <th>工資</th>
       </tr>
   </thead>
            <tbody>
                {% for item in data %} #循環獲取傳入字典數據
                <tr>
                    <td>{{item.name}}</td>
                    <td>{{item.password}}</td>
                    <td>{{item.age}}</td>
                    <td>{{item.salary}}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>


<h1>this is test file</h1>
<script src="/static/jquery-3.3.1.js"></script>
</body>
</html>

在app下新建視圖函數,與數據庫交互:

#cat app01/views.py

from django.shortcuts import render
from django.http import HttpResponse
from app01 import models   #引入數據類模版
# Create your views here.

def home(request):   #創建home函數處理請求
  if request.method == "POST":   #判斷是否為post提交
    #print(request.POST)
    models.userinfo.objects.create(     #提交表單的數據到數據庫
    name = request.POST['username'],
    password = request.POST['password'],
    age = request.POST['age'],
    salary = request.POST['salary'],
)
  data = models.userinfo.objects.all()   #獲取數據庫數據
  return render(request,'app01/home.html',{'data':data}) #渲染模版文件並傳遞數據庫表給模版

#此處是以post方式提交,需要修改Django項目setting設置中的MIDDLEWARE,將csrf安全機制註銷瞭:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

#建立路由:

#cat mysite/urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/',views.home),
]

重新啟動uWSGI:

#uwsgi --stop uwsgi.pid
#uwsgi --ini uwsgi.ini

#瀏覽器訪問:http://192.168.146.139/home

#提交數據到數據庫後並返回給瀏覽器客戶端

虛擬環境搭建實例:

echo 'PS1="[\[\e[32;40m\]\u\[\e[0m\]@\[\e[32;40m\]\h\[\e[0m\]@\[\e[33;40m\]\A\[\e[0m\]\[\e[36;40m\]\W\[\e[0m\]\[\e[35;40m\]<\#>\[\e[0m\]]\\$"' ~/.bashrc
. ~/.bashrc
#配置epel源:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
#按照依賴包
yum install libffi-devel openssl openssl-devel zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel

#安裝Python3.7
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
tar xvf Python-3.7.0.tar.xz
mkdir -p /usr/local/python37
cd Python-3.7.0
./configure --prefix=/usr/local/python37/
make && make install
ln -s /usr/local/python37/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python37/bin/pip3 /usr/local/bin/pip3
pip3 install --upgrade pip

#配置國內python源
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << EFO
[global]
timeout=60
index-url=https://pypi.tuna.tsinghua.edu.cn/simple

[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn
EFO

#創建虛擬環境
python3 -m venv /mysite
cd mysite/
git clone git://github.com/kennethreitz/autoenv.git
echo 'source /mysite/autoenv/activate.sh' >> ~/.bashrc
source ~/.bashrc
echo "source /mysite/bin/activate" >/mysite/.env

#在虛擬環境中安裝Django
pip install django
django-admin.py startproject myblog
cd myblog/

#測試Django
vim /mysite/myblog/myblog/settings.py
ALLOWED_HOSTS = ["*"]

python manage.py runserver 0.0.0.0:8000

#在虛擬環境中安裝Uwsgi
pip install uwsgi
#測試uwsgi
#創建測試文件
cat test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]

uwsgi --http :9000 --wsgi-file test.py

#使用uwsgi測試django
uwsgi --http :9000 --module myblog.wsgi

#安裝nginx
yum install nginx
systemctl enable nginx
systemctl start nginx

#nginx配置django和uwsgi反向代理:
location /static {
alias /mysite/myblog;
}

location / {
uwsgi_pass 127.0.0.1:9000;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_DIR /mysite/myblog;
uwsgi_param UWSGI_MODULE myblog.wsgi;
uwsgi_param UWSGI_ENV /mysite;
}

#contOS7創建系統服務
cat /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/root/uwsgi/uwsgi --ini /etc/uwsgi/emperor.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target


#uwsgi配置文件:
[uwsgi]
socket = 127.0.0.1:9000
chdir = /root/mysite/myblog
module = myblog.wsgi
master = true
processes = 2
threads = 2
max-requests = 2000
vacuum = true
#home = /root/mysite
daemonize = /var/log/uwsgi/uwsgi.log
stats = 127.0.0.1:9001
post-buffering = 65535
buffer-size = 65535
harakiri-verbose = true
harakiri = 300
pidfile = /run/uwsgi.pid
venv = /root/mysite/.venv

#常用選項:
--http      在指定的地址上添加http端口
--http-socket  使用HTTP協議綁定到指定的UNIX/TCP套接字上
--wsgi-file    加載WSGI文件
--processes    指定進程數
--threads 指定每個進程的線程數
-M --master 啟用主進程
--stats  指定狀態查詢綁定地址端口,如:127.0.0.1:9001
-s --socket  使用默認協議綁定到指定的UNIX/TCP套接字
--chdir:指定工程的絕對路徑,如Django的項目路徑  
--module:指定web應用的API,如Django項目下的wsgi.py接口文件    
--max-requests:最大請求數
--daemonize:指定uWSGI日志文件路徑    
--post-buffering:設置緩沖區
--buffer-size:設置緩沖區文件大小
--harakiri-verbose:設置超時true為開啟
--harakiri:設置超時時間
--uid、--gid:設置用戶和組
--pidfile:指定啟動時的pid文件路徑    
--venv  指定python虛擬環境

到此這篇關於django生產環境搭建(uWSGI+django+nginx+python+MySQL)的文章就介紹到這瞭,更多相關django生產環境搭建內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: