1. 程式人生 > 實用技巧 >playbook自動化安裝lamp

playbook自動化安裝lamp

準備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            [::]:*