django生产环境部署

作者: ropon 分类: Django 发布时间: 2018-12-14 10:04
安装python3.6.7
1、安装python
访问https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz 下载python源码包
解压
tar xf Python-3.6.7.tar.xz
cd Python-3.6.7
./configure --prefix=/usr/local/python3.6.7 --enable-optimizations
make && make install

为方便调用创建软连接
ln -sf /usr/local/python3.6.7/bin/python3.6 /usr/bin/python3
ln -sf /usr/local/python3.6.7/bin/pip3.6 /usr/bin/pip3

更换pip源
mkdir ~/.pip
cat ~/.pip/pip.conf
[global] 
trusted-host=mirrors.aliyun.com 
index-url=http://mirrors.aliyun.com/pypi/simple/

安装python虚拟环境virtualenv
pip3 install virtualenv

到本地项目路径导出依赖库
pip freeze > ./requirements.txt

建议网站目录
mkdir -p /home/wwwroot

创建python虚拟环境
cd /home/wwwroot/ormpro
virtualenv --python=/usr/bin/python3 .py3env
cd .py3env/bin
进入python虚拟环境
source activate 
退出python虚拟环境
deactivate

安装django和uwsgi
pip3 install django==2.1.3
pip3 install uwsgi

批量安装依赖库
cd /home/wwwroot/ormpro
pip3 install -r ./requirements.txt

cat uwsgi.ini
[uwsgi]
chdir = /home/wwwroot/mydjango/ormpro //项目路径
module = ormpro.wsgi:application //指定wsgi模块
socket = 127.0.0.1:8000
master = true //主进程         
vhost = true //多站模式           
workers = 4 #子进程数
vacuum = true //退出重启时清理文件
#no-site = true
#reload-mercy = 10 
#max-requests = 1000
#limit-as = 512
#buffer-size = 30000
pidfile = /home/wwwroot/mydjango/ormpro/uwsgi8000.pid
daemonize = /home/wwwroot/mydjango/ormpro/run.log
disable-logging = true
uid = www
gid = www

启动uwsgi
source /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --ini /home/wwwroot/mydjango/ormpro/uwsgi.ini
deactivate

重载uwsgi
source /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --reload /home/wwwroot/mydjango/ormpro/uwsgi.pid
deactivate

停止
souruwsgice /home/wwwroot/mydjango/.py3env/bin/activate
uwsgi --stop /home/wwwroot/mydjango/ormpro/uwsgi.pid
deactivate

nginx安装脚本


nginx配置文件
cat ormpro.conf
server {
	listen 80;
        listen 443 ssl http2;
        ssl_certificate /usr/local/nginx/conf/ssl/_ropon.top.crt;
        ssl_certificate_key /usr/local/nginx/conf/ssl/_ropon.top.key;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 10m;
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_buffer_size 1400;
        add_header Strict-Transport-Security max-age=15768000;
        ssl_stapling on;
        ssl_stapling_verify on;
   	server_name django.ropon.top;
	if ($ssl_protocol = "") { return 301 https://$server_name$request_uri; }
   	location / {
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:8000;
	}
	location /static {
		alias /home/wwwroot/mydjango/ormpro/static;
	}
}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!