1、系统环境,必要知识
1
2
3
4
|
#cat /etc/redhat-release centos linux release 7.5 . 1804 (core) #uname -r 3.10 . 0 - 862.3 . 2.el7 .x86_64 |
暂时关闭防护墙,关闭selinux:
1
2
3
4
|
#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依赖包
1
|
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
1
2
3
4
5
6
7
8
9
10
11
|
#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方便调试 |
测试安装是否正常:
1
2
3
4
5
|
#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
1
2
3
|
#pip3 install django #ln -s /usr/local/python356/bin/django-admin /usr/local/bin/django-admin |
创建项目:
1
|
#django-admin startproject mysite |
创建app:
1
2
|
#cd mysite/ #django-admin startapp app01 |
在项目路径下创建模板文件目录(templates)和静态文件目录(static),后面会用到
1
2
|
# cd mysite/ # mkdir templates static |
配置setting.py文件,配置允许访问主机名、将app加入到项目、添加模板搜索路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 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文件中创建视图函数
1
2
3
4
5
|
# vim app01/views.py from django.shortcuts import render def index(request): return render(request, "app01/index.html" ) |
在templates目录中新建测试模板文件
1
2
3
4
|
# mkdir -p templates/app01 # vim templates/app01/index.html <h1>test django server< / h1> |
在urls.py文件中建立路由
1
2
3
4
5
6
7
8
9
|
# 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' ), #添加的路由 ] |
检查配置是否有错误:
1
2
|
# python3 manage.py check system check identified no issues ( 0 silenced). |
运行测试服务器,测试服务是否正常
1
2
3
4
5
|
# python3 manage.py runserver 0:8000 #在浏览器中测试是否正常显示网页内容 #注意防火墙和selinux是否关闭或允许相关端口访问 #curl 127.0.0.1:8000 <h1>test django server< / h1> |
4、安装uwsgi
使用python的pip工具包安装:
1
2
3
4
5
|
#pip3 install uwsgi #ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi #建立软链接 #uwsgi --version #检查安装成功 2.0 . 17 |
先在项目目录下建立一个测试文件:
1
2
3
4
|
# vim foobar.py def application(env,start_response): start_response( '200 ok' ,[( 'content-type' , 'text/html' )]) return [b "hello world" ] |
然后采用测试脚本方式启动服务:
1
2
3
|
# uwsgi --http :9000 --wsgi-file foobar.py #通过浏览器访问http://ip:9000能看到hello world说明成功 #然后停止服务 |
接下来配置uwsgi部署django服务:
1
2
3
4
5
6
7
|
#运行测试服务,--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反向代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#注意此处的配置指定用户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
重新启动实例:
1
2
3
|
#kill -hup `cat /tmp/project-mast.pid` #uwsgi --reload /tmp/project-master.pid |
还可以在python程序中使用uwsgi.reload()重新加载
停止服务器:
1
2
3
|
#uwsgi --stop /tmp/project-master.pid #kill -int `cat /tmp/project-master.pid` |
编写启动脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#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系统服务脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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启动脚本后需要加载下:
1
|
systemctl daemon - reload |
测试服务:
1
2
3
4
5
6
7
8
9
10
|
#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 查看就清楚了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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:
1
|
yum - y install nginx |
配置nginx反向代理uwsgi服务:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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日志报错:
1
|
bind(): permission denied [core / socket.c line 230 ] |
经过第一个错误折腾,终于可以了,但放入/run目录后,uwsgi启动不了了,权限不够,这个问题解决简单,只需给/run目录,有uwsgi服务用户也就是uid指定的用户有写入权限即可,或者直接给/run目录777的权限但这样不安全。
1
|
#chmod o+w /run #我的解决方法 |
下面是uwsgi和nginx的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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安装配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#下载二进制包安装 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 | + - - - - - - - - - - - - - - - - + |
1
2
3
4
|
#建立个数据库后面要用到 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数据库:
1
2
3
4
5
6
|
databases = { 'default' : { 'engine' : 'django.db.backends.sqlite3' , 'name' : os.path.join(base_dir, 'db.sqlite3' ), } } |
修改成mysql数据库配置:
1
2
3
4
5
6
7
8
9
10
|
databases = { 'default' :{ 'engine' : 'django.db.backends.mysql' , 'name' : 'django' , 'user' : 'root' , 'password' : '123.com' , 'host' : '127.0.0.1' , 'port' : '3306' , } } |
engine : 指定数据库驱动,不同的数据库这个字段不同,下面是常见的集中数据库的engine的写法:
1
2
3
4
|
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的开发包
1
2
|
#yum install mysql-devel #安装mysql插件 #pip3 install mysqlclient #安装mysql驱动 |
1
2
|
#测试django配置 # python3 manage.py check |
环境搭建到此就完成了,下面开始开发项目
7、编写一个简单的博客系统
(1)数据建模,规划博客内容字段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#首先装两个包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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#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后台
1
2
3
4
5
6
|
# vim app01/admin.py from django.contrib import admin from .models import blog_articles #导入数据 admin.site.register(blog_articles) #加载到后台 |
(4)配置url.py路由
1
2
3
4
5
6
7
8
9
10
|
# 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)收集静态文件,生成数据并迁移
1
2
3
4
5
6
7
8
|
# 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)创建后台账户并验证后台添加文章
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 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 电子邮件地址: admin@admin.com 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:
1
2
3
4
5
6
7
8
9
10
11
12
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# 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 = "{{ i.id }}" rel = "external nofollow" rel = "external nofollow" ><img src = "/static/{{ i.picture }}" >< / a>< / span>{{ i.description|safe }}< / p> <a href = "{{ i.id }}" 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/{{ article.picture }}" >< / span>< / p> <span>{{ article.body|safe }}< / span> < / dev> |
修改路由:urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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服务.... #查看显示页:点击详情页验证!!! |
添加后台过滤器:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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类用于生成数据表:
1
2
3
4
5
6
7
8
9
10
|
#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中:
1
2
3
4
5
6
7
8
9
|
installed_apps = [ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , 'app01' ] |
根据model类创建数据库表:
1
2
3
4
5
|
#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下建立模版文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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安全机制注销了:
1
2
3
4
5
6
7
8
9
|
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' , ] |
#建立路由:
1
2
3
4
5
6
7
8
|
#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:
1
2
|
#uwsgi --stop uwsgi.pid #uwsgi --ini uwsgi.ini |
#浏览器访问:http://192.168.146.139/home
#提交数据到数据库后并返回给浏览器客户端
虚拟环境搭建实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
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生产环境搭建内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/zhangxinqi/p/9084126.html