From d11b9c70bc4ae854b97a008e5e70cb53f9567451 Mon Sep 17 00:00:00 2001 From: giteeyunyunyun <18883994582@163.com> Date: Sun, 18 Apr 2021 22:38:11 +0800 Subject: [PATCH 1/2] 16week-zuoye --- .../week16/16-3noteyun.md" | 309 ++++++++++++++++++ ...40\346\226\207\344\273\266-16-1noteyun.md" | 143 ++++++++ ...277\347\273\247\346\211\27716-2noteyun.md" | 208 ++++++++++++ 3 files changed, 660 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/16-3noteyun.md" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\344\270\212\344\274\240\346\226\207\344\273\266-16-1noteyun.md" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\346\250\241\346\235\277\347\273\247\346\211\27716-2noteyun.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/16-3noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/16-3noteyun.md" new file mode 100644 index 00000000..743881fc --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/16-3noteyun.md" @@ -0,0 +1,309 @@ +16-3noteyun + +``` +https://docs.djangoproject.com/zh-hans/2.0/ + +E:\PycharmProjects\Spider_Code\mysite + +(py37b) E:\PycharmProjects\Spider_Code\mysite> +``` + + + +# restful api + +http://www.ruanyifeng.com/blog/2014/05/restful_api.html +https://www.jianshu.com/p/73d2415956bd + +```python +http://www.ruanyifeng.com/blog/2014/05/restful_api.html +https://www.jianshu.com/p/73d2415956bd +``` + +> 随着前后端分离的流行, 为了减少前后端的沟通成本. 那么势必有一套开发规范来为前后端开发提供标准, restful api就是这样的开发规范. + +- 为什么前后端分离 + + - MTV模型这样高度耦合的开发模式更适合个人独立开发, 不适合多人员的配合和快速迭代. + - 为了适配网页端和移动端等多个终端. + - 前端业务场景已经越来越复杂, 需要有专业的人员进行开发, 减少了开发人员的学习成本. + +- restful api + + > URL用来定位资源, method用来表示操作. + + - 基于HTTP协议扩展的开发规范. + + - URL + + - METHOD + + - HTTP请求格式 + + - 请求体 + - 请求头 + + - HTTP响应格式 + + - 返回头 + + - 返回数据类型 + + - **状态码** + + - 200 + + 表示成功 + + - 400 + + - 401 + + 没有权限 + + - 403 + + 访问被禁止 + + - 404 + + 未找到对应资源 + + - 500 + + 表示服务器内部错误. + + - 面向资源的涉及理念 + + > 将前端和后端之间的交互抽象为对数据, 也就是资源的操作. + + - url只表示资源的路径 + + > 动词不应该出现在url当中 + > + > 可以在域名中体现当前api接口 + > + > 可以在url当中体现当前api接口的版本 + + ```python + https://api.example.com/v1/zoos + https://example.com/api/v2/zoos + ``` + + - HTTP协议中的method表示操作 + + - GET(retrieve) + - POST(create) + - PUT(update) + - DELETE(delete) + +# djangorestframework + +``` +https://www.django-rest-framework.org/ +``` + +- 安装 + + ```python + pip install djangorestframework + ``` + +- 配置 + + ```python + INSTALLED_APPS = [ + ... + 'rest_framework', + ] + ``` + +- 编写`serializers`模块 + + - 新建文件`serializers.py` + + - 编写序列化模型 + + ```python + from rest_framework import serializers + from .models import Article + + + class ArticleSerializer(serializers.ModelSerializer): + class Meta: + model = Article + # 可以只填充我们使用到的字段 + fields = ("title", "text", "url", "create_time") + ``` + + - model对象 -> OrderedDict对象 -> json对象 + + ```python + # 进入shell环境 + from blog.models import Article + from blog.serializers import ArticleSerializer + + >>> article_array = Article.objects.all() + >>> se_articles = ArticleSerializer(aritcles, many=True) + >>> json_articles = json.dumps(se_articles.data) + ``` + + - json对象 ->model对象 + + ```python + # 序列化单个对象 + >>> article = article_array[0] + >>> se_article = ArticleSerializer(aritcle) + # 模拟页面返回的json格式字符串 + >>> json_data = json.loads(json.dumps(se_article.data)) + + # 进行反序列化 + >>> se_article = ArticleSerializer(json_data) + >>> se_article.is_valid() + >>> True + >>> article = se_article.create(se_article.data) + >>> article + > + ``` + +# 文章接口 + +```python +from rest_framework.views import APIView +from django.db import transaction + +class ArticleApiView(APIView): + def get(self, request, *args, **kwargs): + """ + article?limit_num=2 + :param args: + :param kwargs: + :return: + """ + # 重点是学习如何获取url当中的参数 + limit_num = request.GET.get("limit_num") + if limit_num: + article_array = Article.objects.all()[:int(limit_num)] + else: + article_array = Article.objects.all() + se_article_array = ArticleSerializer(article_array, many=True) + return JsonResponse({ + "status": 200, + "data": se_article_array.data + }, safe=False) + + def post(self, request, *args, **kwargs): + """ + 添加文章的时候, 需要带上category和tag的信息 + 只要article, category, tag有一方报错的时候, 当前文章都算添加失败. + 所以我们应该适用事务进行包装 + :param request: + :param args: + :param kwargs: + :return: + """ + try: + message = {"status": 200} + with transaction.atomic(): + title = request.POST['title'] + text = request.POST['text'] + categories = json.loads(request.POST['categories']) + tags = json.loads(request.POST['tags']) + + article_data = { + "title": title, + "text": text, + "url": f"/{time.strftime('%Y/%m/%d')}/{title}.html" + } + se_article = ArticleSerializer(data=article_data) + se_article.is_valid() + article = se_article.create(se_article.data) + article.save() + + for category in categories: + cate = Category( + name=category['name'], + slug=category['slug'], + uri=f"/categories/{category['slug']}/" + ) + cate.article = article + cate.save() + + for tag in tags: + tag = Tag( + name=tag['name'], + slug=tag['slug'], + uri=f"/tags/{tag['slug']}/" + ) + tag.article = article + tag.save() + except: + message = {"status": 500, "reason": "添加文章失败"} + finally: + return JsonResponse(message, safe=False) + + def put(self, request, *args, **kwargs): + """ + 重点是如何获取携带的参数 + request.POST(key) + :param request: + :param args: + :param kwargs: + :return: + """ + pass + + def delete(self, request, *args, **kwargs): + pass +``` + +# 分页 + +```python +SELECT * FROM TABLE WHERE 表达式 LIMIT (page_num*page_size), page_size +``` + +# 跨域问题 + +> csrf_token是解决客户端身份认证的问题, 现在基本都适用session + +因为浏览器安全的同源策略, 当前host下不可以发送请求到其他host + +> ali.com下的页面不可以向tencent.com发送请求, 会被浏览器拦截 + +- 如果想突破同源策略, 需要前后端一期配合 + + - 前端需要在请求中设置跨域请求 + + - 后端要做相应的跨域设置 + + ```python + # 设置跨域响应头 + response = JsonResponse(message, safe=False) + response['Access-Control-Allow-Origin'] = "*" # *表示任意域名 + response['Access-Control-Allow-Headers'] = "*" + response['Access-Control-Allow-Methods'] = "OPTIONS, POST, GET" + ``` + +# 课后作业 + +- 完成restful api设计 +- 利用序列化模型修改ArchiveView +- 理解什么是restful api并口述 +- 练习分页 +- 理解跨域问题 + + + + + + + + + + + + + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\344\270\212\344\274\240\346\226\207\344\273\266-16-1noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\344\270\212\344\274\240\346\226\207\344\273\266-16-1noteyun.md" new file mode 100644 index 00000000..ccc94ca7 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\344\270\212\344\274\240\346\226\207\344\273\266-16-1noteyun.md" @@ -0,0 +1,143 @@ +16-1noteyun + +``` +https://docs.djangoproject.com/zh-hans/2.0/ + +E:\PycharmProjects\Spider_Code\mysite + +(py37b) E:\PycharmProjects\Spider_Code\mysite> +``` + + + +# 上传文件 + +为服务添加上传文件的功能, 但是当前做法只适用于独立开发debug模式. 常规做法是通过nginx搭建静态资源服务器. + +- 在`settings`中添加media路径 + + ```python + MEDIA_URL = "/media/" + MEDIA_ROOT = os.path.join(BASE_DIR, "media") + ``` + +- 配置路由 + + ```python + urlpatterns = [ + path("mdeditor/", include("mdeditor.urls")), + ] + static(settings.STATIC_URL) + static(settings.MEDIA_URL, document_root=settings.MEDIR_ROOT) + ``` + +# 评论系统 + +> 不要用模型来表示庞大的评论关系 + +- 平铺型(微信) + + ![QpN8O.jpg](https://i.im5i.com/2021/02/03/QpN8O.jpg) + +- 主题型(bilibili, 微博) + + 一般来说, 一定会有一个评论主题. + +- 盖楼式(网易, acfun) + + 次级评论会引用上级评论, 是个层层递进的关系 + + ![wy69L.jpg](https://i.im5i.com/2021/04/12/wy69L.jpg) + +# 添加Gitalk + +``` +https://github.com/gitalk/gitalk/blob/master/readme-cn.md +``` + +- 注册github + + 不可以使用163邮箱 + +- 引用gitalk + +- 注册github应用 + + 目的是引入github的身份系统. + + - home_page和callback + + 前者是当前博客的主页, 后者是验证完身份后会重定向跳转的页面. + + ``` + http://127.0.0.1:8000 + ``` + + - 生成密钥 + +- 点击右上角`+`创建仓库 + +- 填写参数 + + ```python + var gitalk = new Gitalk({ + clientID: '5f9e597715599d429227', + clientSecret: 'bf459519d7275a802f0eb760fc9e681e84276a21', + repo: 'my_blog_comment', // 保存所有评论的仓库名字 + owner: 'tunan-github', // 用户名 + admin: ['tunan-github'], + id: md5(location.pathname), // 保证当前id是唯一的而且长度要小于50 + // 这里用路径md5hash签名后生成的值作为文章在github仓库中的唯一标识 + distractionFreeMode: false // Facebook-like distraction free mode + }) + ``` + +- 常见的bug + + - Validate Error + + 检查id是否超过50 + + - 引入gitalk失败403 + + 需要向gitalk作者的中转服务器申请权限. + + - 第一步 + + 打开网址:https://cors-anywhere.herokuapp.com/ + + - 第二步 + + 点击下图按钮 + + ![wyN4t.png](https://i.im5i.com/2021/04/13/wyN4t.png) + + - 第三步 + + 看到以下内容即成功申请试用资格 + + ![wygKq.png](https://i.im5i.com/2021/04/13/wygKq.png) + +# 其他 + +- 下一篇 + + ```python + # article.id + 1 是最快的方案, 但是我们不能保证自增键id不会中断. + # 当前返回的结果不是QuerySet而是RawQuerySet, 当前并没有真正地请求并获取到查询结果. + raw_query_set = Article.objects.raw(f"select * from {Article._meta.db_table} where id < {article.id} order by id desc limit 1") + try: + next_article = raw_query_set[0] + context = { + "next_article": next_article, + "article": article, + } + except IndexError: + context = { + "article": article, + } + ``` + +# 课后作业 + +- 完成文件上传功能 +- 完成gitalk评论系统 +- 完成下一篇, 上一篇功能 \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\346\250\241\346\235\277\347\273\247\346\211\27716-2noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\346\250\241\346\235\277\347\273\247\346\211\27716-2noteyun.md" new file mode 100644 index 00000000..8b13c001 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week16/\346\250\241\346\235\277\347\273\247\346\211\27716-2noteyun.md" @@ -0,0 +1,208 @@ +16-2noteyun + +``` +https://docs.djangoproject.com/zh-hans/2.0/ + +E:\PycharmProjects\Spider_Code\mysite + +(py37b) E:\PycharmProjects\Spider_Code\mysite> +``` + + + +# 模板继承 + +- 父模板 + + 为填充的内容提供接口 + + ```python + + ... + {% block content %} + {% endblock %} + ... + + ``` + +- 子模版 + + - 加载指定的父模板 + + - 嵌入子模版 + + ```python + {% extends '_base.html' %} + + {% block content %} + ... + {% endblock %} + ``` + +# Archives页面 + +- 配置路由 + +- 修改模板 + +- 添加视图 + + ```python + class ArchiveView(TemplateView): + """ + archive_dict = { "2018": {"year": 2018, "article_list": []}} + """ + template_name = "archive.html" + + def get(self, request, *args, **kwargs): + article_list = Article.objects.all() + + archive_dict = {} + for article in article_list: + year = article.create_time.strftime("%Y") + # 如果当前字典没有目标key, 则给定默认值 + # 如果当前字典有目标key, 则无变化 + archive_dict.setdefault(year, {"year": year, "article_list": []}) + archive_dict[year]['article_list'].append(article) + + context = { + "archive_list": archive_dict.values(), + "article_count": len(article_list) + } + + return self.render_to_response(context) + ``` + +# 缓存(redis) + +```python +https://django-redis-chs.readthedocs.io/zh_CN/latest/ +``` + +> 涉及两个数据的查询操作, 一定会面临数据一致性的问题. Mysql中数据可能时最实时的, redis会延后30s. 牺牲了数据的实时性换取查询性能. + +- 什么场景下使用缓存 + + - 短时间 + + 解决短时间高并发带来的数据库查询压力, 缓存的生存周期定义在业务合理的范围内. + + - 长时间 + + 将长时间不会发生任何更改的查询结果缓存起来, 减少了查数据库的操作, 提高了性能. + +- 安装 + + ``` + pip install django-redis + ``` + +- 设置 + + ```python + CACHES = { + 'default': { + 'BACKEND': 'django_redis.cache.RedisCache', + 'LOCATION': 'redis://127.0.0.1:6379/3', + 'OPTIONS': { + 'CLIENT_CLASS': 'django_redis.client.DefaultClient' + } + }, + } + ``` + +- 激活缓存 + + ```python + python manage.py createcachetable + ``` + +- 测试是否激活成功 + + ```python + python manage.py shell + + >>> from django.core.cache import cache + >>> cache.set("test_key", "test_value") + True + ``` + +- 对视图进行缓存 + + ```python + from django.views.decorators.cache import cache_page + + @cache_page(60 * 15) + def my_view(request): + ... + ``` + +- 自定义缓存 + + > 需要将对象转换为json存到redis缓存中, 然后从redis缓存中获取json数据, 再反序列化为对象 + + - 使用`serializer.serialize`不能序列化二层的嵌套列表 + + - 使用`serializers.deserialize`返回的是一个`Deserialize`对象, 需要访问`Deserialize.object`来获取`model`对象 + + ```python + class ArchiveView(TemplateView): + """ + archive_dict = { "2018": {"year": 2018, "article_list": []}} + """ + template_name = "archive.html" + + def get(self, request, *args, **kwargs): + redis_key = "archive_cache" + redis_value = cache.get(redis_key) + if redis_value: + print("hit cache") + serializer_archives = json.loads(redis_value) + archive_list = [] + article_count = 0 + for archive in serializer_archives: + # 为了模板可以正常渲染, 需要将article反序列化为对象 + archive_list.append({ + "year": archive['year'], + "article_list": [obj.object for obj in serializers.deserialize('json', archive['article_list'])] + }) + article_count += len(archive['article_list']) + + context = { + "archive_list": archive_list, + "article_count": article_count + } + else: + article_list = Article.objects.all() + + archive_dict = {} + for article in article_list: + year = article.create_time.strftime("%Y") + # 如果当前字典没有目标key, 则给定默认值 + # 如果当前字典有目标key, 则无变化 + archive_dict.setdefault(year, {"year": year, "article_list": []}) + archive_dict[year]['article_list'].append(article) + + context = { + "archive_list": archive_dict.values(), + "article_count": len(article_list) + } + + # 缓存archive_list + serializer_archives = [] + for archive in archive_dict.values(): + serializer_archives.append({ + "year": archive['year'], + "article_list": serializers.serialize("json", archive['article_list']) + }) + cache.set(redis_key, json.dumps(serializer_archives)) + cache.expire(redis_key, 30) + + return self.render_to_response(context) + ``` + +# 课后作业 + +- 完成博客模板继承 +- 完成博客自定义的细节优化 +- 练习添加缓存 \ No newline at end of file -- Gitee From 3da6327e59ac40b6d6a26d57d83c0eb9834b2561 Mon Sep 17 00:00:00 2001 From: giteeyunyunyun <18883994582@163.com> Date: Sun, 25 Apr 2021 21:35:50 +0800 Subject: [PATCH 2/2] week17-homework --- .../Nginx\343\200\201uWSGI-17-3-noteyun.md" | 215 +++++++++++++++ ...6\345\275\225\346\240\221-17-1-noteyun.md" | 255 ++++++++++++++++++ ...7\344\277\241\346\201\257-17-2-noteyun.md" | 202 ++++++++++++++ 3 files changed, 672 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Nginx\343\200\201uWSGI-17-3-noteyun.md" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Unix\345\222\214linux\343\200\201\344\275\277\347\224\250linux\345\201\232\346\234\215\345\212\241\345\231\250\343\200\201\350\277\234\347\250\213\350\277\236\346\216\245linux\346\234\215\345\212\241\345\231\250\343\200\201linux\347\233\256\345\275\225\346\240\221-17-1-noteyun.md" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/\347\224\250apt\350\277\233\350\241\214\345\214\205\347\256\241\347\220\206\343\200\201\346\226\207\344\273\266\346\223\215\344\275\234\343\200\201\344\277\256\346\224\271\346\235\203\351\231\220\343\200\201\346\226\207\346\234\254\346\223\215\344\275\234\343\200\201\347\212\266\346\200\201\346\237\245\347\234\213\343\200\201\347\241\254\344\273\266-\347\263\273\347\273\237\344\277\241\346\201\257-17-2-noteyun.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Nginx\343\200\201uWSGI-17-3-noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Nginx\343\200\201uWSGI-17-3-noteyun.md" new file mode 100644 index 00000000..24201249 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Nginx\343\200\201uWSGI-17-3-noteyun.md" @@ -0,0 +1,215 @@ +17-3-noteyun + +Nginx、uWSGI + + + +# 准备工作 + +- 将项目代码拉到 linux本地环境 + +- 安装环境 + + - 虚拟机当中有python冲突 + + - 自己安装一个新版本python + + ```shell + apt-get install python3.7 + apt-get install python3.7-venv + ``` + + - 创建虚拟环境 + + ``` + python3.7 -m venv venv + ``` + + - 激活虚拟环境 + + ``` + source venv/bin/activate + ``` + + - 安装依赖 + + ``` + pip install -r requirements.txt + ``` + +# Nginx + +- Nginx + + 高性能http服务器及**反向代理服务器**. + + ![nginx]() + +- 为什么使用Nginx + + > 一个域名对应的只有一个IP, 一个IP对应一台服务器, 一台服务器可以启动多个服务. + > + > 那么我们怎么让服务器知道当前的请求应该由哪个服务来处理呢? + + 统一管理当前服务器内开启的对外暴露服务. + + - 减少了对外暴露的端口, 隐藏真实服务地址, 增加了安全性. + +- 安装nginx + + ``` + apt-get install nginx + ``` + +- 启动和关闭nginx + + ```shell + service nginx start + service nginx restart + service nginx stop + ``` + + - `127.0.0.1`, `0.0.0.0`, `localhost`的区别 + + - localhost + + localhost就是一个默认的本机域名, 你可以手动对其进行配置 + + - 127.0.0.1 + + 回环地址, 127开头的地址是一类向自身发送通信的特殊地址 + + - 0.0.0.0 + + 代表本机所有IP地址, 如果你想让外部可以访问到你的服务, 则需要将服务绑定到0.0.0.0地址上. + +- nginx部署静态资源 + + - 修改nginx配置文件 + + ``` + vim /etc/nginx/sites-available/default + + // 添加以下配置 + location /static/ { + alias /var/static/; // 当前静态资源保存的路径 + autoindex on; + add_header Cache-Control private; + expires 30d; + } + ``` + + - 重启nginx + + - 配置静态资源的好处 + + 前后端分离, 静态资源的问题可以由前端自由管理和控制. + +# uWSGI + +``` +https://uwsgi-docs.readthedocs.io/en/latest/ +``` + +- 什么是uWSGI + + > WSGI只是一种协议, 用来解决一个请求到服务端, 服务端与内部应用的交互. + + uWSGI是一个项目, 目的是通过统一的API和配置风格, 集成应用服务器, **代理, 进程管理**, 监控等功能 + +- 为什么需要uWSGI + + - 性能问题: 通过`python manage.py runserver`命令来启动的是单进程多线程的应用. + - uWSGI可以更好地控制并发 + +- 安装uWSGI + + ``` + # 在创建虚拟环境前和环境后各第一时间安装一次, 避免出错. + pip3 install uwsgi + ``` + +- 配置uWSGI + + ``` + https://uwsgi-docs.readthedocs.io/en/latest/Configuration.html + ``` + + - 在当前项目下创建`uwsgi.ini` + + - 添加以下内容 + + ```shell + [uwsgi] + # 项目本地服务地址 + http=127.0.0.1:5000 + + # 观察项目得运行情况 + stats=127.0.0.1:5100 + + # 项目的虚拟环境 + virtalenv=/home/tunan/my_blog_2/venv + + # 项目的根目录 + chdir=/home/tunan/my_blog_2 + + # 绑定wsgi协议 + wsgi-file=myblog/wsgi.py + + # 设置进程数, 跟cpu一致即可. + processes=12 + + # 线程数目 + threads=4 + + # 导出的log日志 + logto=/var/log/myblog.log + + # 如果django项目中使用了STATIC, 就要作静态资源的映射 + static-map=/static=static + ``` + +- 启动uwsgi + + ``` + uwsgi --ini uwsgi.ini & + ``` + +- 在nginx中做代理转发 + + ```shell + location /myblog/ { + proxy_pass http://127.0.0.1:5000; + } + + * 代理转发之后, http://127.0.0.1:5000的外部地址是http://域名/myblog. + ``` + + - 解决url匹配的问题 + + ``` + url也要做相应的修改, 将myblog添加到最高级路径 + ``` + +- 查看nginx日志 + + ```shell + cd /var/log/nginx + ``` + + - access.log + + 访问日志 + + - error.log + + 错误日志 + +# 课后作业 + +- 完成django的uwsgi+nginx的部署 +- 了解nginx的作用 +- 了解uwsgi的作用 + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Unix\345\222\214linux\343\200\201\344\275\277\347\224\250linux\345\201\232\346\234\215\345\212\241\345\231\250\343\200\201\350\277\234\347\250\213\350\277\236\346\216\245linux\346\234\215\345\212\241\345\231\250\343\200\201linux\347\233\256\345\275\225\346\240\221-17-1-noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Unix\345\222\214linux\343\200\201\344\275\277\347\224\250linux\345\201\232\346\234\215\345\212\241\345\231\250\343\200\201\350\277\234\347\250\213\350\277\236\346\216\245linux\346\234\215\345\212\241\345\231\250\343\200\201linux\347\233\256\345\275\225\346\240\221-17-1-noteyun.md" new file mode 100644 index 00000000..e17ac9a1 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/Unix\345\222\214linux\343\200\201\344\275\277\347\224\250linux\345\201\232\346\234\215\345\212\241\345\231\250\343\200\201\350\277\234\347\250\213\350\277\236\346\216\245linux\346\234\215\345\212\241\345\231\250\343\200\201linux\347\233\256\345\275\225\346\240\221-17-1-noteyun.md" @@ -0,0 +1,255 @@ +17-1-notyun + + + +> 计算机系统涉及了太多分支和商业纠葛, 这节课我们不纠结正统的问题, 只从涉及的概念上进行分类 + +Unix和linux、使用linux做服务器、远程连接linux服务器、linux目录树 + + + +# Unix和linux + +- Unix + + > 最古老的现代计算机系统, 始于60年代. 从商业上来讲, 只有unix认证的系统才是unix系统. + + - linux + + > linux和unix不是单纯的继承关系, linux只是继承了unix的设计理念. 但是底层实现不同, 所以不完全兼容. + > + > linux不同的发行版, 只是侧重点不同. 本质是包管理不同. + + - ubuntu + - 使用apt包管理工具 + - 对图形界面支持更加友好 + - 社区活跃, 更新更加积极 + - 更加侧重个人用户 + - centos + - 使用yum包管理工具 + - 对底层硬件的兼容性更好 + - 使用yum包管理工具 + + - mac os + + > mac os可以看作和unix是继承关系, 完全符合unix规范. + +- Windows + + > 始于80年代, 强大的图形界面操作逻辑. 对用户更加友好. + > + > + + + +win10安装linux + +在Win10中安装Ubuntu https://jingyan.baidu.com/article/ae97a64617a90bbbfd461d80.html + +``` +https://jingyan.baidu.com/article/ae97a64617a90bbbfd461d80.html +``` + +# 为什么使用linux做服务器 + +- linux使用费用低 +- linux兼容性更好 +- linux是开源的 +- linux社区活跃 +- 很多新技术都是优先适配linux, 之后再移植到windows和mac os. +- linux管理员拥有实际意义上的最高权限, windows和mac os很多时候没有办法修改 + +# 远程连接linux服务器 + +1. SSH连接 +2. 公钥登录 + + + +- SSH连接 + + - 什么是SSH + + SSH是一种网络协议, 用于计算机之间的加密登录 + + ```shell + // 使用ssh登录 + ssh user@host + + 具体的:ssh ubuntu@121.5.18.188 密码:mayugu2021. 问海哥 + tunan---> ubantu 对服务器进行操作; + ``` + + - ssh管理工具 + + - windows + + **mobaXterm** + + - Mac OS + + shellcraft + + - Linux + + 推荐公钥登录 + + - ssh原理 + + 利用了非对称加密, 客户端和服务端互相发送了对方的**公钥**. 在传输的过程中用**公钥**对数据进行加密, 得到加密后的数据再通过**私钥**进行解密. + + - ssh是绝对安全的吗? + + 是也不是, 通过ssh建立的连接一定是安全的. 但是有一个前提, 那就是第一次连接的时候需要确认连接服务器是安全的, 这个风险是由用户自己承担. + +- 公钥登录 + + > 实际上就是对发送的公钥进行持久化, 这样就不用每一次都互相交换公钥 + + - 生成私钥和公钥 + + ```shell + ssh-keygen + + # 查看当前公钥和私钥 + root@DESKTOP-PUHRNUT:~/.ssh# cd /root/.ssh + root@DESKTOP-PUHRNUT:~/.ssh# ls + ``` + + ```shell + sudo su + ssh-keygen + cd root + ls + ls -a + cd .ssh + ls + cat id_rsa.pub + + cd /root/.ssh + ls + exit 回到tunan + ``` + + ```shell + + ``` + + + + - 将公钥发送到服务端 + + ``` + ssh-copy-id user@host + ssh-copy-id ubantu@121.5.18.188 + 输入服务器端的密码; + # 第一次发送公钥需要输入验证码验证 + ``` + + - 取消公钥登录 + + > 实际上就是在服务端删除持久化的公钥 + + ``` + cd /home/用户/.ssh + cd home/ubantu/.ssh + + cd ubantu + ls -a + cd .ssh + ls + + # 删除对应客户端的公钥 + vim .ssh/authorized_keys + + 删除,或者将其变为空文件; + echo “” > authorized_keys + exit + connection closed………… + ``` + +# linux目录树 + +> linux没有像windows盘符这样的概念, 对于windows来说, 当前的根目录就是盘符. 而linux是/ +> +> 在linux中, 一切皆文件, 访问硬盘和访问文件夹对linux来说没有任何区别. +> +> rm -rf ./* 清除当前目录的所有文件 +> +> rm -rf /* 清除根目录的所有文件,清除整个系统的所有文件; +> +> + +cd / + +- / + + - bin + + 我们执行的命令其实都是运行的可执行文件. 系统的基础可执行文件都在该目录下. + + ls、cat、cd、 + + - sbin + + 跟/bin一致, 但是只有管理员权限才能执行 + + + + - boot + + 引导程序所在目录, 引导程序就是用来开机的时候引导进入系统, 多个系统就会有多个引导程序. + + - dev + + 设备文件目录, 将对设备文件的访问转变为对设备的访问. + + - etc + + 系统和程序配置文件存放的目录. + + .conf结尾; + + - home + + 一台机器可以有多个用户, 该用户配置和文件都存在home目录下 + + > 一般我们代码会存在/home/用户/自定义目录 下 + + - lib + + 存放系统共享库的目录 + + - sys + + 存放内核文件的目录 + + - var + + 存放增量数据的目录 + + > 日志一般都在/var/log 目录下 + + - usr(unix shared resources) + + - bin + + - sbin + + - lib + + - local + + 用户级别的程序目录, 用户自己编译的程序会默认安装到当前目录 + +代码在哪里? /home/用户/自定义目录 下 + +log在哪里? /var/log 目录下 + +配置文件在哪里? /etc/下 + +# 课后作业 + +- 安装linux服务连接管理工具 +- 学会在linux下远程连接linux服务器 +- 学会公钥登录 \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/\347\224\250apt\350\277\233\350\241\214\345\214\205\347\256\241\347\220\206\343\200\201\346\226\207\344\273\266\346\223\215\344\275\234\343\200\201\344\277\256\346\224\271\346\235\203\351\231\220\343\200\201\346\226\207\346\234\254\346\223\215\344\275\234\343\200\201\347\212\266\346\200\201\346\237\245\347\234\213\343\200\201\347\241\254\344\273\266-\347\263\273\347\273\237\344\277\241\346\201\257-17-2-noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/\347\224\250apt\350\277\233\350\241\214\345\214\205\347\256\241\347\220\206\343\200\201\346\226\207\344\273\266\346\223\215\344\275\234\343\200\201\344\277\256\346\224\271\346\235\203\351\231\220\343\200\201\346\226\207\346\234\254\346\223\215\344\275\234\343\200\201\347\212\266\346\200\201\346\237\245\347\234\213\343\200\201\347\241\254\344\273\266-\347\263\273\347\273\237\344\277\241\346\201\257-17-2-noteyun.md" new file mode 100644 index 00000000..b5b4b199 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week17/\347\224\250apt\350\277\233\350\241\214\345\214\205\347\256\241\347\220\206\343\200\201\346\226\207\344\273\266\346\223\215\344\275\234\343\200\201\344\277\256\346\224\271\346\235\203\351\231\220\343\200\201\346\226\207\346\234\254\346\223\215\344\275\234\343\200\201\347\212\266\346\200\201\346\237\245\347\234\213\343\200\201\347\241\254\344\273\266-\347\263\273\347\273\237\344\277\241\346\201\257-17-2-noteyun.md" @@ -0,0 +1,202 @@ +17-2-noteyun + + + +# 用apt进行包管理 + +> 包管理需要root权限, sudo su + +- 安装 + + ``` + # 初次使用apt命令需要执行apt-get update更新源 + apt-get install python3-pip + ``` + +- 换源 + + > 由于各种各样的网络原因, 我们需要更换我们包管理的源. 换一个速度更快的镜像源 + + - 阿里源 + + ``` + https://developer.aliyun.com/mirror/ + ``` + + - 中科大源 + + ``` + http://mirrors.ustc.edu.cn/help/ubuntu.html + ``` + + - 配置源 + + - 备份 + + ``` + cp /etc/apt/sources.list /etc/apt/sources.list.bk + ``` + + - 清空`sources.list` + + ``` + echo "" > /etc/apt/sources.list + ``` + + - 添加配置 + + 一定要注意配置的版本 + + ``` + vim /etc/apt/sources.list + ``` + +- 卸载 + + ``` + apt-get remove python3-pip + apt-get autoremove python3-pip // 自动卸载依赖和配置, 卸载地更干净 + ``` + +- 更新 + + 更新就是安装指定的版本 + + ``` + apt-get install redis-server=5:4.0.9-1 + ``` + +- 搜索包 + + ``` + apt-cache madison redis-server + ``` + +# 文件操作 + +- 切换目录 + + ``` + cd /目录/次级目录 + cd .. 返回上级目录 + ``` + +- Create + + - 创建文件 + + ``` + > /目录/文件名 + ``` + + - 创建目录 + + ``` + mkdir test_dir + ``` + + - 复制文件 + + ``` + cp 文件路径 目标路径 + ``` + + - 复制目录 + + ``` + cp -r 目录路径 目标路径 + ``` + +- Retrieve + + - 显示目录下的文件和目录 + + ``` + ls 目录 // 默认目录为. 也就是当前目录 + ls -a // 显示隐藏文件和目录 + ls -a -l // 显示文件和目录详情 + ``` + + - 匹配文件或者目录 + + > 涉及到管道符号`|`, 它可以接收上一条指令的结果作为参数处理 + > + > grep(global regular expression and print out the line)全局正则搜索并输出到控制台 + + ``` + ls | grep *_dir + ``` + + - 排序 + + - 根据时间排序 + + ``` + ls -l -t + ``` + + - 根据大小排序 + + ``` + ls -l -s + ``` + + - 搜索查询 + + > 可以使用ls -R做简单的递归搜索, 但是有更专业的命令find + + - 根据关键词搜索 + + ``` + find 路径 -name "regex" + find 路径 -name "*py" + ``` + + - 根据类型来进行搜索 + + ``` + find 路径 -type d + find 路径 -type f + ``` + + - Update + + - 移动文件或者目录 + + ``` + mv 文件路径/目录路径 目标路径 + + mv test_dir test_dir2 + ``` + + - Delete + + - 删除文件 + + ``` + rm -f 文件路径 // -f 表示强制移除, force + + rm -f new_test.py + ``` + + - 删除目录 + + ``` + rm -rf test_dir // -r 表示递归, recursive + ``` + + - 删除匹配的文件或者目录 + + ``` + rm -rf *.py + ``` + +# 修改权限 + +# 文本操作 + +# 状态查看 + +# 硬件/系统信息 + +# 课后作业 \ No newline at end of file -- Gitee