Django项目部署上线

Django项目部署
一、编译安装sqlite3数据库
1、yum update && yum install gcc
2、wget --no-check-certificate https://www.sqlite.org/2017/sqlite-autoconf-3170000.tar.gz --no-check-certificate
3、 ./configure --prefix=/usr/local/sqlite3 --disable-static --enable-fts5 --enable-json1 CFLAGS="-g -O2 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_RTREE=1"
4、make && make install

二、编译安装python3.6(关联sqlite3)
1、wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
2、yum install patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
3、tar xf Python-3.6.2.tgz && cd Python-3.6.2
# LD_RUN_PATH=/usr/local/sqlite3/lib ./configure LDFLAGS="-L/usr/local/sqlite3/lib" CPPFLAGS="-I /usr/local/sqlite3/include"
# LD_RUN_PATH=/usr/local/sqlite3/lib make
# LD_RUN_PATH=/usr/local/sqlite3/lib make install
# python3
>>> import sqlite3 #验证sqlite是否正确安装

三、安装虚拟环境
1、pip3 install virtualenv
2、ln -s /usr/local/bin/virtualenv /usr/bin/virtualenv
3、mkdir -p /data/env -----存放env
mkdir -p /data/wwwroot -------存放网站文件
4、cd /data/env && virtualenv --python=/usr/bin/python3 pyweb (关联python3)
5、cd /data/env/pyweb/bin && source activate
6、pip3 install django && pip3 install uwsgi (uwsgi要安装两次,先在系统里安装一次,然后进入对应的虚拟环境安装一次)

四、安装django(系统和虚拟环境下安装)
1、pip install --upgrade pip
2、pip3 install django
3、cd /data/wwwroot/ && django-admin startproject PD
4、python3 manage.py runserver 0.0.0.0:8000
5、验证:http://192.168.243.130:8000/

五、安装uwsgi(支持多线程的web服务应用,通过他来启动django)
1、wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
2、tar xf uwsgi-latest.tar.gz && cd uwsgi-2.0.20/
python3 uwsgiconfig.py --build
python3 setup.py install
3、ln -s /usr/local/bin/uwsgi /usr/bin/uwsgi
4、验证,使用uwsgi启动django:一定要在这个项目目录中
uwsgi --http 192.168.243.130:80 --file PD/wsgi.py --static-map=/static=static
访问项目:http://192.168.243.130

六、配置启动uwsgi启动文件
cd /data/wwwroot/PD && vim uwsgi.ini
[uwsgi]
socket=0.0.0.0:8888 # 指定socket监听的地址和端口
chdir=/data/wwwroot/PD/ # 项目路径
wsgi-file=/data/wwwroot/PD/PD/wsgi.py # django的wsgi文件路径
processes=4
threads=2
master=True
pidfile=/data/wwwroot/PD/uwsgi.pid # uwsgi启动进程id存放路径
daemonize=/data/wwwroot/PD/uwsgi.log # 日志存放路径
vacuum=true
buffer-size=32768
#home=/data/env/ #注释为默认使用root权限
module=PD.wsgi # 使用mmcsite.wsgi模块
chmod-socket=666 # socket权限
enable-threads=true # 允许用内嵌的语言启动线程,这将允许你在app程序中产生一个子线程

七、安装nginx
1、vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2、yum install nginx
3、vim ngin.conf
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log;
charset utf-8;
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 4000;
gzip_types text/plain application/json application/javascript application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass 127.0.0.1:8888;
uwsgi_ignore_client_abort on;
}
location /static {
alias /data/wwwroot/PD/static/;
}

八、启动项目
1、systemctl restart nginx # 开启nginx
2、cd /data/wwwroot/PD/ && uwsgi --ini uwsgi.ini # 启动uwsgi的django项目(停止uwsgi uwsgi --stop uwsgi.ini )
3、访问http://192.168.243.130

posted @ 2022-05-13 11:41  Me-lihu  阅读(146)  评论(0编辑  收藏  举报