安装django并且部署一个应用
django是python一个应用广泛的web开发框架,关于django的使用介绍在中文官方文档中已经有了详细的介绍,有兴趣的同学可以学习django的基础内容,地址如下:
http://python.usyiyi.cn/django/index.html
本系列的文章希望可以使用django+salt等工具可以实现一个资产管理及自动化的运维平台,可能会遇到很多的坑。希望借此机会可以好好实践下运维开发的相关技巧以及学习python的相关编程方法。
安装django
django的安装可以直接使用pip命令从pypi上进行下载。关于pip的安装晚上有很多教程,最关键的是要安装setuptools包。如果有pip命令可以直接指向如下命令:
[root@server2 django]# pip install django
验证django是否安装成功:
[root@server2 django]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.__version__
1.10.4
开启第一个项目
可以使用django-admin命令打开一个django项目,我们这里起名为monitor_eye:
[root@server2 django]# django-admin startproject monitor_eye
该项目的目录初始目录结构如下:
[root@server2 django]# tree monitor_eye/
monitor_eye/
├── manage.py
└── monitor_eye
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
manage.py是一个命令行工具,它会通过django.core.management内的
execute_from_command_line执行我们传入的参数。实现如下:
[root@server2 monitor_eye]# cat manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "monitor_eye.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
其中最内层的monitor_eye目录才是我们真实的python包。其中分布着和项目相关的很多文件,settings.py是该项目的配置文件:
[root@server2 monitor_eye]# ll settings.py
-rw-r--r-- 1 root root 3111 Dec 22 14:15 settings.py
设置数据库
1.设置数据库的配置
django默认的数据库是sqlite3,如果为了后期的数据存储方便,或者为了更强的稳定性可以选择其他数据库,本例我们选择mysql数据库。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'monitor_eye',
'USER': 'monitoreye',
'PASSWORD': 'monitoreye',
'HOST': '192.168.42.131',
'PORT': '3306',
}
}
在对应的主机192.168.42.131上创建mysql的库monitor_eye,授权给monitoreye用户,使用monitoreye密码可以操作该库。mysql的端口设置为3306.
2.安装数据库
在redhat7中采用mariadb数据库:
[root@server2 monitor_eye]# yum install mariadb-server -y
启动mariadb数据库:
[root@server2 monitor_eye]# systemctl start mariadb
[root@server2 monitor_eye]# systemctl enable mariadb
创建表结构并进行授权:
[root@server2 monitor_eye]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zabbix |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create database monitor_eye;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on monitor_eye.* to 'monitoreye'@'%' identified by 'monitoreye';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在settings.py文件中设置模式为调试模式(因为我们当前是处于测试环境),并且把当前django所在服务器的ip加入allow_host列表中:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [u'192.168.42.131']
根据settings.py里的内容创建相关表结构和进行初始化:
[root@server2 monitor_eye]# python manage.py migrate
3.开启django服务
为了让其他节点的主机也可以访问该django服务,以如下方式开启服务:
[root@server2 monitor_eye]# python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
December 22, 2016 - 17:03:35
Django version 1.10.4, using settings 'monitor_eye.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
可以看到测试页面如下:
小结
这个是django的最基本开发环境,在后续的章节中我们将要逐渐介绍django的其他组件内容。