第一部分:安裝apachephp
1、準備工做
html
一、下載httpd、php、mysql 、apr、apr-util、libiconv 本次使用的版本以下:mysql
httpd 2.4.1七、php 5.6.1五、mysql 5.6.19(二進制)、apr 1.5.二、apr-util 1.5.四、libiconv 1.14linux
二、在ansible服務端開始安裝c++
編譯安裝aprweb
#cd /usr/local/srcsql
#tar xf apr-1.5.2.tar.gzshell
#cd apr-1.5.2apache
#./configure --prefix=/usr/local/apr --若是提示以下錯誤(不知道忽略這種錯誤會如何,沒測試):vim
error info:rm: cannot remove `libtoolT': No such file or directory
解決方法:
#vim configure --找到$RM "$cfgfile" 這行, 將其註釋或者刪除
#$RM "$cfgfile" ,而後從新執行configure 就不會報錯了
#make && make install
編譯安裝apr-util
#cd /usr/local/src
#tar xf apr-util-1.5.4.tar.gz
#cd apr-util-1.5.4
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
#make && make install
編譯安裝libiconv
#tar xf libiconv-1.14.tar.gz
#cd libiconv-1.14
#./configure --prefix=/usr/local/
#make && make install
而後拷貝libiconv的so文件到一個目錄
#mkdir /usr/local/libiconv
#cd /usr/local/libiconv
#cp /usr/local/lib/libiconv.la ./
#cp /usr/local/lib/libiconv.so.2.5.1 ./
#cp /usr/local/lib/preloadable_libiconv.so ./
建立軟鏈接
#ln -sv libiconv.so.2.5.1 libiconv.so
#ln -sv libiconv.so.2.5.1 libiconv.so.2
而後打包:
#tar -zcf libiconv.tar.gz ./*
#mv libiconv.tar.gz /tmp
安裝依賴包:
#yum install pcre pcre-devel -y
編譯安裝httpd
#tar xf httpd-2.4.17.tar.gz
#cd httpd-2.4.17
#./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
#make
#make install
在httpd.conf配置文件中添加pid文件,找到# least PidFile,添加其下:
PidFile "/var/run/httpd.pid"
User www --修改成www
Group www --修改成www
爲httpd提供SysV服務腳本/etc/rc.d/init.d/httpd,以下:
#!/bin/bash
# httpd Startup script for the Apache HTTP Server
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
賦予執行權限
#chmod +x /etc/init.d/httpd
啓動httpd
#service httpd start
而後在瀏覽器測試
#http://ip --出現It works! 的網頁,表示httpd安裝成功了。
壓縮apache目錄:
#cd /usr/local/
#tar -zcf httpd-2.4.17.tar.gz apache/
#mv httpd-2.4.17.tar.gz /tmp
壓縮apr目錄:
#tar -zcf apr.tar.gz apr/
#mv apr.tar.gz /tmp
壓縮apr-util目錄:
#tar -zcf apr-util.tar.gz apr-util/
#mv apr-util.tar.gz /tmp
2、配置playbook ,經過ansible安裝配置遠端服務器的httpd
一、規劃(實現安裝和刪除httpd)
安裝:
#cd /etc/ansible/roles
#mkdir apache_install/{files,handlers,meta,tasks,templates,vars}
#cd apache_install
#mv /tmp/httpd.tar.gz files/
#mv /tmp/libiconv.tar.gz files/
#mv /tmp/apr.tar.gz files/
#mv /tmp/apr-util.tar.gz files/
配置meta
#vim meta/main.yml
galaxy_info:
author: liguang xing
description: Install Apache
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
version:
- 6
categories:
- Service
dependencies: []
配置vars變量
#vim vars/main.yml
apr_version: 1.5.2
apr_util_version: 1.5.4
libiconv_version: 1.14
apache_version: 2.4.17
apache_web_dir: /usr/local/apache/htdocs
apache_log: /usr/local/apache/logs/
apache_vhost: /usr/local/apache/conf/vhost
apache_port: 80
apache_user: www
配置tasks
#cat tasks/copy.yml
- name: Copy Apache software to client
copy: src=` item ` dest=/tmp/ owner=root group=root
with_items:
- httpd-` apache_version `.tar.gz
- libiconv.tar.gz
- apr.tar.gz
- apr-util.tar.gz
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: create apache user in client
user: name=` apache_user ` state=present createhome=no shell=/sbin/nologin
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apache software to client
shell: tar xf /tmp/httpd-` apache_version `.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apr software to client
shell: tar xf /tmp/apr.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apr-util software to client
shell: tar xf /tmp/apr-util.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: copy apache sysv start service script to client
template: src=httpd dest=/etc/init.d/httpd owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: copy apache config to Client
template: src=httpd.conf dest=/usr/local/apache/conf/httpd.conf owner=` apache_user ` group=` apache_user ` mode=0644
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
注:when參數不配置也能夠
#cat tasks/delete.yml
- name: delete apache compression software in client
shell: rm -f /tmp/httpd-` apache_version `.tar.gz /tmp/libiconv.tar.gz /tmp/apr.tar.gz /tmp/apr-util.tar.gz
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
#cat tasks/install.yml
- name: create lib install dir
file: dest=/usr/local/lib state=directory
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: check apache Iconv in client
shell: find / -name "libiconv.so.2"|grep -Ev "tmp|soft"|wc -l
register: apache_iconv
ignore_errors: True
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: install apache iconv in client
shell: tar xf /tmp/libiconv.tar.gz -C /usr/local/lib/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0
- name: check lib in config in client
shell: grep -c /usr/local/lib/ /etc/ld.so.conf
register: apache_lib
ignore_errors: True
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: install apache Iconv in client
shell: echo "/usr/local/lib/" >> /etc/ld.so.conf;/sbin/ldconfig
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0
- name: create apache Dir
file: dest=` item ` state=directory
with_items:
- "` apache_vhost `"
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: start apache service in client
shell: /etc/init.d/httpd start
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: add boot start apache service in client
shell: chkconfig --add httpd;chkconfig httpd on
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml
將httpd啓動腳本和httpd.conf配置文件copy一份到templates目錄
#cp /etc/rc.d/init.d/httpd templates/
#cp /usr/local/apache/conf/httpd.conf templates/
template目錄文件
# tree templates/
templates/
├── httpd
└── httpd.conf
二、配置common
#cd /etc/ansible/roles
#mkdir -pv common/{meta,tasks}
#cat meta/main.yml
galaxy_info:
author: liguang xing
description: Install initializtion Software
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
versions:
- 5
- 6
- name: Ubuntu
versions:
- precise
categories:
- system
dependencies: []
#cat tasks/main.yml
- name: install initializtion require software
shell: yum -y install make cmake bc gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients libxslt-devel libevent-devel ntp libtool-ltdl bison libaio libtool vim-enhanced wget readline-devel libyaml-devel patch telnet pcre pcre-devel
poll: 0
注:libaio rpm包必定要安裝,mysql啓動要依賴着包
三、建立apache_install.yml安裝腳本
#cd /etc/ansible/roles
#cat apache_install.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- common
- apache_install
檢查語法:
# ansible-playbook apache_install.yml --syntax-check
playbook: apache_install.yml
沒錯誤開始執行
#ansible-playbook apache_install.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
ok: [10.0.90.25]
TASK: [common | install initializtion require software] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | Copy Apache software to client] ***********************
changed: [10.0.90.25] => (item=httpd-2.4.17.tar.gz)
changed: [10.0.90.24] => (item=httpd-2.4.17.tar.gz)
changed: [10.0.90.24] => (item=libiconv.tar.gz)
changed: [10.0.90.25] => (item=libiconv.tar.gz)
changed: [10.0.90.24] => (item=apr.tar.gz)
changed: [10.0.90.25] => (item=apr.tar.gz)
changed: [10.0.90.25] => (item=apr-util.tar.gz)
changed: [10.0.90.24] => (item=apr-util.tar.gz)
TASK: [apache_install | create apache user in client] *************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_install | uncompression apache software to client] **************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | uncompression apr software to client] *****************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | uncompression apr-util software to client] ************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | copy apache sysv start service script to client] ******
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_install | copy apache config to Client] *************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | create lib install dir] *******************************
ok: [10.0.90.24]
ok: [10.0.90.25]
TASK: [apache_install | check apache Iconv in client] *************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | install apache iconv in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | check lib in config in client] ************************
failed: [10.0.90.24] => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002203", "end": "2015-11-23 18:12:47.338012", "rc": 1, "start": "2015-11-23 18:12:47.335809", "warnings": []}
stdout: 0
...ignoring
failed: [10.0.90.25] => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002412", "end": "2015-11-23 18:12:47.349201", "rc": 1, "start": "2015-11-23 18:12:47.346789", "warnings": []}
stdout: 0
...ignoring
TASK: [apache_install | install apache Iconv in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | create apache Dir] ************************************
changed: [10.0.90.24] => (item=/usr/local/apache/conf/vhost)
changed: [10.0.90.25] => (item=/usr/local/apache/conf/vhost)
TASK: [apache_install | start apache service in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | add boot start apache service in client] **************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | delete apache compression software in client] *********
changed: [10.0.90.24]
changed: [10.0.90.25]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=18 changed=16 unreachable=0 failed=0
10.0.90.25 : ok=18 changed=16 unreachable=0 failed=0
而後能夠到90.24和90.25這兩臺客戶端服務器上檢查apache的安裝狀況,進行測試。
四、刪除httpd
#cd /etc/ansible/roles
#mkdir -pv apache_delete/{meta,tasks,vars}
配置yml文件
#cat apache_delete/meta/main.yml
galaxy_info:
author: liguang xing
description: Delete Apache
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
versions:
- 6
categories:
- Service
dependencies: []
# cat apache_delete/tasks/delete.yml
- name: stop httpd service in client
service: name=httpd state=stopped
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete boot start in client
shell: chkconfig --del httpd
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache Dir in client
shell: rm -rf /usr/local/apache
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apr Dir in client
shell: rm -rf /usr/local/apr
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apr-util Dir in client
shell: rm -rf /usr/local/apr-util
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache service script in client
shell: rm -f /etc/init.d/httpd
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache user
shell: userdel www
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat apache_delete/tasks/main.yml
- include: delete.yml
五、建立apache_delete.yml刪除腳本
#cd /etc/ansible/roles
#cat apache_delete.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- apache_delete
檢查語法:
# ansible-playbook apache_delete.yml --syntax-check
playbook: apache_delete.yml
沒有錯誤,開始執行:
# ansible-playbook apache_delete.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.25]
ok: [10.0.90.24]
TASK: [apache_delete | stop httpd service in client] **************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete boot start in client] ***************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache Dir in client] ***************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_delete | delete apr Dir in client] ******************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_delete | delete apr-util Dir in client] *************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache service script in client] ****************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache user] ************************************
changed: [10.0.90.24]
changed: [10.0.90.25]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=8 changed=7 unreachable=0 failed=0
10.0.90.25 : ok=8 changed=7 unreachable=0 failed=0
第二部分: 安裝mysql
1、首先下載mysql二進制安裝包
下載地址;http://dev.mysql.com/downloads/mysql/
我使用的是mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz
一、將安裝過程整合到ansible
#cd /usr/local/src
#tar xf mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz
#mv mysql-5.6.19-linux-glibc2.5-x86_64 mysql
再次打包
#tar -zcf mysql-5.6.19.tar.gz mysql/
#mv mysql-5.6.19.tar.gz /tmp
二、建立目錄並配置yml文件:
#cd /etc/ansible/roles
#mkdir -pv mysql_install/{files,meta,tasks,templates,vars}
#cd mysql_install
#mv /tmp/mysql-5.6.19.tar.gz files/
meta目錄:
#cat meta/main.yml(參照apache_install/meta/目錄中的main.yml,將description:修改下便可)
tasks目錄:
# cat tasks/copy.yml
- name: copy mysql software to in client
copy: src=mysql-5.6.19.tar.gz dest=/tmp/mysql-5.6.19.tar.gz owner=root group=root
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: create mysql user In client
user: name=` mysql_user ` state=present createhome=no shell=/sbin/nologin
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and ansible_distribution_version|int >=6
- name: Copy Mysql Start Script To Redhat Client
template: src=mysqld dest=/etc/init.d/mysqld owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Install Mysql Script To Redhat Client
template: src=install_mysql.sh dest=/tmp/ owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Mysql Config To Redhat Client
template: src=my.cnf dest=/tmp/ owner=root group=root mode=0644
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Mysql Security Script To Redhat Client
template: src=mysql_security.sh dest=/tmp/ owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/delete.yml
- name: Delete Mysql compression Software In Redhat Client
shell: rm -f /tmp/mysql-5.6.19.tar.gz /tmp/mysql_security.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/install.yml
- name: Create Mysql Install Dir
file: dest=/data/mysql state=directory
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Uncompression Mysql Software To Redhat Client
shell: tar zxf /tmp/mysql-5.6.19.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Modify Mysql Dir Permission In Redhat Client
file: path=` item ` owner=` mysql_user ` group=` mysql_user ` mode=0755
with_items:
- "` mysql_datadir `"
- "` mysql_basedir `"
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Install Mysql Script In Redhat Client
shell: /bin/bash /tmp/install_mysql.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Start Myql Security Script In Redhat Client
shell: /bin/bash /tmp/mysql_security.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Add Boot Start Mysql Service In Redhat Client
shell: chkconfig --add mysqld;chkconfig mysqld on
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
#cat tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml
3|、templates目錄:
#cd /etc/ansible/roles/mysql_install
#cat templates/install_mysql.sh --腳本內容:
#!/bin/bash
mv /tmp/my.cnf /etc/my.cnf
chown -R ` mysql_user `:` mysql_user ` ` mysql_datadir ` ` mysql_basedir `
####init mysql DB##
cd ` mysql_basedir `
./scripts/mysql_install_db --defaults-file=/etc/my.cnf --datadir=` mysql_datadir ` --user=` mysql_user ` >> /dev/null 2>&1 &
sleep 3
service mysqld start
##add mysql env##
echo 'MANPATH /usr/local/mysql/man' >> /etc/man.config
ln -sv /usr/local/mysql/include /usr/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
##
chkconfig --add mysqld
chkconfig mysqld on
rm -f /tmp/$(basename $0)
拷貝mysql的配置文件到templates目錄(也能夠根據本身需求配置my.cnf,以下:)
# cat templates/my.cnf
[client]
port = ` mysql_port `
socket = ` mysql_sock `
[mysqld]
character-set-server = utf8
port = ` mysql_port `
socket = ` mysql_sock `
datadir = ` mysql_datadir `
log-error = ` mysql_datadir `/mysql-error.log
pid-file = ` mysql_datadir `/mysql.pid
log-bin = ` mysql_datadir `/mysql-bin
skip-external-locking
skip-name-resolve
skip-symbolic-links
server-id = 1
binlog_format=mixed
binlog_cache_size=8M
max_binlog_cache_size=24M
max_binlog_size=1G
expire_logs_days=7
default_storage_engine = innodb
key_buffer_size = 384M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
join_buffer_size = 2M
thread_cache_size = 32
thread_concurrency = 16
explicit_defaults_for_timestamp=true
interactive_timeout = 120
wait_timeout = 120
table_open_cache = 4096
open_files_limit = 10240
back_log=600
max_connections = 5000
max_connect_errors = 5000
max_allowed_packet = 16M
tmp_table_size = 256M
max_heap_table_size = 512M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_repair_threads =1
myisam-recover-options
long_query_time = 2
slow_query_log
slow_query_log_file = ` mysql_datadir `/slow.log
innodb_buffer_pool_size = 1G
innodb_data_file_path = ibdata1:1G:autoextend
innodb_data_home_dir = ` mysql_datadir `
innodb_log_group_home_dir = ` mysql_datadir `
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 256M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 1
innodb_support_xa=0
innodb_flush_method = O_DIRECT
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
拷貝mysqld啓動腳本:
#cp /usr/local/src/mysql/support-files/mysql.server templates/
設置密碼腳本:
# cat templates/mysql_security.sh
#!/bin/bash
` mysql_basedir `/bin/mysql -h localhost -u root -e "set password for 'root'@'localhost' = password('mima');"
templates目錄結構:
# tree templates/
templates/
├── install_mysql.sh
├── my.cnf
├── mysqld
└── mysql_security.sh
vars 目錄:
# cat vars/main.yml
mysql_basedir: /usr/local/mysql
mysql_datadir: /data/mysql
mysql_user: mysql
mysql_database_user: root
mysql_passwd: 'mima'
mysql_port: 3306
mysql_sock: /data/mysql/mysql.sock
mysql_charset: utf8
mysql_collation: utf8_general_ci
mysql_version: mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz
三、建立mysql_install.yml安裝文件
#cd /etc/ansible/roles
# cat mysql_install.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- common
- mysql_install
四、開始執行
#ansible-playbook mysql_install.yml --syntax-check --檢測語法
playbook: mysql_install.yml
沒錯誤,開始執行:
# ansible-playbook mysql_install.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
TASK: [common | install initializtion require software] ***********************
changed: [10.0.90.24]
TASK: [mysql_install | copy mysql software to in client] **********************
ok: [10.0.90.24]
TASK: [mysql_install | create mysql user In client] **************************
changed: [10.0.90.24]
TASK: [mysql_install | Copy Mysql Start Script To Redhat Client] **************
changed: [10.0.90.24]
TASK: [mysql_install | Copy Install Mysql Script To Redhat Client] ***********
changed: [10.0.90.24]
TASK: [mysql_install | Copy Mysql Config To Redhat Client] ********************
changed: [10.0.90.24]
TASK: [mysql_install | Copy Mysql Security Script To Redhat Client] ***********
changed: [10.0.90.24]
TASK: [mysql_install | Create Mysql Install Dir] ******************************
changed: [10.0.90.24]
TASK: [mysql_install | Uncompression Mysql Software To Redhat Client] *********
changed: [10.0.90.24]
TASK: [mysql_install | Modify Mysql Dir Permission In Redhat Client] **********
changed: [10.0.90.24] => (item=/data/mysql)
changed: [10.0.90.24] => (item=/usr/local/mysql)
TASK: [mysql_install | Install Mysql Script In Redhat Client] *****************
changed: [10.0.90.24]
TASK: [mysql_install | Start Myql Security Script In Redhat Client] ***********
changed: [10.0.90.24]
TASK: [mysql_install | Add Boot Start Mysql Service In Redhat Client] *********
changed: [10.0.90.24]
TASK: [mysql_install | Delete Mysql compression Software In Redhat Client] ****
changed: [10.0.90.24]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=15 changed=13 unreachable=0 failed=0
成功以後到90.24服務器上查看mysql是否安裝成功!我測試沒問題
2、刪除mysql
一、建立目錄:
#cd /etc/ansible/roles
#mkdir -pv mysql_delete/{meta,tasks,vars}
# cat meta/main.yml (參照apache_install/meta/目錄中的main.yml,將description:修改下便可)
# cat tasks/delete.yml
- name: Stop Mysql Service
service: name=mysqld state=stopped
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Boot Start Script
shell: chkconfig --del mysqld
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Dir And Socket
shell: rm -rf ` mysql_basedir ` ` mysql_datadir `
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql User
shell: userdel ` mysql_user `
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Service Start Script
shell: rm -f /etc/init.d/mysqld
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql lib
shell: rm -f /etc/ld.so.conf.d/mysql.conf
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql include file
shell: rm -f /usr/include/mysql
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql config file
shell: rm -f /etc/my.cnf
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
#cat tasks/main.yml
- include: delete.yml
#cat vars/main.yml
mysql_basedir: /usr/local/mysql
mysql_datadir: /data/mysql
mysql_user: mysql
二、建立mysql_delete.yml文件:
#cd /etc/ansible/roles
#cat mysql_delete.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- mysql_delete
三、檢測語法並執行:
# ansible-playbook mysql_delete.yml --syntax-check
playbook: mysql_delete.yml
沒有報錯,開始執行
# ansible-playbook mysql_delete.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
TASK: [mysql_delete | Stop Mysql Service] *************************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql Boot Start Script] *************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql Dir And Socket] ****************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql User] **************************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql Service Start Script] **********************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql lib] ***************************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql include file] ******************************
changed: [10.0.90.24]
TASK: [mysql_delete | Delete Mysql config file] *******************************
changed: [10.0.90.24]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=9 changed=8 unreachable=0 failed=0
第三部分:安裝php
1、在ansible服務端安裝php或者其餘服務器上(服務器架構和ansible客戶端保持一致)
注:本文檔編譯安裝的php沒有編譯太多模塊,僅供參考,若是須要其餘模塊,請自行編譯加入!
一、下載php源碼,地址:http://php.net/downloads.php
本次下載的是php-5.6.15.tar.gz
二、開始編譯安裝php
若是想讓編譯的php支持mcrypt擴展,須要下載(http://www.rpmfind.net) 以下兩個rpm包並安裝之:
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
安裝libmcrypt:
#cd /usr/local/src
# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:libmcrypt ########################################### [ 50%]
2:libmcrypt-devel ########################################### [100%]
安裝php:
#tar xf php-5.6.15.tar.gz
#cd php-5.6.15
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml \
--with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-iconv=/usr/local/
#make
#make test
#make install
三、爲php提供配置文件
#mkdir /etc/php
#cp php.ini-production /etc/php/php.ini
四、編輯apache配置文件httpd.conf,以使apache支持php
# vim /usr/local/apache/conf/httpd.conf
(1)在AddType處添加以下兩行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
(2)定位至DirectoryIndex index.html
添加index.php:
DirectoryIndex index.php index.html
測試是否能夠正常解析顯示php格式的網頁:
#cd /usr/local/apache/htdocs
#cat index.php
<html><body><h1>
php is works!
</h1></body></html>
<?php
phpinfo();
?>
重啓httpd
#service httpd restart
在瀏覽器中測試:
http://ip --若是網頁顯示php測試頁面,表示已經支持php網頁。(我測試是能夠正常打開php測試頁面的)
注:若是沒法顯示php的網頁,查看selinux和iptables,selinux須要disabled或者permissive狀態!!!
五、測試php和mysql的鏈接狀況
#vim index.php
<html><body><h1>
php is works!
</h1></body></html>
<?php
$conn=mysql_connect('localhost:/data/mysql/mysql.sock','root','123456');
if($conn)
echo "Success...";
else
echo "Failure...";
mysql_close();
?>
注:個人mysql是編譯安裝的,sock文件在/data/mysql/目錄,若是sock文件在/tmp目錄,localhost後面就不須要指定sock路徑。
而後再瀏覽器測試:
http://ip --出現以下頁面,表示php與mysql鏈接正常
php is works!
Success...
2、整合php並經過ansible安裝到客戶端
一、建立目錄
#cd /etc/ansible/roles
#mkdir -pv php_install/{files,meta,tasks,templates,vars}
二、壓縮php目錄並拷貝文件
#cd /usr/local/
#tar -zcf php-5.6.15.tar.gz php/
#mv php-5.6.15.tar.gz /etc/ansible/roles/php_install/files
拷貝libmcrypt文件
#cd /usr/local/src
#cp libmcrypt-*.rpm /etc/ansible/roles/php_install/files
拷貝libphp5.so文件(沒有這個so文件,apache將沒法解析php頁面)
#cp /usr/local/apache/modules/libphp5.so /etc/ansible/roles/php_install/files
拷貝httpd.conf文件
#cd /etc/ansible/roles/php_install
#cp /usr/local/apache/conf/httpd.conf templates/
拷貝php.ini文件
#cp /etc/php/php.ini templates/
建立install_lib.sh腳本
#vim templates/install_lib.sh
#!/bin/bash
cd /tmp
rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
三、建立yml文件
#cat meta/main.yml (參考apache_install/meta/目錄中的main.yml,將description:修改下便可)
#cat tasks/copy.yml
- name: copy php software to client
copy: src=php-5.6.15.tar.gz dest=/tmp/ owner=root group=root
- name: copy php libphp5.so file to client
copy: src=libphp5.so dest=/usr/local/apache/modules/ owner=root group=root mode=0755
- name: uncompression php software to client
shell: tar xf /tmp/php-5.6.15.tar.gz -C /usr/local/
- name: copy install lib script to client
template: src=install_lib.sh dest=/tmp/ owner=root group=root mode=0755
- name: copy libmcrypt package to client
copy: src=libmcrypt-2.5.8-9.el6.x86_64.rpm dest=/tmp/ owner=root group=root
- name: copy libmcrypt-devel package to client
copy: src=libmcrypt-devel-2.5.8-9.el6.x86_64.rpm dest=/tmp/ owner=root group=root
- name: create php config Dir
file: dest=/etc/php state=directory
- name: copy php config file to client
template: src=php.ini dest=/etc/php owner=root group=root
- name: copy httpd.conf config file to client
template: src=httpd.conf dest=/usr/local/apache/conf/ owner=root group=root
#cat tasks/delete.yml
- name: delete php software and libmcrypt in client
shell: rm -f /tmp/php-5.6.15.tar.gz /tmp/libmcrypt-*.rpm /tmp/install_lib.sh
#cat tasks/install.yml
- name: install libmcrypt package in client
shell: /bin/bash /tmp/install_lib.sh
#cat tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml
templates 目錄下文件:
#tree templates/
templates/
├── httpd.conf
├── install_lib.sh
└── php.ini
files目錄下文件
#tree files
files
├── libmcrypt-2.5.8-9.el6.x86_64.rpm
├── libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
├── libphp5.so
└── php-5.6.15.tar.gz
四、建立php_install.yml文件
#cd /etc/ansible/roles
# cat php_install.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- common
- php_install
五、檢測語法並執行:
#ansible-playbook php_install.yml --syntax-check
執行:
#ansible-playbook php_install.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
TASK: [common | install initializtion require software] ***********************
changed: [10.0.90.24]
TASK: [php_install | copy php software to client] *****************************
ok: [10.0.90.24]
TASK: [php_install | copy php libphp5.so file to client] *********************
changed: [10.0.90.24]
TASK: [php_install | uncompression php software to client] ********************
changed: [10.0.90.24]
TASK: [php_install | copy install lib script to client] ***********************
ok: [10.0.90.24]
TASK: [php_install | copy libmcrypt package to client] ************************
ok: [10.0.90.24]
TASK: [php_install | copy libmcrypt-devel package to client] ******************
ok: [10.0.90.24]
TASK: [php_install | create php config Dir] **********************************
ok: [10.0.90.24]
TASK: [php_install | copy php config file to client] **************************
changed: [10.0.90.24]
TASK: [php_install | copy httpd.conf config file to client] *******************
ok: [10.0.90.24]
TASK: [php_install | install libmcrypt package in client] *********************
ok: [10.0.90.24]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=11 changed=10 unreachable=0 failed=0
3、刪除php
一、建立目錄
#cd /etc/ansible/roles
#mkdir -pv php_delete/{meta,tasks}
二、建立yml文件
#cd php_delete
#cat meta/main.yml (參考apache_install/meta/目錄中的main.yml文件,將description:修改下便可)
# cat tasks/delete.yml
- name: delete php directory in client
shell: rm -rf /usr/local/php
- name: delete php config file in client
shell: rm -f /etc/php/php.ini
#cat tasks/main.yml
- include: delete.yml
三、建立php_delete.yml文件
#cd /etc/ansible/roles
# cat php_delete.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- php_delete
四、檢測語法並執行
#ansible-playbook php_delete.yml --syntax-check
執行:
#ansible-playbook php_delete.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
TASK: [php_delete | delete php directory in client] ***************************
changed: [10.0.90.24]
TASK: [php_delete | delete php config file in client] *************************
changed: [10.0.90.24]
PLAY RECAP ********************************************************************
10.0.90.24 : ok=3 changed=2 unreachable=0 failed=0
第四部分:整合ansible安裝LAMP環境
一、建立安裝lamp的lamp_install.yml文件
#cd /etc/ansible/roles
#vim lamp_install.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- apache_install
- mysql_install
- php_install
測試:
# ansible-playbook lamp_install.yml --syntax-check
playbook: lamp_install.yml --無報錯,執行:
# ansible-playbook lamp_install.yml --extra-vars "host=web user=root"
PLAY [web] ********************************************************************
…………………… 太多了,省略
PLAY RECAP ********************************************************************
10.0.90.24 : ok=41 changed=38 unreachable=0 failed=0
二、建立刪除lamp的lamp_delete.yml文件
#cd /etc/ansible/roles
#vim lamp_delete.yml
---
- hosts: "`host`"
remote_user: "`user`"
gather_facts: True
roles:
- apache_delete
- mysql_delete
- php_delete
測試:
# ansible-playbook lamp_delete.yml --syntax-check
playbook: lamp_delete.yml --無報錯,執行:
# ansible-playbook lamp_delete.yml --extra-vars "host=web user=root"
…………………………省略
PLAY RECAP ********************************************************************
10.0.90.24 : ok=18 changed=17 unreachable=0 failed=0
注:參考連接:http://dl528888.blog.51cto.com/2382721/1533086 本人也是剛開始研究ansible,不足之處,請多多指出!