我们项目部署需要用到uwsgi,它可以开启多进程,启动多个uwsgi实例,然后让nginx来做静态文件处理,以及负载均衡和反向代理.
{启动多个uwsgi通过nginx做集群,静态文件放到nginx,静态请求通过nginx直接相应,动态请求转发给uwsgi,如果在Windows上开发的话,配置了虚拟环境,否则安装一些python包和依赖会有问题}
找到uwsgi的配置文件把参数配置好,
然后把nginx的配置文件参数配置好,
就可以运行项目了.
需要注意配置项里面的一切细节,我们的django项目里面的静态文件路径需要跟我们的nginx里面的local static里面静态文件路径,这两个地方需要匹配上,否则系统无法找到静态文件.
本文转载自www.55yyy.top,已注明出处
centos7下部署Django(nginx+uwsgi+python3+django) 安装各类基础模块 yum install gcc-c++ yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel yum install libxml* yum -y install zlib* yum remove lrzsz -y yum install lrzsz -y 关闭防火墙 systemctl stop firewalld ———————————————————————————————- 编译安装python3 mkdir /application/ cd /application/ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar xf Python-3.6.3.tgz -C /usr/local/src/ cd /usr/local/src/Python-3.6.3/ ./configure –prefix=/usr/local/python3 make && make install 将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾 source /etc/profile 给python3安装django和uwsgi以及配置启动项目的xml文件 注:如果出现一片红,那就多执行几遍 pip3 install django pip3 install uwsgi ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3 ————————————————————————————————- uwsgi配置 上传项目目录 cd /application/ mkdir 项目 cd 项目,上传项目 runserver测试 python3 项目目录/manage.py runserver 10.0.0.8:80 mkdir script cd script vim uwsgi.ini [uwsgi] # 项目目录 chdir=/application/opt/teacher # 启动uwsgi的用户名和用户组 uid=root gid=root # 指定项目的application module=teacher.wsgi:application # 指定sock的文件路径 socket=/application/opt/script/uwsgi.sock # 启用主进程 master=true # 进程个数 workers=5 pidfile=/application/opt/script/uwsgi.pid # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/application/opt/script/uwsgi.log ######################################## 启动uwsgi uwsgi –ini uwsgi.ini uwsgi –stop uwsgi.pid 查看uwsgi日志 tail -f /application/opt/script/uwsgi.log ————————————————————————————————- 安装nginx和配置nginx.conf文件 cd /application/ wget http://nginx.org/download/nginx-1.10.3.tar.gz yum install pcre-devel openssl-devel -y tar xf nginx-1.10.3.tar.gz cd nginx-1.10.3 useradd -s /sbin/nologin -M www ./configure make &&make install ————————————————————————————————- nginx配置 >/usr/local/nginx/conf/nginx.conf 编辑conf文件 worker_processes 8; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” “$http_x_forwarded_for”‘; server { listen 80; server_name 10.0.0.8; access_log logs/access.log main; charset utf-8; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定项目路径uwsgi location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/application/opt/script/uwsgi.sock; } # 指定静态文件路径 location /static/{ alias /application/opt/teacher/static/; index index.html index.htm; } } } 启动nginx /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx 查看nginx日志 tail -f /usr/local/nginx/logs/access.log ———————————————————– 关闭uwsgi和nginx uwsgi –stop uwsgi.pid /usr/local/nginx/sbin/nginx -s stop cd 到项目目录下创建admin python3 manage.py migrate 创建admin超级用户 python3 manage.py createsuperuser python3 manage.py collectstatic –noinput
上面的copy过来的文件里面 --符号有转意,
例如: python3 manage.py collectstatic --noinput
参考博客地址:
https://www.cnblogs.com/heysn21/articles/9080385.html
centos7下部署Django(nginx+uwsgi+python3+django)
安装各类基础模块
yum install gcc-c++
yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
yum install libxml*
yum -y install zlib*
yum remove lrzsz -y
yum install lrzsz -y
关闭防火墙
systemctl stop firewalld
———————————————————————————————-
编译安装python3
mkdir /application/
cd /application/
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
tar xf Python-3.6.3.tgz -C /usr/local/src/
cd /usr/local/src/Python-3.6.3/
./configure –prefix=/usr/local/python3
make && make install
将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾
source /etc/profile
给python3安装django和uwsgi以及配置启动项目的xml文件
注:如果出现一片红,那就多执行几遍
pip3 install django
pip3 install uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
————————————————————————————————-
uwsgi配置
上传项目目录
cd /application/
mkdir 项目
cd 项目,上传项目
runserver测试
python3 项目目录/manage.py runserver 10.0.0.8:80
mkdir script
cd script
vim uwsgi.ini
[uwsgi]
# 项目目录
chdir=/application/opt/teacher
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 指定项目的application
module=teacher.wsgi:application
# 指定sock的文件路径
socket=/application/opt/script/uwsgi.sock
# 启用主进程
master=true
# 进程个数
workers=5
pidfile=/application/opt/script/uwsgi.pid
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/application/opt/script/uwsgi.log
########################################
启动uwsgi
uwsgi –ini uwsgi.ini
uwsgi –stop uwsgi.pid
查看uwsgi日志
tail -f /application/opt/script/uwsgi.log
————————————————————————————————-
安装nginx和配置nginx.conf文件
cd /application/
wget http://nginx.org/download/nginx-1.10.3.tar.gz
yum install pcre-devel openssl-devel -y
tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
useradd -s /sbin/nologin -M www
./configure
make &&make install
————————————————————————————————-
nginx配置
>/usr/local/nginx/conf/nginx.conf
编辑conf文件
worker_processes 8;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
server {
listen 80;
server_name 10.0.0.8;
access_log logs/access.log main;
charset utf-8;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# 指定项目路径uwsgi
location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/application/opt/script/uwsgi.sock;
}
# 指定静态文件路径
location /static/{
alias /application/opt/teacher/static/;
index index.html index.htm;
}
}
}
启动nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
查看nginx日志
tail -f /usr/local/nginx/logs/access.log
———————————————————–
关闭uwsgi和nginx
uwsgi –stop uwsgi.pid
/usr/local/nginx/sbin/nginx -s stop
cd 到项目目录下创建admin
python3 manage.py migrate
创建admin超级用户
python3 manage.py createsuperuser
python3 manage.py collectstatic –noinput
原文地址:https://www.cnblogs.com/2012-dream/p/9124076.html