安裝環境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,多實例,優化,動靜分離