playbook自动化安装lamp

标签:自动化   libc   cgi   task   port   end   configure   映射文件   bzip   

准备4台主机,其中一台装ansible,其余三台分别部署apache、mysql、php,实现lamp架构

主控机ip:192.168.170.20   wang   ansible

受控机ip:192.168.170.134   apache   apache

                192.168.170.135  mysql  mysql

                192.168.170.136  php   php

[root@wang ansible]# tree .
.
├── ansible.cfg
├── apache
│   ├── apache.sh
│   ├── apache.yml 
├── hosts
├── inventory
├── mysql
│   ├── mysql.yml
│   └── mysql.sh
├── php
│   ├── php.sh
│   └── php.yml
└── roles

playbook安装apache

安装apache
编辑脚本,利用playbook下载apache,以及依赖包
[root@wang apache]# cat apache.sh
#!/bin/bash
wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz /root
wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz /root
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz
[root@wang apache]# cat apache.yml 
---
- name: bash
  gather_facts: no
  hosts: apache
  tasks:
    - name: bash
      script: /etc/ansible/apache/apache.sh
[root@apache ~]# ls
anaconda-ks.cfg   httpd-2.4.46.tar.gz
apr-1.6.5.tar.gz
创建用户
---
- hosts: apache
  gather_facts: no
  tasks:
   - name: create user
     user:
       name: apache
       state: present
[root@wang apache]# vim apache.yml
---
- hosts: all
  vars:
    package: bzip2,make,openssl-devel,pcre-devel,expat-devel,libtool,gcc,gcc-c++,libxml2-devel
  gather_facts: no
  tasks:
   - name: install development tools
     yum:
       name: "@Development tools"
       state: present
  tasks:
   - name: install
     yum:
       name: "{{ package }}"
       state: present
  tasks:
   - name: bash
     script: /etc/ansible/apache/apache.sh
脚本内容
[root@wang apache]# cat apache.sh 
#!/bin/bash
#配置apr脚本
tar xf apr-1.6.5.tar.gz
tar xf apr-util-1.6.1.tar.gz
tar xf httpd-2.4.46.tar.gz
cd /root/apr-1.6.5/
sed -i s|$RM "$cfgfile"|# $RM "$cfgfile"| /root/apr-1.6.5/configure
 ./configure --prefix=/usr/local/apr
make && make install
#配置apr-util脚本
cd /root/apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
#配置apache脚本
cd /root/httpd-2.4.46 && ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make && make install
#配置httpd的config文件
cd /root/httpd-2.4.46
#设置环境变量
echo export PATH=/usr/local/apache/bin:$PATH > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
#映射文件
ln -s /usr/local/apache/include/ /usr/include/httpd
#设置帮助文档
echo MANPATH /usr/local/apache/man >> /etc/man.config
#关闭警告信息
sed -i /#ServerName/s/#//g /etc/httpd24/httpd.conf
#启动模块
sed -i /proxy_module/s/#//g /etc/httpd24/httpd.conf
sed -i /proxy_fcgi_module/s/#//g /etc/httpd24/httpd.conf
#编辑配置文件
sed -i /    DirectoryIndex/s/index.html/index.php index.html/g /etc/httpd24/httpd.conf
sed -i s|AddType application/x-gzip .gz .tgz|AddType application/x-gzip .gz .tgz\n    AddType application/x-httpd-php .php\n    AddType application/x-httpd-php-source .phps| /etc/httpd24/httpd.conf
#加入文件内容
echo -e <VirtualHost *:80>\n    DocumentRoot "/usr/local/apache/htdocs/"\n    ServerName www.wangming.com\n    ProxyRequests Off\n    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.170.134:9000/var/www/html/$1\n    <Directory "/usr/local/apache/htdocs/">\n        Options none\n        AllowOverride none\n        Require all granted\n    </Directory>\n</VirtualHost> >> /etc/httpd24/httpd.conf
#启动服务
apachectl restart
#关闭防火墙
systemctl stop firewalld.service 
setenforce 0
[root@apache apr-1.6.5]# ss -antl
State Recv-Q Send-Q  Local Address:Port  Peer Address:Port 
LISTEN0      128           0.0.0.0:22         0.0.0.0:*    
LISTEN0      128              [::]:22            [::]:*    
LISTEN0      128                 *:80               *:*  
 

安装mysql

