加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Django连接nginx反向代理

发布时间:2020-12-20 10:56:51 所属栏目:Python 来源:网络整理
导读:在安装 python3的情况,我们先创建一下django的虚拟环境,在/opt下创建, [[email?protected] opt] # python3 -m venv d jango 进入虚拟环境 [[email?protected] opt]# source /opt/django/bin/activate 然后我们来安装 django1.11版本,安装最新版本会报错 (

在安装python3的情况,我们先创建一下django的虚拟环境,在/opt下创建,

[[email?protected] opt]# python3 -m venv django

进入虚拟环境

[[email?protected] opt]# source /opt/django/bin/activate

然后我们来安装django1.11版本,安装最新版本会报错

(django) [[email?protected] opt]# pip install ‘django<1.12‘

(django) [[email?protected] opt]# pip install pymysql

(django) [[email?protected] opt]# pip install ipython

然后我创建一个目录

(django) [[email?protected] ~]# mkdir /myproject

进入到里面创建项目,项目的名字为demo,它里面的demo应用,他的名字会和项目名字一样,比如改成www,它里面的应用也会为www

(django) [[email?protected] ~]# cd /myproject/

(django) [[email?protected] ~]# django-admin startproject demo

进入到项目里创建一个app应用

(django) [[email?protected] myproject]# cd demo/

(django) [[email?protected] demo]# python3 manage.py startapp blog

(django) [[email?protected] demo]# ls

blog ?demo ?manage.py

创建完之后django不知道我们创建了blog应用,我们把他加入到应用里

(django) [[email?protected] demo]# vim demo/settings.py

?

?

?

?

首先我们在demo项目下的demo应用下去添加urls.py的文本

(django) [[email?protected] demo]# ls

__init__.py ?__pycache__ ?settings.py ?urls.py ?wsgi.py

(django) [[email?protected] demo]# vim urls.py

?

再去刚才创建的blog应用下创建视图views.py,这样是测试用的输出字符串

(django) [[email?protected] demo]vim blog/views.py

?

修改完之后我们再去demo应用里创建templates渲染目录下创建index.html

(django) [[email?protected] demo]# mkdir templates

(django) [[email?protected] demo]# cd templates/

?

进入到渲染的目录里去创建index.html

(django) [[email?protected] templates]# vim index.html

?

代码如下:可以直接复制粘贴?

<!DOCTYPE HTML>

<html>

??<head>

?????<meta charset=‘utf8‘/>

?????<title>demo</title>

??</head>

<body>

??<h1>Hello World</h1>

</body>

</html>

写完我们在去修改setting设置把templates渲染加进去

(django) [[email?protected] demo]# vim demo/settings.py

?

我们再来开启django

(django) [[email?protected] demo]# ./manage.py runserver 0.0.0.0:8888(不成功关闭防火墙)

?

然后我在192.168.1.10这一台安装ansible去控制192.168.1.20安装nginx

[[email?protected] ~]# yum -y install ansible

我们还是先来准备实现工作

还是先做ssh免密登录

[[email?protected] ~]# ssh-keygen -t rsa

[[email?protected] ~]# ssh-copy-id [email?protected]

免密做完我们在去设置配置文件和添加资产

[[email?protected] ~]# vim /etc/ansible/ansible.cfg

?

[[email?protected] ~]# vim /etc/ansible/hosts ??这里资产多了一个192.168.1.30不用管

?

然后我们把nginx的包php包依赖包拖进去拖进去

先解压

[[email?protected] ~]# tar zxf nginx-1.14.0.tar.gz

然后把配置文件拷贝出来

[[email?protected] ~]# cp nginx-1.14.0/conf/nginx.conf /root/

然后我们修改他的配置文件

[[email?protected] ~]# vim nginx.conf

[[email?protected] ~]# vim nginx.sh

useradd -M -s ?/sbin/nologin nginx

cd /root/nginx-1.14.0

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-pcre

make && make install

cd? ?/root

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin(软连接)

nginx(启动)

我们在来编写roles(角色)

我们去/etc/ansible/roles/

[[email?protected] ~]# cd /etc/ansible/roles/

[[email?protected] roles]# ls

[[email?protected] roles]# mkdir nginx

[[email?protected] roles]# ls

nginx ?

[[email?protected] roles]# cd nginx/

[[email?protected] nginx]# mkdir tasks

[[email?protected] nginx]# ls

tasks

[[email?protected] nginx]# cd tasks/

我们在编写nginx.yml

[[email?protected] tasks]# vim nginx.yml

- name: fu zhi bao

??copy: src=/root/nginx-1.14.0.tar.gz dest=/root

- name: jie ya bao

??command: tar zxf /root/nginx-1.14.0.tar.gz

- name: yum yi laibao

??yum: name=pcre-devel,zlib-devel,openssl-devel,gcc state=present

- name: fu zhi nginx jiao ben

??copy: src=/root/nginx.sh dest=/root

- name: zhi xing nginx jiao ben

??shell: bash /root/nginx.sh

- name: ba nginx file fu zhi gei 1.20

??copy: src=/root/nginx.conf dest=/usr/local/nginx/conf/

- name: chong qi nginx

??shell: nginx -s stop

- name: kai qi

??shell: nginx

- name: firewalld jia kai duankou

??shell: firewall-cmd --add-port=80/tcp --permanent

- name: ‘9000‘

??shell: firewall-cmd --add-port=9000/tcp --permanent

- name: chong zai

??shell: firewall-cmd --reload

在编写main.yml的进行引用

[[email?protected] tasks]# vim main.yml

- include: nginx.yml

[[email?protected] roles]# vim lnmp.yml

????---

- hosts: 192.168.1.20

??????roles:

????????- nginx

最后可以访问了,实验完毕

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读