Django项目部署

Django项目部署到服务器的常用命令及步骤

Posted by aptx1231 on September 25, 2020
字数统计:2538 | 阅读时长:8min

常用命令

django

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
python manage.py inspectdb

django-admin startproject ***

python manage.py startapp ***

python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

python manage.py runserver 

python manage.py runserver 0.0.0.0:8000

mysql

1
2
3
4
5
6
7
8
9
# 登录mysql
mysql -u root -p
# 导出sql文件
mysqldump -u root -p buaa_activity > buaa_activity.sql
# 导入sql文件
source ..
# 重启mysql
service mysql restart
MySQL8.0.19root用户密码root996258

scp

1
2
# 上传文件到服务器
scp .\UniActive-Server.jar root@114.115.134.188:~

ssh

1
2
登录命令: ssh root@114.115.134.188
密码:buaa202004

jar

1
2
3
4
5
6
# 查找进程
ps -ef|grep java
# 关闭进程
kill -9 id
# 后台执行
nohup java -jar UniActive-Server.jar &

uwsgi

1
2
3
4
5
6
# 关闭进程
pkill -f uwsgi -9
# 查找进程
ps aux | grep uwsgi
# 启动uwsgi
uwsgi -i uwsgi.ini

nginx

1
2
3
4
5
6
# 重启
service nginx restart
# 启动
service nginx start
# 重新加载
nginx -s reload

django 部署到服务器

#### 手机与本地django服务器连接 ####

1
python manage.py runserver 0.0.0.0:8000

手机代码用的ip地址选择本机cmd输入ipconfig显示的地址192.168.0.*:8000

这个地址需要加入django,settings的allow_host

uwsgi

pip3 install uwsgi

在django项目根目录新建uwsgi.ini

1
2
3
4
5
6
7
8
[uwsgi]
# django根目录
chdir    = /root/vuedjango
module   = vuedjango.wsgi
master   = true
processes = 10
socket   = 127.0.0.1:8080
vacuum   = true

nginx

apt-get install nginx

/etc/nginx/nginx.conf 主配置文件,修改第一行为root,否则访问静态资源会403

1
user root;

新建 /etc/nginx/sites-available/mysite.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
upstream django {
    server 127.0.0.1:8080; # for a web port socket (we'll use this first)
}

server{
    listen 80;
    server_name 114.115.134.188;
    charset utf-8;
    client_max_body_size 75M;  #上传文件大小限制

    # 网站静态文件所在目录
    location /static{
        alias /root/vuedjango/vue-xuAdmin/dist/static;
    }

    # 动态文件交给uwsgi处理
    location / {
        uwsgi_pass 127.0.0.1:8080;
        include /etc/nginx/uwsgi_params;
    }
}

将此文件再sites-enabled中创建一个软连接,表示此配置文件已经启用:

1
ln -s /etc/nginx/sites-available/mysite.conf   /etc/nginx/sites-enabled/mysite.conf

django settings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ALLOWED_HOSTS = ['114.115.134.188']   #服务器ip地址

MIDDLEWARE = [
    #'django.middleware.csrf.CsrfViewMiddleware',  #(注释掉,跨域错误)
]

TEMPLATES = [
    {
        'DIRS': ['vue-xuAdmin/dist'],   # vue编译之后的目录
        ....
    },
]

#静态资源目录  vue编译出的dist目录中静态资源必须在static目录下,不可改名字
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "vue-xuAdmin/dist/static")
]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

参考:https://cloud.tencent.com/developer/article/1514726

Vue 前端部署到服务器

/etc/nginx/sites-available/mysite.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server{
    listen 8000;
    server_name 112.124.28.130;
    charset utf-8;
    client_max_body_size 75M;  #上传文件大小限制
    location / {
        root   /root/KGraph/dist;
        index  index.html index.htm;
        try_files $uri $uri/ @router;
    }
    location @router {
        rewrite ^.*$ /index.html last;
    }
}

重新加载nginx

1
nginx -s reload

关于域名

想要通过域名访问,那么server_name既要设置ip地址,也要设置域名,例如:

1
2
server_name 112.124.28.130;
server_name aptx1231.xyz;

转载请标明如下内容:

本文作者:姜佳伟

本文链接:https://aptx1231.github.io/2020/09/25/Django%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%83%A8%E7%BD%B2/

Github:aptx1231