3.yum安装分布式LNMP--剧本

修改hosts

vim /etc/ansible/hosts

[webservers]
192.168.242.67

[dbservers]
192.168.242.68

[phpservers]
192.168.242.69

创建剧本文件

vim lnmp.yaml
- name: nginx play
  hosts: webservers
  remote_user: root
  vars:
  - http_port: 192.168.242.67:80
  - http_hostname: www.ggl.com
  - root_dir: /usr/share/nginx/html
  - http_remote: 192.168.242.69:9000
  - service: nginx

  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true
  
  - name: copy nginx.repo
    copy: src=nginx.repo dest=/etc/yum.repos.d/

  - name: install nginx
    yum: name={{service}} state=latest

  - name: copy index.php
    copy: src=index.php dest={{root_dir}}

  - name: copy nginx.conf
    template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
    notify: reload nginx

  - name: start nginx
    service: name={{service}} state=started enabled=yes
  handlers:
  - name: reload nginx
    service: name={{service}} state=reloaded
    
- name: mysql play
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true

  - name: yum remove mariadb
    yum: name=mariadb* state=absent

  - name: install rpm
    shell: wget -P /etc/yum.repos.d/ http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    ignore_errors: true

  - name: install noarch.rpm
    shell: yum -y install /etc/yum.repos.d/mysql57-community-release-el7-10.noarch.rpm
    ignore_errors: true

  - name: replace wprd
    replace: path=/etc/yum.repos.d/mysql-community.repo regexp='gpgcheck=1' replace='gpgcheck=0'
    ignore_errors: true

  - name: install mysql
    yum: name="mysql-server"

  - name: start mysqld
    service: name=mysqld state=started enabled=yes


  - name: login mysql
    shell: mysql -uroot -p"$(grep "password" /var/log/mysqld.log | awk 'NR==1 {print $NF}')" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"

  - name: grantmysql
    shell: mysql -uroot -pAdmin@123 -e "grant all privileges on *.* to root@'%' identified by 'Admin@123' with grant option;"


- name: php play
  hosts: phpservers
  remote_user: root
  tasks:
  vars: 
  - service: php-fpm
  - timezone: Asia/Shanghai
  - user_name: php
  - http_port: 192.168.242.69:9000
  - remote_addr: 192.168.242.67
  - root_dir: /usr/share/nginx/html
  - mysql_dir: /var/lib/mysql/mysql.sock
  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true
    
  - name: install yum repo
    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.rpm"
    ignore_errors: true

  - name: install php
    with_items:
    - php72w
    - php72w-cli
    - php72w-common
    - php72w-devel
    - php72w-embedded
    - php72w-gd
    - php72w-mbstring
    - php72w-pdo
    - php72w-xml
    - php72w-fpm
    - php72w-mysqlnd
    - php72w-opcache
    yum: name={{item}}

  - name: create php user
    user: name={{user_name}}

  - name: create php group
    group: name={{user_name}}

  - name: create web dir
    file: name={{root_dir}} state=directory


  - name: copy index.php
    copy: src=index.php dest={{root_dir}}


  - name: modify php.ini
    template: src=php.ini.j2 dest=/etc/php.ini
    notify: reload php

  - name: copy www.conf
    copy: src=www.conf dest=/etc/php-fpm.d/www.conf

  - name: start php
    service: name={{service}} state=started enabled=yes
  handlers:
  - name: reload php
    service: name={{service}} state=reloaded
                                                              
  
  
##在剧本的当前目录中,需要准备需要的文件

index.php
nginx.repo
default.conf.j2

www.conf
php.ini.j2
###  index.php

<?php
phpinfo();
?>
#  nginx.repo


[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
###修改www.conf文件


##主要修改几行配置文件

user = php
group = php
listen = 192.168.242.69:9000
listen.allowed_clients = 192.168.242.67
##  php.ini.j2  配置文件模板


###修改几行

1097行
mysqli.default_socket = {{mysql_dir}}

877行
date.timezone = {{timezone}}
##templates目录  中存放  nginx 的  j2  的配置文件模板

##修改几行

listen       {{http_port}};
server_name  {{http_hostname}};

root   {{root_dir}};


    location ~ \.php$ {
        root           {{root_dir}};
        fastcgi_pass   {{http_remote}};
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  {{root_dir}}$fastcgi_script_name;
        include        fastcgi_params;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜海赤竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值