Ubuntu部署Django项目

来源:互联网 发布:写算法 编辑:程序博客网 时间:2024/05/18 01:09

Ubuntu部署Django项目

1. 安装Ubuntu系统或CentOS系统

2. pip freeze >requirements.txt

3. pip install -r requirements.txt

4. 相关环境搭建

5. sudo passwd 输入root相关密码

6. 输入su回车  进入超级管理员

sudo apt-get install python (如果有python环境可以不装)

sudo apt-get install python-dev

sudo apt-get install python-pip

sudo apt-get install libxml*

sudo apt-get install net-tools

sudo apt-get install lsof

------安装 数据库---------

apt-get install mysql-server

apt-get install mysql-client

apt-get install libmysqlclient-dev

------更新pip版本---------

pip install --upgrade pip

------安装SSH---------

sudo apt-get install openssh-server

------安装 Nginx---------

sudo apt-get install nginx

------安装 uwsgi---------

sudo pip install uwsgi

 

测试uwsgi

def application(env, start_response):    start_response('200 OK', [('Content-Type','text/html')])    return “HelloWorld” 


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

 

show variables like 'character_set_database';

create database <数据库名> character set utf8;

-------修改django项目的配置文件-----------

DEBUG =FalseALLOWED_HOSTS = ["*"]


-------在linux环境中,安装必要的文件-----------

例如django,captchar

注意生成数据库迁移文件

 

 

-------静态文件-----------

说明:STATIC_ROOT必须设置,而且和STATICFILES_DIRS

不能共存

 

在django的setting文件中,添加下面一行内容:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后运行(收集admin后台的静态文件,如果不收集,将来访问linux的blog的admin将没有样式):

python manage.py collectstatic

 

将项目上传到Linux系统中

 

 

为你的项目创建一个uwsgi的配置文件,后缀为ini

[uwsgi]# Django-related settingssocket = 127.0.0.1:9001# 项目绝对路径chdir           = /home/project/LoginDemo# Django的wsgi文件相对路径wsgi-file       =LoginDemo/wsgi.py# process-related settings# mastermaster          = True# 最大进程数processes       = 4# 线程数threads         = 2#设置此参数,有一个主进程master=True#守护进程的方式运行,log日志存在此log文件里deamonize=/var/log/uwsgi/djangoProject.log#主进程id写入文件里pidfile= /var/log/nginx/uwsgi.pid# ... with appropriate permissions - may be needed# chmod-socket    = 664#退出时,清理环境vacuum          = Truereload-mercy    = 10max-requests    = 5000limit-as        = 512buffer-size     = 30000 

----进入etc/nginx/conf.d文件夹下创建django_nginx.conf文件------

 server { listen 8000; server_name 127.0.0.1 access_log /var/log/nginx/logindemo_access.log; error_log /var/log/nginx/logindemo_error.log;charset utf-8; client_max_body_size 75M; root /home/project/LoginDemo location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9001; uwsgi_read_timeout 2; } location /static/ { expires 30d; autoindex on; add_header Cache-Control private; alias /home/project/LoginDemo/static/; } }


----通过uwsgi运行django项目------

重启nginx: sudo service nginx restart

uwsgi --ini 配置文件的路径(myblog_uwsgi.ini)

 

再次通过浏览器访问项目.出现界面则表示部署正确

 

 

----------------问题-------------------

1,uwsgi遇到ImportError: No module named django.core.wsgi问题

通过在wsgi.py里面打印sys.path发现是因为path里面并没有包含该安装的site-packages的路径,因此在wsgi.py添加如下语句

sys.path.append('/usr/lib/python2.7/site-packages')  

sys.path.append('/usr/lib64/python2.7/site-packages')

 

 

netstat -ap | grep 8080  查看8080端口是谁在使用

kill -9 pid号  强制关闭占用端口的程序