300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > docker + nginx + uwsgi + ubuntu部署django项目

docker + nginx + uwsgi + ubuntu部署django项目

时间:2018-08-20 03:43:50

相关推荐

docker + nginx + uwsgi + ubuntu部署django项目

前提:你项目部署的服务器端口是开启的,在外部是可以访问到的。这里不懂就百度一下。并且在服务器上已经安装了docker。

一、拉取镜像

1.1 拉取python镜像

在这里我们拉取的是python的镜像,这样方便我们项目中库包的下载,我们就不用自己取安装pip。python中有自带的pip。

sudo docker pull python

1.2 制作python容器

我们用python镜像制作python容器,hengda是我们的项目名

sudo docker run -it --name hengda -v /home/ubuntu/hengda:/home/hengda -p 9100:80 python /bin/bash

在这里--name hengda是容器的名字;-v /home/ubuntu/hengda:/home/hengda是将服务器上的项目挂载到容器中;-p 9100:80是配置端口,9100是服务器的端口,80是容器的端口,

将容器的80端口映射到服务器的9100端口。

回车之后就进入到容器中了。

二、vim、nginx、uwsgi下载

我们按照上面的操作就会进入docker的hengda容器中,这是一个微型的服务器,是一个沙箱机制,完全独立的系统,这样就不会与本机的环境有冲突,

而且也方便管理。

2.1 vim下载

# 更新源apt-get update# 下载vim编辑器apt-get install vim

2.2 nginx下载

apt-get install nginx

2.3 uwsgi下载

pip install uwsgi

三、配置nginx和uwsgi

3.1 下载项目所需的库包

cd home/hengda # 进入项目的根目录

进入项目的根目录下,我们可以看到requirement.txt,这个文件是集中放库包版本的文件

pip install -r requirements.txt -i https://mirrors./pypi/web/simple

我们进入项目的根目录下运行项目试试能不能行:python manage.py runserver

可能会出现这种情况,解决方法为:/qq_45758854/article/details/115874528

下载完之后我们再运行项目发现可以了。

3.2 项目中settings中需要的设置

DEBUG = TrueALLOWED_HOSTS = ['*'] #自己设置可以访问的域名,‘*’代表所有都可以访问# 改为DEBUG = FalseALLOWED_HOSTS = ['*'] #自己设置可以访问的域名,‘*’代表所有都可以访问

#在django的setting文件中,添加下面一行内容:STATIC_ROOT = os.path.join(BASE_DIR, "collected_static/")#运行命令python manage.py collectstatic

3.2 配置uwsgi

在项目的根目录下创建uwsgi.ini,在uwsgi配置如下信息

[uwsgi]socket = 0.0.0.0:8000chdir = /home/hengdamodule= hengda.wsgi:applicationmaster= trueprocesses = 4 vacuum= truebuffer-size=65535# 每个进程最大的请求数max_requests = 1000# 运行的日志,通常放在 uwsgi_config 下daemonize = /home/hengda/uwsgi_config/run.log# 指定pid文件,用于重启和停止,通常放在 uwsgi_config 下pidfile = /home/hengda/uwsgi_config/uwsgi.pid# 启用线程enable-threads = true

这个配置,其实就相当于在上一小节中通过 wsgi 命令,后面跟一堆参数的方式,给文件化了。

socket: 指定项目执行的端口号。

chdir: 指定项目的目录。

module: module = hello.wsgi 可以这么来理解。对于 uwsgi.ini 文件而言,与它同级目录有一个 myweb 目录,这个目录下有一个 wsgi.py 文件。

其它几个参数,可以参考上一小节中参数的介绍。

接下来,通过 uwsgi 命令读取 uwsgi.ini 文件启动项目

uwsgi --ini uwsgi.ini

运行结果:

setup@labideas-data:~/myweb$ uwsgi --ini uwsgi.ini[uWSGI] getting INI configuration from uwsgi.ini*** Starting uWSGI 2.0.17 (64bit) on [Tue Mar 20 11:11:30 ] ***compiled with version: 5.4.0 0609 on 19 March 09:13:12os: Linux-4.4.0-105-generic #128-Ubuntu SMP Thu Dec 14 12:42:11 UTC nodename: labideas-datamachine: x86_64clock source: unixdetected number of CPU cores: 4current working directory: /home/setup/hellodetected binary path: /usr/local/bin/uwsgi!!! no internal routing support, rebuild with pcre support !!!chdir() to /home/setup/helloyour processes number limit is 64049your memory page size is 4096 bytesdetected max file descriptor number: 65535lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with --thunder-lock)uwsgi socket 0 bound to TCP address :8888 fd 3Python version: 3.5.2 (default, Nov 23 , 16:37:01) [GCC 5.4.0 0609]*** Python threads support is disabled. You can enable it with --enable-threads ***Python main interpreter initialized at 0x1f73aa0your server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 364520 bytes (355 KB) for 4 cores*** Operational MODE: preforking ***WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f73aa0 pid: 7097 (default app)*** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 7097)spawned uWSGI worker 1 (pid: 7099, cores: 1)spawned uWSGI worker 2 (pid: 7100, cores: 1)spawned uWSGI worker 3 (pid: 7101, cores: 1)spawned uWSGI worker 4 (pid: 7102, cores: 1)

3.3 配置nginx

我们需要进入 /etc/nginx/con.d 目录下新建hengda.conf文件

# the upstream component nginx needs to connect toupstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8000; # for a web port socket (we'll use this first)}# configuration of the serverserver {# the port your site will be served onlisten80;# the domain name it will serve forserver_name 106.52.188.166; charsetutf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media {alias /home/hengda/media; # 指向django的media目录}location /static {alias /home/hengda/collected_static; # 指向django的static目录}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass django;includeuwsgi_params; }}

启动nginx

service nginx start # 启动nginxservice nginx stop # 停止nginxservice nginx restart # 重启nginxservice -l reload # 重新加载nginx

四、重新启动uwsgi

uwsgi --ini uwsgi.ini

访问地址:http://106.52.188.166:9102/

这样网站就部署成功了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。