美文网首页我爱编程
Ubuntu:Django + MySQL + Uwsgi +

Ubuntu:Django + MySQL + Uwsgi +

作者: 捞小虾 | 来源:发表于2018-04-16 14:34 被阅读116次

OS: Ubuntu 16.04.1
Django: 2.0.4


Django 安装

  • 安装python3:apt install python3
  • 安装pip3: apt install python3-pip
  • 安装django: pip3 install Django
  • 检查是否安装成功
root@server_1:/var/HttpTestProject# django-admin.py --version
2.0.4

MySQL 安装

  • 安装MySQL Server: apt install mysql-server
  • 安装MySQL Client: apt install mysql-client
  • 启动MySQL Server: /etc/init.d/mysql start

DB配置 -- Local MySQL

  • MySQL创建项目DBCREATE DATABASE IF NOT EXISTS test COLLATE utf8_general_ci;
  • 更改项目 settings.py 中的 DB 配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}
  • 安装MySQLdb python module: apt-get install libmysqld-dev
  • 检查是否安装成功
>>> import MySQLdb
>>>
  • 如果没有安装成功且提示ImportError: No module named 'MySQLdb', 安装pip3 install MySQL-python
  • 如果安装MySQL-python失败,且提示ImportError: No module named 'ConfigParser', 安装:
pip3 install mysqlclient
apt install python3-dev libmysqlclient-dev
  • MySQLdb安装成功:
>>> import MySQLdb
>>>
  • 检查 Django 与 MySQL 是否绑定成功
root@server_1:/var/HttpTestProject# python3 manage.py dbshell
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Uwsgi 安装

  • 安装 uWSGI:pip3 install uwsgi
  • 检查是否安装成功
root@server_1:/etc/nginx/sites-enabled# uwsgi --version
2.0.17

Uwsgi 部署

  • Django Project 示例
    • Project: /var/HttpTestProject/
    • APP: /var/HttpTestProject/HttpTest/
HttpTestProject/
├── HttpTest
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-35.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-35.pyc
│   │   ├── __init__.cpython-35.pyc
│   │   ├── models.cpython-35.pyc
│   │   └── views.cpython-35.pyc
│   ├── static
│   ├── templates
│   │   └── index.html
│   ├── tests.py
│   └── views.py
├── HttpTestProject
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   ├── urls.cpython-35.pyc
│   │   └── wsgi.cpython-35.pyc
│   ├── settings.py
│   ├── settings.py.original
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── __pycache__
    └── manage.cpython-35.pyc
  • 在项目 HttpTestProject 目录下添加 uwsgi 目录
cd /var/HttpTestProject/
mkdir uwsgi
  • 在 uwsgi 目录下添加 uwsgi.ini 文件
# uwsig使用配置文件启动
[uwsgi]

# 项目目录
chdir=/var/HttpTestProject/

# 指定项目的application
module=HttpTestProject.wsgi:application

# 指定sock的文件路径       
socket=/var/HttpTestProject/uwsgi/HttpTestProject.sock

# 进程个数       
workers=4
pidfile=/var/run/HttpTestProject.pid

# 指定IP端口       
http=127.0.0.1:8001

# 指定静态文件
static-map=/static=/var/HttpTestProject/static

# 启动uwsgi的用户名和用户组
uid=root
gid=root

# 启用主进程
master=true

# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true

# 序列化接受的内容,如果可能的话
thunder-lock=true

# 启用线程
enable-threads=true

# 设置自中断时间
harakiri=30

# 设置缓冲
post-buffering=4096

# 设置日志目录
daemonize=/var/log/HttpTestProject_uwsgi.log

Nginx 安装

  • 安装 nginx:apt install nginx
  • 检查是否安装成功
root@server_1:/var# nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

Nginx 部署

  • 修改 /etc/nginx/sites-enabled 配置
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        # 指定项目路径uwsgi
        location / {

                # 导入一个Nginx模块他是用来和uWSGI进行通讯的
                include uwsgi_params;   

                # 设置连接uWSGI超时时间
                uwsgi_connect_timeout 30;  

                # 指定uwsgi的sock文件所有动态请求就会直接丢给他
                uwsgi_pass unix:/var/HttpTestProject/uwsgi/HttpTestProject.sock; 
        }

        # 指定静态文件路径
        location /static/ {
                alias /var/HttpTestProject/HttpTest/static/; 
        }
}

相关文章

网友评论

    本文标题:Ubuntu:Django + MySQL + Uwsgi +

    本文链接:https://www.haomeiwen.com/subject/umzckftx.html