ansible整机编排lnmp

ansible部署单机lnmp

编写lnmp的yaml文件

- name: nginx
  hosts: dbservers
  remote_user: root
  gather_facts: false
  tasks:
    - name: check mount
      shell: df -h | grep /dev/sr0 || mount /dev/sr0 /mnt
    - name: test connection
      ping:
    - name: disable seliux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: set yum
      copy: src=/opt/nginx.repo dest=/etc/yum.repos.d/nginx.repo
      notify:
        - restart nginx
    - name: install nginx
      yum: name=nginx state=latest
    - name: start nginx service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted
- name: mysql
  gather_facts: false
  hosts: dbservers
  remote_user: root
  tasks:
    - name: test connection
      ping:
    - name: disable seliux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: remove mariadb
      yum: name=mariadb* state=absent
    - name: wget
      command: wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    - name: install mysql
      yum: name=mysql57-community-release-el7-10.noarch.rpm
    - name: install mysql
      yum: name=mysql-community-server state=latest
    - name: start mysql service
      service: name=mysqld state=started enabled=yes
    - name: modify password                                   #获取mysql的登录密码并进行更改,需要大小写字母加符号和数字的组合
      shell: mysqladmin -u root -p"$(grep "password" /var/log/mysqld.log | awk 'NR==1{print $NF}')" password "Admin@123"
    - name: shouquan                                          #授予登录权限
      shell: mysql -uroot -p'Admin@123' -e "grant all privileges on *.* to root@'%' identified by 'Admin@123' with grant option;" -e "flush privileges;"
    - name: remove mysql57
      yum: name=mysql57-community-release-el7-10.noarch state=absent
- name: php
  gather_facts: false
  hosts: dbservers
  remote_user: root
  tasks:
    - name: rpm three
      shell: rpm -Uvh http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libargon2-20161029-3.el7.x86_64.rpm && 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
    - name: install 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-opcache php72w-redis
    - name: start php
      service: name=php-fpm state=started enabled=true
    - name: nginx support php
      copy: src=/opt/default.conf dest=/etc/nginx/conf.d/default.conf
      notify:
        - restart nginx
    - name: modify index.php
      copy: src=/opt/index.php dest=/usr/share/nginx/html/index.php
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

准备index.php、nginx.repo、default.conf

vim /opt/index.php  #用来测试php连接mysql
<?php
$link=mysqli_connect('192.168.80.13','root','Admin@123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
============================================================
vim /opt/nginx.repo   #nginx源

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
============================================================
vim /opt/default.conf    #让nginx与php对接
   location ~ \.php$ {                                 #29行开始修改
         root           html;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;     
         include        fastcgi_params;
      }

img
img
img

浏览器测试

http://192.168.80.13/index.php

img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Ansible分离部署LNMP可以让你更轻松地管理和部署LNMP服务器。以下是一些基本步骤: 1. 安装Ansible 2. 创建Ansible inventory文件,包含LNMP服务器的IP地址。 3. 创建Ansible playbook文件,指定要安装的软件包。 4. 在Ansible playbook文件中定义任务,例如安装Nginx、MySQL和PHP。 5. 运行Ansible playbook文件,Ansible将自动在LNMP服务器上安装和配置软件。 下面是一个可能的示例Ansible playbook文件: ``` --- - hosts: lnmp_servers become: true vars: nginx_version: 1.18.0 mysql_version: 5.7 php_version: 7.4 tasks: - name: Install Nginx yum: name: nginx-{{ nginx_version }} state: present - name: Start Nginx service: name: nginx state: started - name: Install MySQL yum: name: mysql-server-{{ mysql_version }} state: present - name: Start MySQL service: name: mysqld state: started - name: Install PHP yum: name: php-{{ php_version }} php-fpm-{{ php_version }} state: present - name: Start PHP-FPM service: name: php-fpm state: started ``` 在这个示例中,我们假设我们有一个名为"lnmp_servers"的Ansible组,其中包含我们要部署LNMP的服务器的IP地址。此外,我们定义了要安装的软件包的版本号,并在任务中安装和启动Nginx、MySQL和PHP。 运行Ansible playbook文件的命令是: ``` ansible-playbook -i inventory_file playbook_file.yml ``` 其中,inventory_file是你的Ansible inventory文件的路径,playbook_file.yml是你的Ansible playbook文件的路径。 这就是使用Ansible分离部署LNMP的基本步骤。你可以根据你的具体需求进行修改和定制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值