如何将本地django项目部署到服务器,Django本地项目部署到云服务器

开发环境

开发语言: Python

后台框架:Django

web 服务器:nginx

wsgi 服务器:uwsgi

云服务器系统:Ubuntu

查看本地项目 Django 和 Python 版本:

(可以直接安装 Django 最新版本就可以了,Python 版本不能低于 3.4)

1

2

3

4

5

6

7

8

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/closetUsers# python3

Python 3.5.2 (default, Nov 12 2018, 13:43:14)

[GCC 5.4.0 20160609] on linux

Type "help", "copyright", "credits" or "license" for more information.

>> import django

>> django.VERSION

(2, 1, 4, 'final', 0)

>>

1. 预备安装

1.1 安装数据库

安装 mysql

1.2 安装 Django

Django 初步使用移步 Django 系列教程:

1.3 安装 uwsgi

1.3.1 uwsgi 安装1

root@iZwz969jm0y04abuhgx4byZ:~# python3 -m pip install uwsgi

Successfully installed uwsgi-2.0.17

1.3.2 uwsgi 测试

测试 uwsgi 是否正常:

新建 test.py 文件,内容如下:

1

2

3

def (env, start_response):

start_response('200 OK', [('Content-Type','text/html')])

return "cloestUsers World"

然后在终端运行:

1

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

1.3.3 uwsgi 关闭1

2

3

4

5

6

7

8

9

10

11

12

13

killall -9 uwsgi # 强行关闭

#关闭某一个进程

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/closetUsers# netstat -ntpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 22256/java

tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 22256/java

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 18840/mysqld

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3044/nginx.conf

tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 22256/java

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 11022/sshd

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/closetUsers# kill -9 22256

#查看哪个要关闭的进程的pid,使用kill -9

1.4 安装 nginx

1.4.1 安装 Nginx

安装命令如下:

1

2

3

4

5

6

7

8

cd ~

wget http://nginx.org/download/nginx-1.11.0.tar.gz

tar xf nginx-1.11.0.tar.gz

cd nginx-1.11.0

./configure --prefix=/usr/local/nginx-1.11.0

--with-http_stub_status_module

--with-http_gzip_static_module

make && make install

你可以阅读 Nginx 安装配置 了解更多内容。

1.4.2 测试 nginx

查看 nginx 是否安装成功,查看 nginx 的版本:

1

2

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/closetUsers# nginx -v

nginx version: nginx/1.10.3 (Ubuntu)

在/home/projects 目录下新建一个 test 目录,在里面添加一个简单的 html 文件。然后在 nginx.conf 中简单配置:

1

2

3

4

5

6

7

8

events{}

http{

server{

server_name www.sukamay.xyz; # 域名或者ip地址

root /home/projects/test;

index index.html;

}

}

启动该 nginx:(启动之前,要将/usr/local/nginx/mime.types 文件复制到该 nginx.conf 所在目录,并且必须使用绝对路径)

1

nginx -c /home/projects/test/nginx.conf

如果端口被占用,则先释放该端口,则使用lsof -i :8082查看占用端口进程的 pid,并使用kill -9 [pid]关闭相应的进程。

若想要关闭所有 nginx 打开的进程,可使用:

1

killall -9 nginx

1.4.3 关闭 nginx1

2

nginx -s stop

#or /etc/init.d/nginx stop

2. 在线项目部署

部署本地 Django 项目到服务器,结合 uwsgi+Django+nginx。

2.1 项目代码上传到服务器

clone 本地项目代码到服务器/通过 ftp 复制项目到服务器

我使用的是 scp

1

2

xxxdeMacBook-Pro:~ xxx$ scp -r /Users/xxx/Desktop/closet/closetUsers root@120.76.62.132:/home/projects/closetTest1.0

#pattern: scp root@:

在我的项目目录/home/projects 里创建一个 closet 目录,并在里面创建一个 conf 目录,用来存放配置文件,这样分离开来,方便更新维护项目代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

root@iZwz969jm0y04abuhgx4byZ:/home/projects# tree -L 3

.

├── closet

│   ├── closetUsers

│   │   ├── closetUsers

│   │   ├── jsonTest

│   │   ├── manage.py

│   │   ├── __pycache__

│   │   ├── static

│   │   ├── users

│   └── conf

│   ├── mime.types #copy from /usr/local/nginx/conf

│   ├── nginx.conf #configure file of nginx

│   ├── uwsgi.ini #configure file of uwsgi

│   └── uwsgi.log

└── closetTest1.0

└── closetUsers

├── closetUsers

├── manage.py

└── users

13 directories, 10 files

2.2 修改项目代码

2.2.1 修改 settings.py:1

2

3

4

DEBUG = False # Django是否加载静态资源

ALLOWED_HOSTS = ['localhost','127.0.0.1','120.76.62.132'] # 允许访问的主机地址

STATIC_ROOT = os.path.join(BASE_DIR, "static/") # 收集Django的静态文件到同一个static中

2.2.2 创建 uwsgi.ini 和 uwsgi.log

uwsgi.ini 不变,依旧为:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/conf# vim uwsgi.ini

#closet uwsgi.ini file

[uwsgi]

socket = 127.0.0.1:8002

#http = :8002 如果不通过nginx可以直接用http,但要通过web服务器就必须要用socket

#http = 127.0.0.1:8002

#the base directory (full path)

chdir = /home/projects/closet/closetUsers

#Django's wsgi file

module = closetUsers.wsgi

#process-related settings

#master

master = true

#maximum number of worker processes

processes = 4

#clear environment on exit

vacuum = true

processes = 4

"uwsgi.ini" 29L, 549C 1,0-1 Top

2.2.3 创建 nginx.conf1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

root@iZwz969jm0y04abuhgx4byZ:/home/projects/closet/conf# vim nginx.conf

http{

include mime.types;

server{

listen 80;

# server_name www.sukamay.xyz;

server_name 120.76.62.132;

index index.html ;

root /home/projects/closet/closetUsers;

charset utf-8;

location /static {

alias /home/projects/closet/closetUsers/static; # your Django project's static files - amend as required

}

# Finally, send all non-media requests to the Django server.

location / {

include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed

uwsgi_pass 127.0.0.1:8002;

}

}

}

将 /usr/local/nginx/conf/mime.types文件 copy 到 conf 下

2.3 创建数据库、收集静态文件1

2

python manage.py migrate

python manage.py collectstatic

2.4 启动服务

启动 nginx

1

nginx -c /home/projects/closet/conf/nginx.conf

后台启动 uwsgi

1

uwsgi /home/projects/closet/conf/uwsgi.ini -d /home/projects/closet/conf/uwsgi.log

3. error summary

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值