docker容器部署django+nginx+uwsgi环境


环境:centos7+Nginx+uwsgi+python36+django1.2.4

一、搭好docker容器

二、配置好Python3.6和pip3

1. 进入容器
2. 安装EPEL和IUS软件源
yum install epel-release -y
yum install https://centos7.iuscommunity.org/ius-release.rpm -y
3. 安装Python3.6
yum install python36u -y
4. 创建python3连接符
ln -s /bin/python3.6 /bin/python3
5. 安装pip3
yum install python36u-pip -y
6. 创建pip3链接符
ln -s /bin/pip3.6 /bin/pip3

三、安装Nginx

1. 安装
yum install -y nginx
2. 修改端口测试Nginx
# 进入配置目录
cd /usr/etc/
cd /etc/nginx/
# 备份
cp nginx.conf nginx.conf.back
# 修改端口,将默认端口改为70等其他端口
vi nginx.conf
# 开启Nginx
 /usr/sbin/nginx

在这里插入图片描述

测试成功!

三、安装uwsgi

1. 安装
pip3 install uwsgi
2. 出现问题
       raise Exception("you need a C compiler to build uWSGI")
    Exception: you need a C compiler to build uWSGI

    ----------------------------------------
Command "/usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-tqaeqiis/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-kz3ekp8d-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-tqaeqiis/uwsgi/
You are using pip version 9.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
3. 升级pip
pip3 install --upgrade pip
4. 再次安装

还是那个问题

 Exception: you need a C compiler to build uWSGI
# 没有编译
5. 发现需要安装python-devel(python36u-devel)

但是安装完成还是没有解决问题

6. 安装gcc编译环境
yum install gcc
7. 安装完成后再次uwsgi,终于安装成功
[root@b2e801a3b6ee ~]# pip3 install uwsgi
Collecting uwsgi
  Using cached https://files.pythonhosted.org/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz
Installing collected packages: uwsgi
  Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.18

四、配置django

1. 安装
 pip3 install django==2.1.4

安装过程中因为服务器问题出现一些错误

pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

使用软件源安装pip会出现这样的问题,其他方式安装则没有这种问题。需要多次尝试才能成功

2. 新建一个django项目
3. 设置项目路径为:/root/server/dj_test
4. 迁移数据库
python3 manage.py migrate
5. 修改setting.py的ip配置
ALLOWED_HOSTS = ["*"]
6. 试运行django

运行成功

五. 配置uwsgi.ini

1. 在项目根目录下新建uwsgi.ini
2. 写入以下内容
[uwsgi]

socket = 0.0.0.0:4433	#本地ip:端口
chdir =/root/server/dj_test	#项目绝对地址
module =dj_test.wsgi	#wsgi.py相对地址
master =true
process =4
threads = 2
buffer-size = 65536
vacuum =true
daemonize =/root/server/uwsgi.log	#日志
pidfile =/root/server/pid.log	#日志
3. 启动uwsgi
uwsgi --ini uwsgi.ini

六. 配置Nginx

1. 进入配置文件
2. 修改配置文件(nginx.conf)
  • 原内容:
 server {
        listen       70 default_server;
        listen       [::]:70 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

  • 修改后内容:
 server {
        listen       70 default_server;
        listen       [::]:70 default_server;
        server_name  *.*.*.*;	#服务器IP
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass 127.0.0.1:4433;
        client_max_body_size 35m;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

3. 重启Nginx
4. 配置成功

在这里插入图片描述

5. 生成镜像,push到创库

七. 遇到过的一下问题

1. 配置好Nginx,uswgi停止后无法启动

原因:setting.py的ip没有配置

2. The page you are looking for is temporarily unavailable. Please try again la问题

原因:setting.py的ip没有配置

还有各种问题忘了…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值