发布django 程序

1、配置需求环境

pip freeze > requirements.txt 在开发环境将工程依赖的包导出。
pip install virtualenv 
pip install virtualenvwrapper
编辑~/.bashrc
  export WORKON_HOME=$HOME/.virtualenvs
  source /usr/bin/virtualenvwrapper.sh
执行 source ~/.bashrc,后workon命令就可以用了,并且通过mkvirtualenv创建的都可以通过workon命令查看
mkvirtualenv testenv创建一个虚拟环境
pip install -r requirements.txt 就会从文件里边读取依赖并且安装。
  如果提示mysql_config not found报错,则执行yum -y install mysql-devel
  如果提示gcc错误,则安装 yum -y install gcc python-devel
pip install uwsgi 安装uwsgi
  uwsgi --http :8000 --module muxue.wsgi 测试uwsgi 安装是否成功
  muxue.wsgi 指的是django应用的根目录下的muxue文件夹下的wsgi.py文件,这样就可以直接把django项目启动起来

2、添加nginx配置文件

upstream django {
        server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
server {
        listen      80;
        server_name 192.168.201.131; # substitute your machine's IP address or FQDN
        charset     utf-8;

        client_max_body_size 75M;   # adjust to taste
        location /media  {
            alias /opt/django_muxue/media;  # 指向django的media目录
        }
        location /static {
            alias /opt/django_muxue/static; # 指向django的static目录
        }
        location / {
            uwsgi_pass  django;
            include     uwsgi_params; # the uwsgi_params file you installed
        }
}

3、拉取所有需要的static file 到同一个目录

  在django的setting文件中,添加下面一行内容:
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")
    运行命令 python manage.py collectstatic

4、配置一个uwsgi .ini配置文件,然后用配置文件来启动uwsgi

[uwsgi]
chdir           = /opt/django_muxue/
module          = muxue.wsgi
master          = true
processes       = 10
socket          = 127.0.0.1:8000
vacuum          = true
virtualenv = /root/.virtualenvs/mxonline
  启动uwsgi,通过配置文件的方式  uwsgi -i uwsgi.ini
  重启uwsgi,pkill -f uwsgi
 


作者:猪肉楠
链接:https://www.jianshu.com/p/e572f82e9db6
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
原文地址:https://www.cnblogs.com/ExMan/p/9364960.html