在安装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
最后能够访问了,实验完毕