[root@wang mysql]# cat mysql.yml 
---
- hosts: mysql
  tasks:
    - name: install
      dnf:
        name: ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel
       stae: present
    - name: create mysql
      user:
        name: mysql
        system: yes
        create_home: no
        shell: /sbin/nologin
        state: present
  tasks:
    - name: copy
      copy:
        src:/root/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
      dest: /root/
  tasks:
    - name: man config
      lineinfile:
        path: /etc/man_db.conf
        line: MANDATORY_MANPATH     /usr/local/mysql/man
  tasks:
    - name: bash
      script: /etc/ansible/mysql/.mysql.sh
脚本内容
[root@wang mysql]# cat .mysql.sh 
#/bin/bash
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
#以防数据库依赖包缺少,提前安装
yum -y  install libncurses*
#添加映射文件
ln -sv mysql-5.7.31-linux-glibc2.12-x86_64 mysql
#修改属主属组
chown -R mysql.mysql ~/mysql
#设置环境变量
echo export PATH=~/mysql/bin:$PATH >/etc/profile.d/mysql.sh 
. /etc/profile.d/mysql.sh
#创建MySQL文件目录,修改属性
mkdir /opt/data
chown -R mysql.mysql /opt/mysql
#初始化数据库
 /root/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ > /root/mima 2>&1
#添加映射文件
ln -sv ~/mysql/include/ /usr/local/include/mysql
echo /root/mysql/lib > /etc/ld.so.conf.d/mysql.conf
ldconfig
echo [mysqld]
basedir = /root/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve > /etc/my.cnf
#启动服务脚本
cp -a /root/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri s#^(basedir=).*#\1/root/mysql#g /etc/init.d/mysqld
sed -ri s#^(datadir=).*#\1/opt/data#g /etc/init.d/mysqld
#启动服务
service mysql start
#关闭防火墙
systemctl stop firewalld.service
etenforce 0
#提取并修改密码
/root/mysql/bin/mysql -uroot --p"$(awk  ‘/password/{print$NF}‘ /root/mima)"  --connect-expired-password -e "set password = password(\"990304\");"
[root@mysql ~]# ss -antl
State Recv-Q Send-Q  Local Address:Port  Peer Address:Port
LISTEN0      128           0.0.0.0:22         0.0.0.0:*   
LISTEN0      128              [::]:22            [::]:*   
LISTEN0      80                  *:3306             *:*   

 

 

 

安装php

[root@wang php]# cat php.yml 
---
- hosts: php
  tasks:
     - name: install
       yum:
          name: php*
          state: present
     - name: install
       yum:
          name: "Development Tools"
          state: present
     - name: libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libicu-devel,libjpeg,libjpeg-devel,libpng,libpng-devel,openldap-devel,pcre-devel,freetype,freetype-devel,gmp,gmp-devel,libmcrypt,libmcrypt-devel,readline,readline-devel,libxslt,libxslt-devel,mhash,mhash-devel,php-mysqlnd
       state: present
     - name: bash
       script: /root/ansible/php/php.sh
     - name: start php
       service:
          name: php-fpm
          state: restarted
脚本内容
[root@wang ansible]# cat php/php.sh 
#!/bin/bash
#创建测试页面
echo -e "<?php\n\tphpinfo();\n?>" > /var/www/html/index.php
chown -R apache.apache /var/www/html/
#修改配置文件 ,设置所有端口可访问,并加入apacheip
sed -i s|listen = /run/php-fpm/www.sock|listen=0.0.0.0:9000| /etc/php-fpm.d/www.conf
sed -i s|127.0.0.1|192.168.170.134| /etc/php-fpm.d/www.conf

#关闭防火墙
systemctl stop firewalld.service
etenforce 0
[root@php ~]# ss -antl
State Recv-Q Send-Q  Local Address:Port  Peer Address:Port 
LISTEN0      128           0.0.0.0:22         0.0.0.0:*    
LISTEN0      128           0.0.0.0:9000       0.0.0.0:*    
LISTEN0      128              [::]:22            [::]:*    

 

技术图片

 

playbook自动化安装lamp

标签:自动化   libc   cgi   task   port   end   configure   映射文件   bzip   

原文地址:https://www.cnblogs.com/wangming/p/14253132.html

版权声明:完美者 发表于 2021-01-13 10:47:52。
转载请注明:playbook自动化安装lamp | 完美导航

暂无评论

暂无评论...