首页 > python web
时间:2020-07-24 python web 查看: 836
阿里云linux系统上Django安装与简单配置教程(1)
目录
码好python的每一篇文章.
大家好!又有一段时间没更新文章了,没错,是真的忙了。想要写一篇文章,确实要花几天时间的功夫,因为要保障文章的质量。
后面的文章我会通过写些基础理论和实战文章,毕竟干技术的,光看理论确实会没那么多耐心,总之,我会穿插一些,保证各位看官满意为止。
序号 IP地址 操作系统 安装软件 备注 1 192.168.8.130 CentOS 7.6.1810 python3.6+django2.2 都运行在虚拟环境 2 192.168.8.131 CentOS 7.6.1810 mysql mysql数据 3 192.168.8.1 windows 10 PyCharm Pro 专业版能够使用与centos远程同步配置 CentOS7安装环境,请谷歌 或百度查找安装文档,该步骤略。
python环境安装
如何更新 pip 源步骤:
[root@localhost ~]# mkdir .pip
[root@localhost ~]# cd .pip/
[root@localhost .pip]# touch pip.conf
[root@localhost .pip]# vim pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
如何更新 YUM 源步骤:
[root@localhost ~]# yum install wget -y
# 备份yum源
[root@localhost python]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mkdir repo_bak
[root@localhost yum.repos.d]# mv *.repo repo_bak/
# 下载新的repo
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除系统yum缓存并生成新的yum缓存
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
# 安装EPEL(Extra Packages for Enterprise Linux )源
[root@localhost yum.repos.d]# yum install -y epel-release
# 再次清除系统yum缓存并生成新的yum缓存
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
# 查看系统可用的yum源和所有的yum源
[root@localhost yum.repos.d]# yum repolist enabled
PIP源更新参考链接
YUM源更新参考链接
(py369) [python@localhost Python]$ pip install django==2.2
(py369) [python@localhost Python]$ python -m django --version
2.2
(py369) [python@localhost Python]$ pip install mysqlclient
2.2 安装 Mysql数据库 说明: 此台CentOS IP地址是 192.168.8.131(通过NAT映射,可以上互联网)
安装与启动
提前下载如下安装包,通过winscp上传到根目录下:
http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.31-1.el7.x86_64.rpm
复制上面链接通过浏览器下载,速度更快。
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# yum -y install mysql-community-server-5.7.31-1.el7.x86_64.rpm
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
...此处省略字符
# active 就表示正常在运行了
Active: active (running) since Tue 2020-07-21 10:14:23 CST; 2s ago
...此处省略字符
修改密码及创建数据库
[root@localhost ~]# grep "password" /var/log/mysqld.log
2020-07-21T20:48:28.965616Z 1 [Note] A temporary password is generated for root@localhost: uQjeSi?N(2zH
[root@localhost ~]# mysql -uroot -p
Enter password:uQjeSi?N(2zH # 输入密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'DevOps@2020';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to 'root'@'%' identified by 'DevOps@2020';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS devops default charset utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| devops |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
[root@localhost ~]# cat /etc/my.cnf |grep -v ^#|grep -v ^$
[client]
default-character-set = utf8
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server = utf8
collation-server = utf8_general_ci
[root@localhost ~]# service mysqld restart
数据库连接验证正常:
windows 10下安装mysql拓展并验证连接数据库:
C:\>pip install mysqlclient
当一位屌丝打开浏览器访问某页面的时候,输入http://www.baidu.com,然后回车;
django后台urls就会匹配到链接,可以把urls看作是http链接,专业叫做路由;
说明 :不是我们攻城狮理解的路由协议哈。
urls匹配到了就会在View视图查找代码,然后做出响应;
说明 :view就是我们要写的python代码
template简单理解就是html页面模板;
Model简单理解数据库的一张表;
3.1 Django简单配置 3.1.1 创建一个工程(project)为devops:
[python@localhost ~]$ sudo yum install tree
(py369) [python@localhost Python]$ django-admin startproject devops
(py369) [python@localhost Python]$ tree devops
devops
├── devops
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
3.1.2 通过PyCharm远程同步服务器代码 说明: pycharm将django项目下载到window电脑上进行编辑,然后再实时同步到CentOS上,pycharm编写代码方便。
打开pycharm,点击Tools->Deployment->Configuration
4)修改自动同步配置,Tools->Deployment->Options
3.1.3 在pycharm上直接修改django的配置文件:
1)devops->devops->settings.py:
ALLOWED_HOSTS = ['*']
# 增加数据库信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'devops',
'HOST':'192.168.8.131',
'USER':'root',
'PASSWORD':'root@123',
'PORT':3306,
}
}
#修改语言编码和时区
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
2) 改完配置后,会自动同步到远程django那里。
摘取log部分:
2020/3/27 23:07] Automatic upload completed in 33 ms: 1 file transferred (103.6 kbit/s)
3.1.4 启动django服务 1)通过命令启动:
python manage.py runserver 0.0.0.0:8888
(py369) [python@localhost devops]$ python manage.py runserver 0.0.0.0:8888
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 27, 2020 - 23:45:14
Django version 2.2, using settings 'devops.settings'
Starting development server at http://0.0.0.0:8888/
Quit the server with CONTROL-C.
2)打开浏览器,输入
http://192.168.8.130:8888 :
此时,还没有创建超级管理员用户和密码,不能够登陆。
4)接下来启动自带的APP admin
(py369) [python@localhost devops]$ python manage.py migrate 备注: 此命令会把系统自带的admin app数据库写入到mysql中。
5)登陆数据库mariadb,看是否正常写入
[root@localhost ~]# mysql -u root -pDevOps@2020
# 查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ddjsdb |
| devops |
| devops01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.73 sec)
mysql> use devops
mysql> show tables;
+----------------------------+
| Tables_in_devops |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
10 rows in set (0.00 sec)
6)创建一个管理员用户
(py369) [python@localhost devops]$ python manage.py createcachetable
7)成功登陆后台管理系统
(py369) [python@localhost devops]$ python manage.py runserver 0.0.0.0:8888
(py369) [python@localhost devops]$ python manage.py startapp hello
(py369) [python@localhost devops]$ touch hello/urls.py
(py369) [python@localhost devops]$ tree hello
hello
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
├── urls.py
└── views.py
3.2.2 pycharm同步下载远程hello目录
说明 :方法同章节3.1.2一样。
3.2.3 修改django配置脚本 1) 修改devops->devops->setting.py配置
INSTALLED_APPS = [
...此处已省略
'hello.apps.HelloConfig', # 方式一:新增hello app信息
'hello', # 方式二:新增hello app信息
]
2) 修改devops->hello->views.py配置
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('<p>hello django</p>')
3) 修改devops->hello->urls.py配置
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.index, name='index'),
]
4)最后修改devops->devops->urls.py配置
说明: 此urls.py为根路由入口(即总入口)。
第一种静态路由urls演示方法:
from hello import views
urlpatterns = [
path('admin/', admin.site.urls), # 缺省
path('hello/', views.index), # 新增
]
登陆浏览器,输入 http://192.168.8.130:8888/hello/, 效果如下:
第二种静态路由urls演示方法:
from django.urls import path,include # 新增,导入include模块
urlpatterns = [
path('admin/', admin.site.urls), # 缺省
path('hello/', include('hello.urls')), # 新增
]
登陆浏览器,输入 http://192.168.8.130:8888/hello/hello, 效果如下: