部署步骤
- 通过ssh连接你的远程服务器
ssh 登录的用户名 @ 登录的 ip地址
- 检查和更新系统软件
sudo apt update && upgrade
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
- 安装pip3,虚拟环境
配置虚拟环境,
sudo vi ~/.bashrc
末行添加
export WORKON_HOME=$HOME/.virtualenvs export
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
source .bashrc
使虚拟环境生效
- 创建虚拟环境(安装相关的python包)
mkvirtualenv h1_django
进入本地虚拟环境导出pip包
pip freeze > ~/desktop/1.txt
scp
进入主机
pip install -r 1.txt
scp ./1.txt ubuntu@94.191.98.56:~
- 安装Nginx
sudo apt-get install nginx
- 6.上传项目
scp -r /django_prj/ ubuntu@94.191.98.56:~
! 绝对路径
- 7 . 安装 uwsgi
pip install uwsgi
- 8 . 测试uwsgi
uwsgi --http :8000 --module test1.wsgi
- 静态文件配置(可以先跳过,进行下一步配置nginx)
- 配置nginx
- 10.1 在项目目录下面新建一个文件夹conf,用于存放一些我们的配置文件
- 10.2 创建一个配置文件test1_nginx.conf
# 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;
# 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 /home/ubuntu/test1/static/media; # 指向django的media目录
}
location /static {
alias /home/ubuntu/test1/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
}
}
- 10.3 将配置文件加入到nginx的启动配置文件中(当然你也可以把文件直接拷贝进去)
sudo ln -s /home/ubuntu/test1/conf/test1_nginx.conf /etc/nginx/conf.d
https://www.cnblogs.com/shuo1208/p/6702531.html
------nginx下载重装(如果安装错误)
- 10.5 拉取所有需要的static file到同一个目录
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
- file注释掉
运行 python manage.py collectstatic
- 配置uwsgi11.1 新建uwsgi配置文件叫uwsgi.ini文件(我们依旧保存到conf目录下面)
* 新建uwsgi.ini 配置文件, 内容如下:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/test1
# Django's wsgi file
module = test1.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/h1_django
#logto = /tmp/mylog.log
- 注: chdir: 表示需要操作的目录,也就是项目的目录 module: wsgi文件的路径 processes: 进程数 virtualenv:虚拟环境的目录
- uwsgi -i 你的目录/home/ubuntu/test1/conf/uwsgi.ini
如果想后台启动加一个&
- uwsgi -i 你的目录/home/ubuntu/test1/conf/uwsgi.ini &