共计 26488 个字符,预计需要花费 67 分钟才能阅读完成。
阅读目录
1、系统环境,必要知识
2、安装 Python3.6.5
3、安装 uWSGI
4、安装 nginx
5、安装 Django
6、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、安装 uWSGI
使用 python 的 pip 工具包安装:
#pip3 install uwsgi
#ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi #建立软链接
#uwsgi –version #检查安装成功
2.0.17
建立 uWSGI 的配置文件, 在 django 项目中建立 uwsgi.ini 文件:
[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:指定监听地址和端口
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 文件路径
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 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
测试服务:
#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.
4、安装 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; #指定 django 的静态文件
}
location / {
include /etc/nginx/uwsgi_params; #加载 uwsgi 模块
uwsgi_pass 127.0.0.1:9090; #所有请求转到 9090 端口交给 uwsgi 处理
}
}
}
5、安装 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
先建立个测试文件:
#cat settings.py
ALLOWED_HOSTS = [‘192.168.146.139’] #添加本地 IP,外网访问
#cat urls.py
from django.contrib import admin
from django.urls import path
from app01 import views #导入 app01 视图
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(”,views.index,name=’index’), #添加路由
]
#cat ../app01/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request): #添加视图函数
return HttpResponse(‘hello world’)
重新加载 uwsgi:
#uwsgi –reload uwsgi.pid
访问:http://192.168.146.139 能显示“hello world”说明环境部署成功了。
(1)配置 Django 模版文件的使用:
首先必须在项目的 setting 文件中配置 templates 模版文件的搜索路径; 找到 TEMPLATES 项, 在 DIRS 中写入模版搜索路径:
‘DIRS’: [os.path.join(BASE_DIR,’templates’)], #此处为项目目录下的 templates
配置 home 路由,在视图中使用 render 渲染模版文件,然后建立模版文件进行测试。
#vim app01/views.py
def home(request):
return render(request,’app01/home.html’)
#vim mysite/urls.py
urlpatterns = [
path(‘home/’,views.home),
]
#mkdir -p templates/app01
#vim templates/app01/home.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>test</title>
</head>
<body>
<h1>this is test file</h1>
</body>
</html>
测试:http://192.168.146.139/home
(2)配置 djando 静态文件加载
首先在 django 的项目文件中,打开 setting.py,找到 STATIC_URL 填写搜索路径
#STATIC_URL = ‘/static/’ #在 linux 下这两种写法都可以, 在 windows 系统下必须两种都要写上,不知何解!
STATICFILES_DIRS = (
os.path.join(BASE_DIR,’static’),
)
# 在模块中引入静态文件时,必须是 /static,此静态文件在项目下面
<script src=”https://www.linuxidc.com/static/jquery-3.3.1.js”></script>
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)
(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 驱动
(2)通过 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=”https://www.linuxidc.com/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
# 提交数据到数据库后并返回给浏览器客户端
阅读目录
1、系统环境,必要知识
2、安装 Python3.6.5
3、安装 uWSGI
4、安装 nginx
5、安装 Django
6、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、安装 uWSGI
使用 python 的 pip 工具包安装:
#pip3 install uwsgi
#ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi #建立软链接
#uwsgi –version #检查安装成功
2.0.17
建立 uWSGI 的配置文件, 在 django 项目中建立 uwsgi.ini 文件:
[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:指定监听地址和端口
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 文件路径
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 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
测试服务:
#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.
4、安装 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; #指定 django 的静态文件
}
location / {
include /etc/nginx/uwsgi_params; #加载 uwsgi 模块
uwsgi_pass 127.0.0.1:9090; #所有请求转到 9090 端口交给 uwsgi 处理
}
}
}
5、安装 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
先建立个测试文件:
#cat settings.py
ALLOWED_HOSTS = [‘192.168.146.139’] #添加本地 IP,外网访问
#cat urls.py
from django.contrib import admin
from django.urls import path
from app01 import views #导入 app01 视图
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(”,views.index,name=’index’), #添加路由
]
#cat ../app01/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request): #添加视图函数
return HttpResponse(‘hello world’)
重新加载 uwsgi:
#uwsgi –reload uwsgi.pid
访问:http://192.168.146.139 能显示“hello world”说明环境部署成功了。
(1)配置 Django 模版文件的使用:
首先必须在项目的 setting 文件中配置 templates 模版文件的搜索路径; 找到 TEMPLATES 项, 在 DIRS 中写入模版搜索路径:
‘DIRS’: [os.path.join(BASE_DIR,’templates’)], #此处为项目目录下的 templates
配置 home 路由,在视图中使用 render 渲染模版文件,然后建立模版文件进行测试。
#vim app01/views.py
def home(request):
return render(request,’app01/home.html’)
#vim mysite/urls.py
urlpatterns = [
path(‘home/’,views.home),
]
#mkdir -p templates/app01
#vim templates/app01/home.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>test</title>
</head>
<body>
<h1>this is test file</h1>
</body>
</html>
测试:http://192.168.146.139/home
(2)配置 djando 静态文件加载
首先在 django 的项目文件中,打开 setting.py,找到 STATIC_URL 填写搜索路径
#STATIC_URL = ‘/static/’ #在 linux 下这两种写法都可以, 在 windows 系统下必须两种都要写上,不知何解!
STATICFILES_DIRS = (
os.path.join(BASE_DIR,’static’),
)
# 在模块中引入静态文件时,必须是 /static,此静态文件在项目下面
<script src=”https://www.linuxidc.com/static/jquery-3.3.1.js”></script>
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)
(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 驱动
(2)通过 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=”https://www.linuxidc.com/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
# 提交数据到数据库后并返回给浏览器客户端