安装环境html
Remote: CentOS 7.4 x64 (django.example.com)python
Python: Python3.6.5linux
Django: Django 2.0.4nginx
nWSGI: uwsgi-2.0.15sql
Nginx: nginx- 1.10.2-1.el6数据库
1.关闭iptables和selinuxdjango
# su - rootflask
# service iptables stopbash
# setenforce 0服务器
# vi /etc/sysconfig/selinux
修改
SELINUX=disabled
2.添加本地host DNS
# vi /etc/hosts
127.0.0.1 django.example.com
1.安装python3.6.5源及依赖包
# yum install epel-release -y
# yum groupinstall "Development tools" -y
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y
2.编译安装python3.6.5以及pip package manager
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
# tar xf Python-3.6.5.tar.xz
# cd Python-3.6.5
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && make altinstall
3.安装virtualenv
# pip3.6 install --upgrade pip
# pip3.6 install virtualenv
1. 安装nginx package
# yum install nginx -y
2.配置nginx with nWSGI
# vi /etc/nginx/conf.d/django.conf
server { listen 80; server_name django.example.com; charset utf-8; access_log /var/log/nginx/django_access.log; error_log /var/log/nginx/django_error.log; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /usr/share/nginx/html/django.example.com; } client_max_body_size 20M; location / { include uwsgi_params; uwsgi_pass unix:/etc/uwsgi/uwsgi-django.sock; uwsgi_read_timeout 30s; uwsgi_send_timeout 30s; } }
1. uWSGI配置
# mkdir -p /etc/uwsgi
# vi /etc/uwsgi/uwsgi-django.ini
[uwsgi] project = django.example.com base = /data/www chdir = %(base)/%(project) home = %(base)/%(project)/.py3env module = myproject.wsgi:application pidfile = /tmp/uwsgi-master-django.pid master = true processes = 2 enable-threads = true # use unix socket because it is more secure and faster than TCP socket socket = /etc/uwsgi/uwsgi-django.sock chmod-socket = 660 uid = nginx gid = nginx vacuum = true die-on-term = true logto = /var/log/nginx/uwsgi-django.log
socket : 地址和端口号,例如:socket = 127.0.0.1:50000
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。因为GIL的存在,我以为这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 容许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最经常使用的,仍是把运行记录输出到一个本地文件上。
log-maxsize :以固定的文件大小(单位KB),切割日志文件。 例如:log-maxsize = 50000000 就是50M一个日志文件。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。若是不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
log-maxsize: 日志大小,当大于这个大小会进行切分 (Byte)
log-truncate: 当启动时切分日志
2. 配置Django base folder
# cd /usr/share/nginx/html
# mkdir django.example.com
# cd django.example.com
# virtualenv -p /usr/local/bin/python3 .py3env
3. 开启virtualenv python3环境
# source .py3env/bin/activate
4. 在此环境安装Django相关模块
# pip install django uwsgi PyMySQL
5. 建立uWSGI启动脚本
# mkdir -p /etc/uwsgi/bin
# vi /etc/systemd/system/uwsgi-django.service
[Unit] Description=uWSGI instance to serve myproject [Service] BASE=/data/www/django.example.com ENV=$BASE/.py3env ExecStartPre=-/usr/bin/bash -c 'chown -R nginx:nginx /etc/uwsgi' ExecStart=/usr/bin/bash -c 'source /usr/share/nginx/html/django.example.com/.py3env/bin/activate; uwsgi --ini /etc/uwsgi/uwsgi-django.ini' [Install] WantedBy=multi-user.target
1. 保证virtualenv python3环境开启
# cd /usr/share/nginx/html/django.example.com/
# source .py3env/bin/activate
2.建立一个Django项目
# django-admin startproject myproject .
3.添加static目录
# vi myproject/settings.py
末行添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
4.建立本地SQLlite文件
Tip:这里使用SQLlite代替其余数据库做为咱们项目的DB
# ./manage.py makemigrations
# ./manage.py migrate
5.建立项目管理员帐户
# ./manage.py createsuperuser
6.生成项目静态文件目录
# ./manage.py collectstatic
7.修改wsgi入口文件
# vi myproject/wsgi.py
import os import sys os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") sys.path.append('/usr/share/nginx/html/django.example.com') from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
8.添加ALLOWED_HOSTS
# vi myproject/settings.py
ALLOWED_HOSTS = ['django.example.com']
9. 修改权限(可执行并保持与nginx启动user一致)
# chmod -R 755 /etc/uwsgi
# chown -R nginx:nginx /etc/uwsgi
# chmod -R 755 /usr/share/nginx/html/django.example.com
# chown -R nginx:nginx /usr/share/nginx/html/django.example.com
10.启动nginx+uwsgi
# systemctl restart nginx
# systemctl restart uwsgi-django
ps.欠flask,多实例,优化,动静分离