uwsgi + nginx + django 项目部署

  • 作者:ykk
  • 分类: Django
  • 发表时间:2020-05-27 13:47:40
  • 阅读量:(462)

部署环境


[root@localhost ~]# rpm -q centos-release
centos-release-7-8.2003.0.el7.centos.x86_64

#Django版本 
Django   ==   2.2.10

#Python版本
Python 3.7.3 (default, May  2 2020, 20:46:31) 

部署开始

先建立好虚拟环境,将项目文件上传至服务器 并生成requirements.txt,进入文件路径并执行


Django==2.2.10
django-ckeditor==5.9.0
django-js-asset==1.2.2
Pillow==6.2.1
pytz==2019.3

pip install -r requirements.txt

安装完成后,下面进行sqllite升级。


wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
tar zxvf sqlite-autoconf-3290000.tar.gz 
cd sqlite-autoconf-3290000
./configure --prefix=/usr/local
make && make install

查看当前系统 sqlite3 --version
[root@tomcatii ~]# /usr/bin/sqlite3 --version #系统自带旧的
[root@tomcatii ~]# /usr/local/bin/sqlite3 --version #新安装的

# 替换系统低版本 sqlite3
mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3

#加入系统默认库
echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
sqlite3 -version

安装uwsgi


pip3 install uwsgi

编辑以下文件test.py


# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

在文件目录下运行


uwsgi --http :8000 --wsgi-file test.py

浏览器访问8000端口

返回"Hello World"

说明uwsgi配置成功

在项目wsgi.py文件中配置以下信息


import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mainproject.settings')#注意项目名称

application = get_wsgi_application()

创建uwsgi.ini 配置文件


[uwsgi]
#通过Ngnix访问需要配置socket
socket = 172.31.90.99:8001

#使用http ip需要填写服务器内网地址
#http =172.31.90.99:8001  

 #指定项目的application
module=mainproject.wsgi:application  

#启动主程序
master = True

#最大请求数
max-requests=5000

#请求超时时间
harakiri  = 60

#用户组root
#uid=1000
#gid=2000

#启动进程文件
pidfile =/root/mainproject/conf/blog.pid

#服务器关闭或退出时,回收pid
vacuum=True
#plugin=python

#虚拟环境位置
virtualenv=/root/myblog_env  

#项目目录
chdir = /root/mainproject  

#启动4个工作进程,生成指定数目的worker/进程
processes = 4  

#线程为2
#threads =2  

#日志文件位置
daemonize=/root/mainproject/conf/app.log  



在ini文件目录下运行


uwsgi --ini blogwsgi.ini

uwsgi相关命令


#查看uwsgi 进程
ps aux|grep uwsgi

#杀掉uwsgi 进程
killall -s INT /usr/local/bin/uwsgi

 

下面进行nginx安装


#安装 nginx
yum install nginx

#查看  nginx版本
rpm -qa | grep nginx

#找到nginx.conf 文件
/etc/nginx

#查看nginx 状态
nginx -t
systemctl status nginx

#查看nginx相关进程
ps -ef | grep nginx

#杀掉nginx相关进程
killall -9 nginx

#重启nginx
service nginx restart

#nginx日志文件所在路径
 /var/log/nginx

#相关软链接命令
sudo ln -s /root/mainproject/conf/nignx.conf /etc/nginx/conf.d/

sudo ln -s /root/mainproject/conf/nignx.conf /etc/nginx/default.d/

sudo ln -s /root/mainproject/conf/nignx.conf /etc/nginx

nginx.conf


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;  #以最高权限root用户运行
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
 #   include /etc/nginx/conf.d/*.conf;

   server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name ykk.one; # substitute your machine's IP address or FQDN 远端的域名
charset utf-8;

# max upload size
client_max_body_size 75M; # adjust to taste

# Django media
location /media/ {
alias /root/mainproject/media/; # 指向django的media目录
}

location /static/ {
alias /root/mainproject/static/; # 指向django的static目录

}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 172.31.90.99:8001; #uwsgi端口
include /root/mainproject/uwsgi_params; # the uwsgi_params file you installed
}

}


# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

正式启动前 需要收集静态文件


进入虚拟环境 收集静态文件
python manage.py collectstatic

 

上一篇: Centos升级python3.7

下一篇: CSS简单实现吃豆人动画

评论 列表: