1.下载与项目对应的django版本
pip3 install django==1.11.16 -i https://pypi.douban.com/simple/
2.用django内置的wsgi模块测试项目是否可以正常运行
python manage.py runserver 0.0.0.0:8080
3.下载uwsgi--此文所用版本:2.0.17.1
pip install uwsgi
4.测试uwsgi是否可以正常使用
1>测试文件test.py
def application(env, start_response): start_response('200 OK',[('Content-Type', 'text/html')]) return [b'Hello world'] # Python3
2>测试语句
uwsgi --http 0.0.0.0:8080 --wsgi-file test.py
3>选做:可以直接命令测试django项目是否可以用uwsgi运行
切换到django的项目根目录中
[root@VM_0_3_centos day18_crm]# ls crm day18_crm manage.py nohup.out rbac README3 script static templates [root@VM_0_3_centos day18_crm]# pwd /data/day18_crm
注意:运行之前必须收集django项目的静态文件 --如果你的项目是每个APP下都有static文件夹的话
python manage.py collectstatic
uwsgi --http 0.0.0.0:8080 --file day18_crm/wsgi.py --static-map=/static=static
5.在django项目根目录中创建uwsgi.ini,将uwsgi参数写到配置文件中,后续与nginx配合
1>文件内容如下
[uwsgi]
# uwsgi +django直接用的时候web端口号 #http= :9000 #niginx将动态请求代理回此端口号 socket=0.0.0.0:8080 #项目目录 chdir= /data/day18_crm #wsgi.py wsgi-file=day18_crm/wsgi.py processes=4 #进程 threads=2 #线程 stats =:9191 #服务停止的时候清除 socket环境 vacuum=true
6.用uwsgi启动django
1>正常启动
uwsgi --ini uwsgi.ini
2>后台挂起式启动
nohup uwsgi uwsgi.ini &
7.下载安装nginx--此文所用版本:nginx version: nginx/1.12.2
1>下载
yum -y install nginx
2>测试--查看xx.xx.xx.xx:80 nginx是否正常
systemctl start nginx.service
8.nginx配置
1>在django项目根目录创建uwsgi_params(注意文件名字不要错),改成777的权限,文件内容如下
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
2>切换到nginx服务
cd /etc/nginx/conf.d/
3>创建配置文件--crm_nginx.conf
upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 0.0.0.0:8080; # for a web port socket (we'll use this first) 与uwsgi.ini的socket ip保持一致 } # configuration of the server server { # the port your site will be served on 服务访问的时候的IP xx.xx.xx.xx:8000/crm/login listen 8000; # the domain name it will serve for server_name 0.0.0.0; # substitute your machine's IP address or FQDN 服务器IP
charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media #location /media { # alias /path/to/your/mysite/media; # your Django project's media files - amend as required # } location /static { alias /data/day18_crm/static; # your Django project's static files - amend as required 收集的静态文件目录 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; #代理块 include /data/day18_crm/uwsgi_params; # the uwsgi_params file you installed 与nginx通信用的,在django的根目录中存在的文件 } }
4>查看django项目根目录文件
[root@VM_0_3_centos day18_crm]# ls crm day18_crm manage.py nohup.out rbac README3 script static templates uwsgi.ini uwsgi_params (俩个重要的文件)
9.启动nginx
1>启动
systemctl start nginx.service
2>查看现在需要的端口是否都启动
netstat -tunlp
tcp 0 0 0.0.0.0:9191 0.0.0.0:* LISTEN 20406/uwsgi tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 20406/uwsgi #动态文件交给uwsgi处理 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12460/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 16787/sshd tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 12460/nginx: master #niginx处理静态文件的端口 tcp6 0 0 :::3306 :::* LISTEN 14978/mysqld tcp6 0 0 :::80 :::* LISTEN 12460/nginx: master udp 0 0 0.0.0.0:68 0.0.0.0:* 880/dhclient udp 0 0 172.27.0.3:123 0.0.0.0:* 550/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 550/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 550/ntpd udp 0 0 0.0.0.0:44221 0.0.0.0:* 22821/ntpdate udp 0 0 0.0.0.0:59948 0.0.0.0:* 880/dhclient udp6 0 0 :::41081 :::* 880/dhclient udp6 0 0 :::123 :::* 550/ntpd
3>再收集一遍静态文件
注意:一定要在django的settings.py中需要配置static根目录
STATIC_ROOT = os.path.join(BASE_DIR,'static')
python manage.py collectstatic