Ansible自动化批量部署LNMP
nginx剧本
需要单独在主控端,写一个nginx启动脚本和php测试页面,用来传文件,nginx用copy,php用模板,用copy也行
[root@localhost ~]# vim nginx.yml
- hosts: dbserver
remote_user: root
tasks:
- name: install nginx
unarchive: src=/root/nginx-1.11.1.tar.gz dest=/usr/src
- name: yum install pcre* openssl*
yum: name=pcre-devel,openssl-devel,gcc,gcc-c++,zlib-devel
- name: make install nginx
# 以下表示进行shell操作时,先进入/usr/src/nginx-1.11.1目录
shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-pcre && make && make install
args:
chdir: /usr/src/nginx-1.11.1
- name: lnfile
file: src=/usr/local/nginx/sbin/nginx dest=/usr/local/sbin/nginx state=link
- name: create nginx user
user: name=nginx create_home=no shell=/sbin/nologin state=present
- name: nginx start script
copy: src=/root/nginx dest=/etc/init.d/nginx
- name: xp
file: path=/etc/init.d/nginx mode=0755
- name: add system service
shell: chkconfig --add nginx
- name: open system auto started
shell: systemctl enable nginx
- name: start nginx
service: name=nginx state=started
[root@localhost ~]# ansible-playbook nginx.yml
Mysql剧本
[root@localhost ~]# vim mysql.yml
- hosts: dbserver
remote_user: root
tasks:
- name: yum install ncurses-devel
yum: name=ncurses-devel
- name: install cmkae
unarchive: src=/root/cmake-2.8.7.tar.gz dest=/usr/src/
- name: make install cmake
shell: ./configure && gmake && gmake install
args:
chdir: /usr/src/cmake-2.8.7
- name: install mysql
unarchive: src=/root/mysql-5.5.22.tar.gz dest=/usr/src/
- name: make install mysql
shell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install
args:
chdir: /usr/src/mysql-5.5.22
- name: soft link
file: src=/usr/local/mysql/lib/libmysqlclient.so.18 dest=/usr/lib/libmysqlclient.so.18 state=link
- name: soft link
file: src=/usr/local/mysql/bin/mysql dest=/usr/bin/mysql state=link
- name: mysqldupm soft link
file: src=/usr/local/mysql/bin/mysqldump dest=/usr/bin/mysqldump state=link
- name: add user
user: name=mysql create_home=no shell=/sbin/nologin state=present
- name: mysql config file
copy: src=/usr/src/mysql-5.5.22/support-files/my-medium.cnf dest=/etc/my.cnf remote_src=yes
- name: mysql start script
copy: src=/usr/src/mysql-5.5.22/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes
- name: shou quan
file: path=/etc/init.d/mysqld mode=0755
- name: create system service
shell: chkconfig --add mysqld
- name: init database
shell: /usr/local/mysql/scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
- name: directory quanxian
file: path=/usr/local/mysql owner=mysql group=mysql recurse=yes
- name: start mysqld
service: name=mysqld state=started
- name: bainliang
shell: echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && source /etc/profile
[root@localhost ~]# ansible-playbook mysql.yml
php剧本
[root@localhost ~]# vim php.yml
- hosts: dbserver
remote_user: root
tasks:
- name: yum install gd libxml2-devel libjpeg-devel libpng-devel
yum: name=gd,libxml2-devel,libjpeg-devel,libpng-devel
- name: rz php tar
unarchive: src=/root/php-5.3.28.tar.gz dest=/usr/src
- name: make install php
shell: ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-config-file-path=/usr/local/php --enable-fpm --enable-mbstring --with-jpeg-dir=/usr/lib && make && make install
args:
chdir: /usr/src/php-5.3.28
- name: cp configfile
copy: src=/usr/src/php-5.3.28/php.ini-development dest=/usr/local/php/php.ini remote_src=yes
- name: alter primary configfile
replace: path=/usr/local/php/php.ini regexp='^default_charset' replace='default_charset = "utf-8"'
- name: alter primary configfile1
replace: path=/usr/local/php/php.ini regexp='^short_open_tag' replace='short_open_tag = On'
- name: install youhua
unarchive: src=/root/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz dest=/usr/src
- name: cp configfile
copy: src=/usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so dest=/usr/local/php/lib/php/ remote_src=yes
- name: edit configfile
shell: sed -i '$azend_extension=/usr/local/php/lib/php/ZendGuardLoader.so\nzend_loader.enable=1' /usr/local/php/php.ini
- name: cp php-fpm
copy: src=/usr/src/php-5.3.28/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes
- name: script php-fpm quanxian
file: path=/etc/init.d/php-fpm mode=0755
- name: add system service
shell: chkconfig --add php-fpm
- name: cp php-fpm configfile
copy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes
- name: edit php-fpm cfg max_children
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.max_children = 5' replace='pm.max_children = 50'
- name: edit php-fpm cfg star_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.start_servers = 2' replace='pm.start_servers = 20'
- name: edit php-fpm cfg min_spare_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.min_spare_servers = 1' replace='pm.min_spare_servers = 5'
- name: edit php-fpm cfg max_spare_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.max_spare_servers = 3' replace='pm.max_spare_servers = 35'
- name: edit php-fpm pid
replace: path=/usr/local/php/etc/php-fpm.conf regexp=';pid = run/php-fpm.pid' replace='pid = run/php-fpm.pid'
- name:
replace: path=/usr/local/php/etc/php-fpm.conf regexp='user = nobody' replace='user = nginx'
- name:
replace: path=/usr/local/php/etc/php-fpm.conf regexp='group = nobody' replace='group = nginx'
- name: start php-fpm
service: name=php-fpm state=started
[root@localhost ~]# ansible-playbook php.yml
整合php
[root@localhost ~]# vim zhphp.yml
- hosts: dbserver
remote_user: root
tasks:
- shell: sed -i '/ server_name localhost;/a \ location ~ \.php$ {\n root html;\n fastcgi_pass 127.0.0.1:9000;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;\n include fastcgi.conf;\n}' /usr/local/nginx/conf/nginx.conf
- replace: path=/usr/local/nginx/conf/nginx.conf regexp=' index index.html index.htm;' replace=' index index.html index.htm index.php;'
- service: name=nginx state=restarted
- file: path=/usr/local/nginx/html/index.php state=touch
[root@localhost ~]# ansible-playbook zhphp.yml
写验证文件
# 主控端创建模板文件
[root@localhost ~]# vim index.php.j2
<?php
$link=mysqli_connect('192.168.1.4','root');
if($link) echo "我爱你!!";
mysqli_close($link);
?>
[root@localhost ~]# vim phpfile.yml
- hosts: dbserver
remote_user: root
tasks:
- template: src=/root/index.php.j2 dest=/usr/local/nginx/html/index.php
[root@localhost ~]# ansible-playbook phpfile.yml
最后需要关闭防火墙或者放行 端口,数据库授权
LNMP PLAYBOOK
拖包:nginx、cmake、mysql、php、zend
# nginx脚本文件
[root@localhost ~]# vim nginx
#!/bin/bash
#chkconfig:- 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
# 主控端创建php验证模板文件
[root@localhost ~]# vim index.php.j2
<?php
$link=mysqli_connect('192.168.1.4','root');
if($link) echo "我爱你!!";
mysqli_close($link);
?>
# 编写剧本
[root@localhost ~]# vim lnmp.yml
- hosts: dbserver
remote_user: root
tasks:
# nginx的安装运行
- name: install nginx
unarchive: src=/root/nginx-1.11.1.tar.gz dest=/usr/src
- name: yum install pcre* openssl*
yum: name=pcre-devel,openssl-devel,gcc,gcc-c++,zlib-devel
- name: make install nginx
shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-pcre && make && make install
args:
chdir: /usr/src/nginx-1.11.1
- name: lnfile
file: src=/usr/local/nginx/sbin/nginx dest=/usr/local/sbin/nginx state=link
- name: create nginx user
user: name=nginx create_home=no shell=/sbin/nologin state=present
- name: nginx start script
copy: src=/root/nginx dest=/etc/init.d/nginx
- name: xp
file: path=/etc/init.d/nginx mode=0755
- name: add system service
shell: chkconfig --add nginx
- name: open system auto started
shell: systemctl enable nginx
- name: start nginx
service: name=nginx state=started
# mysql安装
- name: yum install ncurses-devel
yum: name=ncurses-devel
- name: install cmkae
unarchive: src=/root/cmake-2.8.7.tar.gz dest=/usr/src/
- name: make install cmake
shell: ./configure && gmake && gmake install
args:
chdir: /usr/src/cmake-2.8.7
- name: install mysql
unarchive: src=/root/mysql-5.5.22.tar.gz dest=/usr/src/
- name: make install mysql
shell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install
args:
chdir: /usr/src/mysql-5.5.22
- name: soft link
file: src=/usr/local/mysql/lib/libmysqlclient.so.18 dest=/usr/lib/libmysqlclient.so.18 state=link
- name: soft link
file: src=/usr/local/mysql/bin/mysql dest=/usr/bin/mysql state=link
- name: mysqldupm soft link
file: src=/usr/local/mysql/bin/mysqldump dest=/usr/bin/mysqldump state=link
- name: add user
user: name=mysql create_home=no shell=/sbin/nologin state=present
- name: mysql config file
copy: src=/usr/src/mysql-5.5.22/support-files/my-medium.cnf dest=/etc/my.cnf remote_src=yes
- name: mysql start script
copy: src=/usr/src/mysql-5.5.22/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes
- name: shou quan
file: path=/etc/init.d/mysqld mode=0755
- name: create system service
shell: chkconfig --add mysqld
- name: init database
shell: /usr/local/mysql/scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
- name: directory quanxian
file: path=/usr/local/mysql owner=mysql group=mysql recurse=yes
- name: start mysqld
service: name=mysqld state=started
- name: bainliang
shell: echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && source /etc/profile
# php安装启动
- name: yum install gd libxml2-devel libjpeg-devel libpng-devel
yum: name=gd,libxml2-devel,libjpeg-devel,libpng-devel
- name: rz php tar
unarchive: src=/root/php-5.3.28.tar.gz dest=/usr/src
- name: make install php
shell: ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-config-file-path=/usr/local/php --enable-fpm --enable-mbstring --with-jpeg-dir=/usr/lib && make && make install
args:
chdir: /usr/src/php-5.3.28
- name: cp configfile
copy: src=/usr/src/php-5.3.28/php.ini-development dest=/usr/local/php/php.ini remote_src=yes
- name: alter primary configfile
replace: path=/usr/local/php/php.ini regexp='^default_charset' replace='default_charset = "utf-8"'
- name: alter primary configfile1
replace: path=/usr/local/php/php.ini regexp='^short_open_tag' replace='short_open_tag = On'
- name: install youhua
unarchive: src=/root/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz dest=/usr/src
- name: cp configfile
copy: src=/usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so dest=/usr/local/php/lib/php/ remote_src=yes
- name: edit configfile
shell: sed -i '$azend_extension=/usr/local/php/lib/php/ZendGuardLoader.so\nzend_loader.enable=1' /usr/local/php/php.ini
- name: cp php-fpm
copy: src=/usr/src/php-5.3.28/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes
- name: script php-fpm quanxian
file: path=/etc/init.d/php-fpm mode=0755
- name: add system service
shell: chkconfig --add php-fpm
- name: cp php-fpm configfile
copy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes
- name: edit php-fpm cfg max_children
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.max_children = 5' replace='pm.max_children = 50'
- name: edit php-fpm cfg star_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.start_servers = 2' replace='pm.start_servers = 20'
- name: edit php-fpm cfg min_spare_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.min_spare_servers = 1' replace='pm.min_spare_servers = 5'
- name: edit php-fpm cfg max_spare_servers
replace: path=/usr/local/php/etc/php-fpm.conf regexp='^pm.max_spare_servers = 3' replace='pm.max_spare_servers = 35'
- name: edit php-fpm pid
replace: path=/usr/local/php/etc/php-fpm.conf regexp=';pid = run/php-fpm.pid' replace='pid = run/php-fpm.pid'
- name:
replace: path=/usr/local/php/etc/php-fpm.conf regexp='user = nobody' replace='user = nginx'
- name:
replace: path=/usr/local/php/etc/php-fpm.conf regexp='group = nobody' replace='group = nginx'
- name: start php-fpm
service: name=php-fpm state=started
# 整合nginx和php
- shell: sed -i '/ server_name localhost;/a \ location ~ \.php$ {\n root html;\n fastcgi_pass 127.0.0.1:9000;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;\n include fastcgi.conf;\n}' /usr/local/nginx/conf/nginx.conf
- replace: path=/usr/local/nginx/conf/nginx.conf regexp=' index index.html index.htm;' replace=' index index.html index.htm index.php;'
- service: name=nginx state=restarted
- file: path=/usr/local/nginx/html/index.php state=touch
# 验证php页面,需要mysql授权,防火墙端口放行或者关闭防火墙
- template: src=/root/index.php.j2 dest=/usr/local/nginx/html/index.php