记录工作和生活点点滴滴

python之web开发系列:CentOS7安装python3.7+django+nginx+Uwsgi(五)

一、概述

准备学python,但是看基础教程,脑瓜疼,准备以实际需求入手,记录下学习过程。之前写过几篇,然后发现前后都乱套了,准备重新写,大部分理论信息来自官方文档。

二、环境准备

操作系统:CentOS Linux release 7.6.1810 mini版
Python版本:3.7.4
Django版本:2.2.3
Nginx版本:1.16.0
Uwsgi版本:2.0.18

资料整理:
python3.7.4官方中文文档

django2.2.3官方中文文档

三、开始安装

3.1 安装python3.7.4

  1. 安装依赖包
    yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

    yum groupinstall "Development tools"

  2. 升级SQLite最新版

    # wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
    # tar zxvf sqlite-autoconf-3290000.tar.gz
    # cd sqlite-autoconf-3290000
    # ./configure
    ## make && make install

    验证SQLite版本
    sqlite3 --version
    file

  3. 安装python3.7 打开python官网,下载源码。选择适合你的系统版本

file

  1. 输入解压缩命令:
    #tar zxvf Python-3.7.4.tgz

    # cd Python-3.7.4
    #  ./configure  --prefix=/usr/local/python3.7
    # make && make install
    # ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3

    file

  2. 验证是否安装成功,出现如下提示安装成功
    # python3
    file

    3.2 安装Django

    输入下面命令安装django
    # /usr/local/python3.7/bin/pip3 install django -i https://pypi.tuna.tsinghua.edu.cn/simple

    确认django是否安装成功

    file

    3.3 安装virtualenv

    # /usr/local/python3.7/bin/pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
    # /usr/local/python3.7/bin/pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple

    3.4 安装nginx

    # rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    # yum install -y nginx

    3.5 安装uWSGI

  3. 输入如下命令# /usr/local/python3.7/bin/pip3 install uwsgi

file

四、修改配置文件

  1. 修改nginx配置文件创建vhost主机/etc/nginx/conf.d/poll.xucg.info.conf,复制下面代码到这个文件

    server {
    listen  80;
    server_name  poll.xucg.info;  
    
    charset utf-8;
    
    access_log  /var/log/nginx/poll_access.log;
    error_log   /var/log/nginx/poll_error.log;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    
    location /static/ {
        root /home/wwwroot/poll/poll;
    }
    
    client_max_body_size 20M;
    
    location / {
       uwsgi_pass 127.0.0.1:3031;
        include uwsgi_params;
        uwsgi_read_timeout 30s;
        uwsgi_send_timeout 30s;
    }
    }
  2. 修改uWSGI配置

    # mkdir -p /etc/uwsgi/conf
    #vi /etc/uwsgi/conf/uwsgi-django.ini
    [uwsgi]
    project = poll.xucg.info
    base = /home/wwwroot/
    chdir = %(base)/%(project)
    home = %(base)/%(project)/.py3env
    module = myproject.wsgi:application
    pidfile = /tmp/uwsgi-master-django.pid
    master = true
    processes = 2
    enable-threads = true
    # use unix socket because it is more secure and faster than TCP socket
    socket = /etc/uwsgi/uwsgi-poll.sock
    chmod-socket = 660
    uid = nginx
    gid = nginx
    vacuum = true
    die-on-term = true
    logto = /var/log/nginx/uwsgi-poll.log
  3. 配置Django base folder

    # cd /home/wwwroot/
    # /usr/local/python3.7/bin/virtualenv -p /usr/bin/python3 .py3env

    file

  4. 开启virtualenv python3环境
    # source .py3env/bin/activate

  5. 在此环境安装Django相关模块
    # pip3 install django uwsgi PyMySQL -i https://pypi.tuna.tsinghua.edu.cn/simple

    file

  6. 创建uWSGI启动脚本

    # mkdir -p /etc/uwsgi/bin
    # vi /etc/systemd/system/uwsgi-django.service
    [Unit]
    Description=uWSGI instance to serve myproject
    [Service]
    BASE=/home/wwwroot/poll.xucg.info
    ENV=$BASE/.py3env
    ExecStartPre=-/usr/bin/bash -c 'chown -R nginx:nginx /etc/uwsgi'
    ExecStart=/usr/bin/bash -c 'source /home/wwwroot/poll .py3env/bin/activate; uwsgi --ini /etc/uwsgi/conf/uwsgi-poll.ini'
    [Install]
    WantedBy=multi-user.target
  7. Django项目配置

  8. 保证virtualenv python3环境开启

    # cd /home/wwwroot/
    # source .py3env/bin/activate
  9. 创建一个Django项目
    # django-admin startproject poll

  10. 添加static目录poll/poll/settings.py

    末行添加:
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")

  11. 创建本地SQLlite文件

    Tip:这里使用SQLlite代替其他数据库作为我们项目的DB

    # ./manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK
  1. 创建项目管理员账户
./manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.

file

  1. 生成项目静态文件目录
    ./manage.py collectstatic
    file

  2. 修改wsgi入口文件poll/poll/wsgi.py

    import os
    import sys
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    sys.path.append('/home/wwwroot/poll/poll')
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
  3. 添加ALLOWED_HOSTS
    # vi poll/poll/settings.py

    Update:
    ALLOWED_HOSTS = ['django.xucg.info']
  4. 修改权限(可执行并保持与nginx启动user一致)

    # chmod -R 755 /etc/uwsgi
    # chown -R nginx:nginx /etc/uwsgi
    # chmod -R 755 /home/wwwroot/poll
    # chown -R nginx:nginx /home/wwwroot/poll
  5. 启动nginx+uwsgi

    # systemctl restart nginx
    # systemctl restart uwsgi-django

    展示效果(保证Windows本地host文件能够解析django.xucg.info)
    file
    file

未经允许不得转载:许春刚技术博客 » python之web开发系列:CentOS7安装python3.7+django+nginx+Uwsgi(五)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址