1. 安装Nginx
docker run -d \
-p 18000:18000 \
-p 18001:18001 \
--name nginx \
--volume /home/nginx/html:/usr/share/nginx/html \
--restart always \
nginx:1.14
2. 安装python3
docker exec -it nginx /bin/bash
apt-get update && apt-get install -y net-tools vim python3 python3-pip
python3 -V
pip3 -V
3. 安装django
pip3 install django
python3 -m django --version
4. 安装uwsgi
pip3 install uwsgi
5. 创建django项目
cd /mnt
django-admin startproject django_test
cd ./django_test
vim ./django_test/settings.py
ALLOWED_HOSTS ["*",]
python3 manage.py runserver 0.0.0.0:18001
6. uwsgi启动django项目
uwsgi --http :18001 --wsgi-file /mnt/django_test/wsgi.py
7. nginx+uwsgi配置
7.1 配置nginx
vim /etc/nginx/conf.d/django_test.conf
upstream django {
server 127.0.0.1:18001;
}
server {
listen 18000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
# location /media {
# alias /mnt/django_test/media;
# }
#
# location /static {
# alias /mnt/django_test/static;
# }
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 192.168.2.118:18001;
}
}
7.2 配置uwsgi
vim /mnt/django_test/django_uwsgi.ini
[uwsgi]
#http = :18001
socket = :18001 #使用nginx代理请求时配置,直接访问uwsgi使用http方式
chdir = /mnt/django_test #项目根目录的绝对路径
module = django_test.wsgi #项目目录下的uwsgi.ini
master = true # 指定启动主进程
processes = 10 # 设置工作进程的数量
vacuum = true
buffer-size = 32768
#pidfile = uwsgi.pid #存储uwsgi运行的pid值的文件
#daemonize = uwsgi.log #存储运行的日志文件
8. nginx+uwsgi启动django
8.1 启动nginx
nginx -t
nginx -s reload
docker restart nginx
8.2 启动uwsgi
uwsgi --ini /mnt/django_test/django_uwsgi.ini
注:
# uwsgi启动
uwsgi --ini uwsgi.ini
#uwsgi 停止
uwsgi --stop uwsgi.pid
9. 测试
访问地址:http://192.168.2.118:18000
遇到的问题:
Q1:uwsgi未配置buffer-size
报错信息:invalid request block size: 21573 (max 4096)...skip
原因: url地址长度超过了4096个字符,而4096就是uwsgi配置中buffer-size的默认值,所以只需要将buffer-size改大一点即可。
解决:
vim /mnt/django_test/django_uwsgi.ini
buffer-size = 32768
Q2:docker容器重启无法启动
原因: docker容器中更改nginx配置文件错误
解决:
- 方式一: 在当前主机中存放了docker容器中的配置信息(一般默认目录是:/var/lib/docker/overlay2/),那只要找到这个配置文件,修改后就可以重新启动了 宿主机更改容器配置文件:
docker inspect nginx |greep overlay
- 方式二: 使用docker cp命令,把docker容器中的配置文件复制到主机中来,然后修改完后再复制到docker容器中去
教训: 更改nginx配置文件后,先nginx -t测试,无误后再重启nginx