LNMP分布式劇本
- 一:環境設置
- 二:編寫Nginx劇本
- 準備nginx下載源
- 準備配置文件并開放PHP的訪問路徑
- 準備php測試頁面
- 編寫nginx劇本
- 三:編寫Mysql劇本
- 編寫密碼獲取腳本
- 準備Mysql的yum源
- 編寫mysql劇本
- 四:準備PHP劇本
- 準備兩個配置文件
- 編寫php劇本
一:環境設置
主機 | 部署應用 |
---|---|
192.168.52.100 | ansible |
192.168.52.110 | nginx |
192.168.52.120 | mysql |
192.168.52.130 | php |
二:編寫Nginx劇本
準備nginx下載源
mkdir -p /etc/ansible/playbook/nginx
cd /etc/ansible/playbook/nginx
vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
準備配置文件并開放PHP的訪問路徑
vim default.conf
...
location / {root /usr/share/nginx/html;index index.html index.htm index.php;# 添加Nginx.php匹配項}
....
location ~ \.php$ {root html;fastcgi_pass 192.168.52.130:9000; #執行php的服務器和端口fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;include fastcgi_params;
準備php測試頁面
vim /etc/ansible/playbook/nginx/index.php
<?php
phpinfo();
?>
編寫nginx劇本
vim /etc/ansible/playbook/nginx/nginx.yml
- name: LAMP nginxhosts: webserversremote_user: roottasks:- name: stop firewalld #關閉防火墻service: name=firewalld state=stopped enabled=no- name: stop selinux #關閉selinuxcommand: '/usr/sbin/setenforce 0'ignore_errors: true- name: nginx.repo #準備Nginx的yum源copy: src=/etc/ansible/playbook/nginx/nginx.repo dest=/etc/yum.repos.d/nginx.repo- name: install nginx #下載nginxyum: name=nginx- name: start nginx #啟動Nginxservice: name=nginx state=started enabled=yes- name: copy nginx.conf #修改配置文件copy: src=/etc/nginx/conf.d/default.conf dest=/etc/nginx/conf.d/default.confnotify: "restart nginx" #指定觸發器- name: index.php #準備網頁測試王建copy: src=/etc/ansible/playbook/nginx/index.php dest=/usr/share/nginx/htmlhandlers:- name: restart nginx #觸發器任務,重啟Nginxservice: name=nginx state=restarted
ansible-playbook nginx.yml
三:編寫Mysql劇本
編寫密碼獲取腳本
mkdir -p /etc/ansible/playbook/mysql
vim /etc/ansible/playbook/mysql/passwd.sh#!/bin/bash
#獲取Mysql的密碼
passd=$(grep "password" /var/log/mysqld.log | awk '{print $NF}' | head -1)
#更改密碼
mysql -uroot -p"$passd" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"
#授權
mysql -uroot -pAdmin@123 -e "grant all privileges on *.* to root@'%' identified by 'Admin@123' with grant option;
準備Mysql的yum源
sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo
編寫mysql劇本
vim /etc/ansible/playbook/mysql/mysql.yml
- name: LAMP mysqlhosts: mysqlremote_user: roottasks:- name: stop firewalldservice: name=firewalld state=stopped enabled=no- name: stop selinuxcommand: '/usr/sbin/setenforce 0'ignore_errors: true- name: install mysql.repo #轉變mysql瞎子啊源shell: wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm && rpm -ivh mysql57-community-release-el7-11.noarch.rpmignore_errors: true- name: mysql.repo #修改yum源,把倉庫打開copy: src=/etc/yum.repos.d/mysql-community.repo dest=/etc/yum.repos.d/mysql-community.repo- name: install mysql #下載mysqlyum: name=mysql-server- name: start msql #啟動mysqlservice: name=mysqld state=started enabled=yes- name: grep passwd #指定修改密碼腳本,修改密碼并授權script: /etc/ansible/playbook/mysql/passwd.sh
ansible-playbook mysql.yml
四:準備PHP劇本
準備兩個配置文件
php.ini
#添加修改時時區
date.timezone = Asia/Shanghai
www.conf文件
user = php
group = php
listen = 192.168.52.130:9000
listen.allowed_clients = 192.168.52.110
編寫php劇本
vim //etc/ansible/playbook/php.yml
- name: LAMP nginxhosts: dbserversremote_user: roottasks:- name: stop firewalldservice: name=firewalld state=stopped enabled=no- name: stop selinuxcommand: '/usr/sbin/setenforce 0'ignore_errors: true- name: install php1 #準備php下載源shell: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpmignore_errors: true- name: install php2 #下載PHP及依賴包shell: yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcacheignore_errors: true- name: start php #開啟phpservice: name=php-fpm state=started enabled=yes- name: user php #創建運行用戶user: name=php create_home=no shell=/sbin/nologin- name: php.ini #修改配置文件copy: src=/etc/ansible/playbook/php.ini dest=/etc/php.ini- name: www.confcopy: src=/etc/ansible/playbook/www.conf dest=/etc/php-fpm.d/www.conf- name: create nginxfile: name=/usr/share/nginx state=directory- name: create nginxfile: name=/usr/share/nginx/html state=directory- name: index.php #準備測試頁面copy: src=/etc/ansible/playbook/nginx/index.php dest=/usr/share/nginx/html
ansible-playbook php.yml