Django鏈接nginx反向代理

在安裝python3的狀況,咱們先建立一下django的虛擬環境,在/opt下建立,php

[root@localhost opt]# python3 -m venv djangohtml

進入虛擬環境python

[root@localhost opt]# source /opt/django/bin/activatemysql

而後咱們來安裝django1.11版本,安裝最新版本會報錯nginx

(django) [root@localhost opt]# pip install 'django<1.12'sql

(django) [root@localhost opt]# pip install pymysqlshell

(django) [root@localhost opt]# pip install ipythondjango

而後我建立一個目錄vim

(django) [root@localhost ~]# mkdir /myprojectbash

進入到裏面建立項目,項目的名字爲demo,它裏面的demo應用,他的名字會和項目名字同樣,好比改爲www,它裏面的應用也會爲www

(django) [root@localhost ~]# cd /myproject/

(django) [root@localhost ~]# django-admin startproject demo

進入到項目裏建立一個app應用

(django) [root@localhost myproject]# cd demo/

(django) [root@localhost demo]# python3 manage.py startapp blog

(django) [root@localhost demo]# ls

blog  demo  manage.py

建立完以後django不知道咱們建立了blog應用,咱們把他加入到應用裏

(django) [root@localhost demo]# vim demo/settings.py

 

 

 

 

首先咱們在demo項目下的demo應用下去添加urls.py的文本

(django) [root@localhost demo]# ls

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

(django) [root@localhost demo]# vim urls.py

 

再去剛纔建立的blog應用下建立視圖views.py,這樣是測試用的輸出字符串

(django) [root@localhost demo]vim blog/views.py

 

修改完以後咱們再去demo應用裏建立templates渲染目錄下建立index.html

(django) [root@localhost demo]# mkdir templates

(django) [root@localhost demo]# cd templates/

 

進入到渲染的目錄裏去建立index.html

(django) [root@localhost 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) [root@localhost demo]# vim demo/settings.py

 

咱們再來開啓django

(django) [root@localhost demo]# ./manage.py runserver 0.0.0.0:8888(不成功關閉防火牆)

 

而後我在192.168.1.10這一臺安裝ansible去控制192.168.1.20安裝nginx

[root@localhost ~]# yum -y install ansible

咱們仍是先來準備實現工做

仍是先作ssh免密登陸

[root@ansible-server ~]# ssh-keygen -t rsa

[root@ansible-server ~]# ssh-copy-id root@192.168.1.20

免密作完咱們在去設置配置文件和添加資產

[root@ansible-server ~]# vim /etc/ansible/ansible.cfg

 

[root@ansible-server ~]# vim /etc/ansible/hosts   這裏資產多了一個192.168.1.30不用管

 

而後咱們把nginx的包php包依賴包拖進去拖進去

先解壓

[root@ansible-server ~]# tar zxf nginx-1.14.0.tar.gz

而後把配置文件拷貝出來

[root@ansible-server ~]# cp nginx-1.14.0/conf/nginx.conf /root/

而後咱們修改他的配置文件

[root@ansible-server ~]# vim nginx.conf

[root@ansible-server ~]# 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/

[root@ansible-server ~]# cd /etc/ansible/roles/

[root@ansible-server roles]# ls

[root@ansible-server roles]# mkdir nginx

[root@ansible-server roles]# ls

nginx  

[root@ansible-server roles]# cd nginx/

[root@ansible-server nginx]# mkdir tasks

[root@ansible-server nginx]# ls

tasks

[root@ansible-server nginx]# cd tasks/

咱們在編寫nginx.yml

[root@ansible-server 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的進行引用

[root@ansible-server tasks]# vim main.yml

- include: nginx.yml

[root@ansible-server roles]# vim lnmp.yml

    ---

- hosts: 192.168.1.20

      roles:

        - nginx

最後能夠訪問了,實驗完畢

相關文章
相關標籤/搜索