在安裝python3的情況,我們先創建一下django的虛擬環境,在/opt下創建,
[root@localhost opt]# python3 -m venv django
進入虛擬環境
[root@localhost opt]# source /opt/django/bin/activate
然后我們來安裝django1.11版本,安裝最新版本會報錯
(django) [root@localhost opt]# pip install 'django<1.12'
(django) [root@localhost opt]# pip install pymysql
(django) [root@localhost opt]# pip install ipython
然后我創建一個目錄
(django) [root@localhost ~]# mkdir /myproject
進入到里面創建項目,項目的名字為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
最后可以訪問了,實驗完畢