django项目的部署

django项目部署到云服务器:   

0.通过xshell连接远程主机服务器ip

1.使用xftp将项目发送到服务器端(也可以使用git)

a.路径推荐为/var/project/(项目名)

2.给服务器安装必要的环境

a.升级,更新apt-get
  apt-get update
  apt-get upgrade
b.安装mysql数据库
  apt-get install mysql-server-5.7
  (为root设置密码)
c.安装pip3(默认未安装)
  apt install python3-pip
d.安装虚拟环境
  pip install --upgrade pip (如版本低,先升级pip)
  pip install virtualenv (如报这个错 Consider using the '--user' option or check the permissions在virtualenv前加上--user即可)
e.安装虚拟环境管理软件
  easy_install virtualenvwrapper
  注:安装上它才可以使用workon, mkvirtualenv等命令
f.配置虚拟环境
  创建虚拟环境管理目录 mkdir $HOME/.local/virtualenvs
  配置环境变量:
  在~/.bashrc中添加行

export VIRTUALENV_USE_DISTRIBUTE=1 #总是使用 pip/distribute 
export WORKON_HOME=$HOME/.local/virtualenvs #所有虚拟环境存储的目录 
if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then 
source $HOME/.local/bin/virtualenvwrapper.sh 
else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then 
source /usr/local/bin/virtualenvwrapper.sh 
fi 
fi 
export PIP_VIRTUALENV_BASE=$WORKON_HOME 
export PIP_RESPECT_VIRTUALENV=true

刷新环境变量: source ~/.bashrc

创建python3.5的虚拟环境:mkvirtualenv -p /usr/bin/python3.5 虚拟环境的名称

ps: 使用虚拟环境: workon 虚拟环境名
退出虚拟环境 : deactivate
删除虚拟环境:rmvirtualenv 虚拟环境名称

3. 下载安装python中的第三方库

根据每个人的项目库的需求来安装,常用库有:
a. requests
b. Pillow
c. pymysql
d. uwsgi

4. 安装nginx

a. 下载认证密钥
  wget http://nginx.org/keys/nginx_signing.key (下载到当前目录下)
b. 配置该密钥
  sudo apt-key add nginx_signing.key
c. 切换到apt的源列表
  vim /etc/apt/sources.list
加上源:
  deb http://nginx.org/packages/ubuntu/ codename nginx
  deb-src http://nginx.org/packages/ubuntu/ codename nginx
注:
将codename替换成当前linux的版本号, Ubuntu16.04 对应为: xenial, 保存退出
其它的linux版本可以在网上查
d. 更新并安装
  apt-get update
  apt-get install nginx
e.查看nginx服务是否开启
  ps -ef | grep nginx 通过存在的进程来筛选查看nginx的状态

5. 配置django和uwsgi

a. 在项目目录下建uwsgi.ini文件, 并配置

[uwsgi]
# 使用nginx连接时 使用
#socket=127.0.0.1:8010
# 直接作为web服务器使用
http=127.0.0.1:8010
# 配置工程目录
chdir=/home/rock/Python1801/DjangoAXF/AXF
# 配置项目的wsgi目录。相对于工程目录
wsgi-file=AXF/wsgi.py

#配置进程,线程信息
processes=4
threads=2
enable-threads=True
master=True
pidfile=uwsgi.pid 
daemonize=uwsgi.log

b. 在django项目setting中将DEBUG置为False
c. database配置为服务器上的mysql
d. 建库,迁移表,插入数据(如果使用了数据库缓存,python manage.py createcachetable)
e. 一切就绪后执行 uwsgi --ini uwsgi.ini

6.启动nginx

以配置文件的方式启动
1.测试:nginx -t -c 配置文件路径(见到successful便成功)
2.启动:启动前确保之前的已经彻底关闭(nginx -s stop)
nginx -c 配置文件路径

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

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

    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;

    keepalive_timeout  65;

    #gzip  on;
  
   include /etc/nginx/conf.d/*.conf;

   server {
     listen       80;
     server_name  localhost;

     charset utf-8;
     root   /var/www/learn;
     index  hello.html;

  
     location / {
     include /etc/nginx/uwsgi_params;
     uwsgi_pass localhost:8000;
     }
     location /static{
     alias  /var/project1804/game/2048game/;
     }
  }
}
       

 

7.输入公网ip,访问项目测试!



















posted @ 2018-10-01 14:23  Sakura_L  阅读(2776)  评论(0编辑  收藏  举报