Django的云服务器部署

这里我采用的云服务器是腾讯云 Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-91-generic x86_64)

 

首先安装Nginx与mysql-server

sudo apt-get install nginx
ps aux|grep nginx  # 看Nginx是否启动
sudo apt-get install mysql-server
ps aux|grep mysql # 看mysql是否启动

 

 mysql -u root -p  进入mysql,用户名root,密码root

 配置mysql: sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 

 

 这里主要更改bin-address为0.0.0.0方便我们在Windows上Navicat连接服务器的mysql并上传数据。

然后重启mysql服务: service mysql restart 

我们授权一个叫Ubuntu(叫什么由你定)的账户,并授予它远程连接的权力,命令如下:     

GRANT ALL PRIVILEGES ON *.* TO 'root'@'118.**.**.54' IDENTIFIED BY 'root' WITH GRANT OPTION;

 

 (需要在mysql下运行)

运行完后紧接着输入,以更新数据库:     FLUSH PRIVILEGES; 刷新权限

然后就可以连接了:

然后新建数据库:

数据库名: mxonline
字符集: utf8 -- UTF-8 Unicode
排序规则:utf8_general_ci

 

 

 

创建python的虚拟环境:

首先更换pip源:

sudo su  # (进入root权限,可使用exit退出)

 

 在主目录下:

mkdir ~/.pip
sudo vim ~/.pip/pip.conf

# vim中
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple #清华源
index-url = https://pypi.douban.com/simple # 豆瓣源

 

 更新pip版本:(可选)

pip install --upgrade pip
# 当你想升级一个包的时候 pip install --upgrade 包名

 

 然后就可以下载两个虚拟环境的包了:

pip install virtualenv
pip install virtualenvwrapper

 

安装完virtualenvwrapper之后,配置使用workon命令

1.创建目录用来存放虚拟环境
    mkdir $HOME/.virtualenvs
2.在~/.bashrc中添加行:
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
3.运行:
    source ~/.bashrc

 

创建虚拟环境: mkvirtualenv mxonline 

导出本地的依赖包:

在本地进入你工作的那个虚拟环境:

pip freeze > requirements.txt

 

然后把这个复制到云服务器上当前项目中的requirements.txt中(当然这步也可以在开发完成后直接打包好)

安装打包好的包:  pip install -r requirements.txt 

安装mysql的开发包时有可能发生错误,先安装一个: sudo apt-get install libmysqlclient-dev ,然后继续安装原来的。

 

安装与配置uwsgi:

pip install uwsgi

 

本地监听:

~/mxonline$ python manage.py runserver 0.0.0.0:8000

 

监听确认无误后,可以用uwsgi的http方式来启动服务:

~$ cd mxonline
~/mxonline$ uwsgi --http :8000 --module MxOnline.wsgi # 用uwsgi在http的8000端口上去启动这个module
# MxOnline.wsgi是在mxonline(django项目文件夹名),的MxOnline里面有一个wsgi.py的配置文件

# 注意不能在mxonline外面一层使用uwsgi --http :8000 --module mxonline.MxOnline.wsgi

 

 

配置Nginx:

vim uc_nginx.conf

# 在vim中写入:
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
listen      80;:w
# the domain name it will serve for
server_name 你的ip地址 ; # 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 你的目录/mxonline/media;  # 指向django的media目录
}

location /static {
    alias 你的目录/mxonline/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}

 

将该配置文件加入到nginx的启动配置文件中

sudo ln -s /home/ubuntu/mxonline/uc_nginx.conf /etc/nginx/conf.d/

 

# 好处是我们可以直接修改uc_nginx.conf即可,不用更改后者
 sudo service nginx restart 重启服务

 


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

vim MxOnline/settings.py

 


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

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

 

并注释掉已有的STATIC_ROOT


运行命令 python manage.py collectstatic   输入yes

 

http并不是一种好的方式,我们通过读配置文件来启动uwsgi(不对外暴露http方式):

创建一个uwsgi.ini文件:

输入:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/ubuntu/mxonline # 你项目的路径
# Django's wsgi file
module          = MxOnline.wsgi
# the virtualenv (full path)

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = 127.0.0.1:8000 # 你之前配置的端口
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
virtualenv = /home/ubuntu/.virtualenvs/mxonline # 你的虚拟环境路径

logto = /tmp/mylog.log # 打印到日志文件去了

 

保存完成后用uwsgi来启动它:

~/mxonline/conf$ uwsgi -i uwsgi.ini

 

posted @ 2018-04-08 19:24  chen狗蛋儿  阅读(267)  评论(0)    收藏  举报