ansible一鍵安裝LNMP
阿新 • • 發佈:2018-12-18
- 搭建LNMP(ansible)
ansible安裝nginx,php,mysql(nginx.yaml等)
分發nginx配置檔案,作為web伺服器
重啟nginx - 搭建LNMT(ansible)
ansible安裝nginx,tomcat,mysql(nginx.yaml等)
分發nginx配置檔案,作為web伺服器
重啟nginx
先寫子目錄劇本,再將引數加入主目錄劇本main.yaml
roles/nginx/tasks/main.yaml 這個劇本只寫tasks任務(都是一級標題)
files目錄存放:指令碼,rpm包,原始碼包等
給指令碼x許可權
無需指定路徑,預設去固定目錄下查詢
templates目錄,別寫錯了,指定相對路徑即可
notify模組在handlers目錄下查詢
搭建wordpress部落格
先搭建LNMP
nginx,php,MySQL模組只定義最原始檔案,無需修改配置檔案(不變部分)
分別對這三個分發檔案,觸發啟動
在搭建一個LNMP模組,修改引數(可變部分)
再搭建tomcat模組原始檔案,及可變動的LNMT模組即可
分發範圍一樣的,hosts設定一個
分發範圍不一樣,hosts設定不同角色即可
部署ansible環境
- 搭建ansible
- 環境部署
[[email protected] myroles]# tree /myroles/ /myroles/ ├── main.yaml ├── mysql.yaml ├── nginx.yaml ├── php.yaml ├── roles │ ├── main │ │ ├── files │ │ │ ├── main.sh │ │ │ └── wordpress-4.7.4-zh_CN.tar.gz │ │ ├── handlers │ │ │ └── main.yaml │ │ ├── tasks │ │ │ └── main.yaml │ │ ├── templates │ │ │ └── nginx.conf │ │ └── vars │ │ └── main.yaml │ ├── mysql │ │ ├── files │ │ │ ├── mysql-5.5.32-linux2.6-x86_64.tar.gz │ │ │ └── mysql.sh │ │ ├── handlers │ │ │ └── main.yaml │ │ ├── tasks │ │ │ └── main.yaml │ │ ├── templates │ │ └── vars │ │ └── main.yaml │ ├── nginx │ │ ├── files │ │ │ ├── nginx-1.10.2.tar.gz │ │ │ ├── nginx.conf │ │ │ └── nginx.sh │ │ ├── handlers │ │ │ └── main.yaml │ │ ├── tasks │ │ │ └── main.yaml │ │ ├── templates │ │ └── vars │ ├── php │ │ ├── files │ │ │ ├── libiconv-1.14.tar.gz │ │ │ ├── libmcrypt-2.5.8-9.el6.x86_64.rpm │ │ │ ├── libmcrypt-devel-2.5.8-9.el6.x86_64.rpm │ │ │ ├── mcrypt-2.6.8-10.el6.x86_64.rpm │ │ │ ├── mhash-0.9.9.9-3.el6.x86_64.rpm │ │ │ ├── php-5.3.28.tar.gz │ │ │ └── php.sh │ │ ├── handlers │ │ │ └── main.yaml │ │ ├── tasks │ │ │ └── main.yaml │ │ ├── templates │ │ └── vars │ └── tomcat │ ├── files │ │ ├── apache-maven-3.3.9-bin.tar.gz │ │ ├── apache-tomcat-8.0.27.tar.gz │ │ ├── java.sh │ │ ├── jdk-8u60-linux-x64.tar.gz │ │ ├── jpress-web-newest.war │ │ ├── memtest.war │ │ └── tomcat.sh │ ├── handlers │ │ └── main.yaml │ ├── tasks │ │ └── main.yaml │ ├── templates │ └── vars └── tomcat.yaml 31 directories, 39 files
部署nginx環境
- 配置nginx.yaml劇本
[[email protected] files]# cd /myroles/
[[email protected] myroles]# cat nginx.yaml
---
- hosts: all
gather_facts: True
roles:
- nginx
- 配置nginx配置檔案
[[email protected] myroles]# cd roles/nginx/
[[email protected] nginx]# ls
files handlers tasks templates vars
[ [email protected] nginx]# cd files/
[[email protected] files]# ls
nginx-1.10.2.tar.gz nginx.conf nginx.sh
[[email protected] files]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
- 配置nginx的安裝指令碼
[[email protected] files]# cat nginx.sh
#!/bin/bash
rpm -qf nginx
if [ $? -ne 0 ];then umount /dev/sr0
[ -d /media/cdrom ] || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom
if [ $? -ne 0 ];then
echo "請插入光碟!"
exit
fi
[ -d /etc/yum.repo.d ] || mkdir -p /etc/yum.repos.d
cd /etc/yum.repos.d
cat > /etc/yum.repos.d/localyum.repo << FEF
[local]
name=local
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1
FEF
/usr/bin/yum -y clean all &>/dev/null
/usr/bin/yum makecache &>/dev/null
[ $? -eq 0 ] && echo "yum 搭建完畢" || echo "yum搭建失敗"
/usr/bin/yum -y install gcc gcc-c++ make pcre-devel openssl-devel &>/dev/null
[ $? -eq 0 ] && echo "nginx軟體包安裝完畢" || echo "nginx軟體包安裝失敗"
fi
cd ~
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2/
useradd -s /sbin/nologin -M www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module &>/dev/null
make && make install
[ $? -eq 0 ] && echo "nginx編譯成功" || echo "nginx編譯失敗"
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
- 配置nginx的notify指令碼
[[email protected] handlers]# cd ..
[[email protected] nginx]# ls
files handlers tasks templates vars
[[email protected] nginx]# cd handlers/
[[email protected] handlers]# ls
main.yaml
[[email protected] handlers]# cat main.yaml
---
- name: start_nginx
shell: /usr/local/nginx/sbin/nginx
- name: stop_nginx
shell: /usr/local/nginx/sbin/nginx -s stop
- name: reload_nginx
shell: /usr/local/nginx/sbin/nginx -s reload
- 配置nginx的tasks執行指令碼
[[email protected] handlers]# cd ..
[[email protected] nginx]# ls
files handlers tasks templates vars
[[email protected] nginx]# cd tasks/
[[email protected] tasks]# ls
main.yaml
[[email protected] tasks]# cat main.yaml
---
- name: check alived
ping:
- name: cp install_source
copy: src=nginx-1.10.2.tar.gz dest=/root/
- name:
shell: chmod +x nginx.sh
- name: nginx install
script: nginx.sh
register: ls_result
- debug: var=ls_result
- name: nginx conf
copy: src=nginx.conf dest=/usr/local/nginx/conf/ backup=yes
notify: start_nginx
[[email protected] nginx]# ls templates/
[[email protected] nginx]# ls vars/
- 執行nginx.yaml劇本,一鍵安裝測試
[[email protected] myroles]# ansible-playbook nginx.yaml
- 檢視客戶端
[[email protected] ~]# netstat -antup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4711/nginx
部署MySQL環境
- 配置mysql.yaml劇本
[[email protected] myroles]# ls
mysql.retry mysql.yaml nginx.retry nginx.yaml php.retry php.yaml roles tomcat.yaml
[[email protected] myroles]# cat mysql.yaml
---
- hosts: all
gather_facts: True
roles:
- mysql
- 配置mysql安裝指令碼
[[email protected] myroles]# cd roles/mysql/
[[email protected] mysql]# ls
files handlers tasks templates vars
[[email protected] mysql]# cd files/
[[email protected] files]# ls
mysql-5.5.32-linux2.6-x86_64.tar.gz mysql.sh
[[email protected] files]# cat mysql.sh
#!/bin/bash
rpm -qf mysql
if [ $? -ne 0 ];then umount /dev/sr0
[ -d /media/cdrom ] || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom
if [ $? -ne 0 ];then
echo "請插入光碟!"
exit
fi
[ -d /etc/yum.repo.d ] || mkdir -p /etc/yum.repos.d
cd /etc/yum.repos.d
cat > /etc/yum.repos.d/localyum.repo << FEF
[local]
name=local
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1
FEF
/usr/bin/yum -y clean all &>/dev/null
/usr/bin/yum makecache &>/dev/null
[ $? -eq 0 ] && echo "yum 搭建完畢" || echo "yum搭建失敗"
/usr/bin/yum -y install gcc gcc-c++ make libaio &>/dev/null
[ $? -eq 0 ] && echo "mysql軟體包安裝完畢" || echo "mysql軟體包安裝失敗"
fi
cd ~
tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
useradd -s /sbin/nologin -M mysql
echo "`hostname -I` LNMP" >> /etc/hosts
ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql
cd /usr/local/mysql/
/bin/cp support-files/my-small.cnf /etc/my.cnf
chown -R mysql.mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql &>/dev/null
/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/* /usr/local/bin/
- 配置notify通知指令碼
[[email protected] mysql]# ls
files handlers tasks templates vars
[[email protected] mysql]# cd handlers/
[[email protected] handlers]# ls
main.yaml
[[email protected] handlers]# cat main.yaml
---
- name: start_mysql
shell: /etc/init.d/mysqld start
- name: stop_mysql
shell: /etc/init.d/mysqld stop
- name: reload_mysql
shell: /etc/init.d/mysqld reload
- name: restart_mysql
shell: /etc/init.d/mysqld restart
- name: chkon_mysql
shell: chkconfig mysqld on
- name: chkoff_mysql
shell: chkconfig mysqld off
- 配置tasks的執行指令碼
[[email protected] mysql]# ls
files handlers tasks templates vars
[[email protected] mysql]# cd tasks/
[[email protected] tasks]# ls
main.yaml
[[email protected] tasks]# cat main.yaml
---
- name: check alived
ping:
- name: cp install_source
copy: src=mysql-5.5.32-linux2.6-x86_64.tar.gz dest=/root/
- name: mysql install
script: mysql.sh
register: my_result
notify:
- start_mysql
- debug: var=my_result
- name:
shell: chkconfig --add mysqld
notify:
- chkon_mysql
- name: mysqladmin
shell: mysqladmin -u{{ user }} password '{{ password }}'
- 配置自定義變數指令碼
[[email protected] mysql]# ls
files handlers tasks templates vars
[[email protected] mysql]# ls templates/
[[email protected] mysql]# ls vars/
main.yaml
[[email protected] mysql]# cat vars/main.yaml
user: "root"
password: "123456"
- 執行mysql.yaml劇本,一鍵安裝測試
[[email protected] myroles]# ansible-playbook mysql.yaml
- 檢視客戶端
[[email protected] ~]# netstat -antup | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6447/mysqld
部署PHP環境
- 配置php.yaml劇本
[[email protected] myroles]# cat php.yaml
---
- hosts: all
gather_facts: True
roles:
- php
- 配置php安裝指令碼
[[email protected] myroles]# cd roles/php/
[[email protected] php]# ls
files handlers tasks templates vars
[[email protected] php]# cd files/
[[email protected] files]# ls
libiconv-1.14.tar.gz mcrypt-2.6.8-10.el6.x86_64.rpm php.sh
libmcrypt-2.5.8-9.el6.x86_64.rpm mhash-0.9.9.9-3.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm php-5.3.28.tar.gz
[[email protected] files]# cat php.sh
#!/bin/bash
rpm -qf php
if [ $? -ne 0 ];then umount /dev/sr0
[ -d /media/cdrom ] || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom
if [ $? -ne 0 ];then
echo "請插入光碟!"
exit
fi
[ -d /etc/yum.repo.d ] || mkdir -p /etc/yum.repos.d
cd /etc/yum.repos.d
cat > /etc/yum.repos.d/localyum.repo << FEF
[local]
name=local
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1
FEF
/usr/bin/yum -y clean all &>/dev/null
/usr/bin/yum makecache &>/dev/null
[ $? -eq 0 ] && echo "yum 搭建完畢" || echo "yum搭建失敗"
/usr/bin/yum -y install openssl-devel openssl
/usr/bin/yum -y install gcc gcc-c++ make zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel &>/dev/null
/usr/bin/yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel &>/dev/null
[ $? -eq 0 ] && echo "php軟體包安裝完畢" || echo "php軟體包安裝失敗"
fi
cd ~
tar xf libiconv-1.14.tar.gz -C /usr/src/
cd /usr/src/libiconv-1.14/
./configure --prefix=/usr/local/libiconv && make && make install &>/dev/null
[ $? -eq 0 ] && echo "libiconv編譯成功" || echo "libiconv編譯失敗"
cd ~
rpm -ivh mhash-0.9.9.9-3.el6.x86_64.rpm &>/dev/null
rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm &>/dev/null
rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm &>/dev/null
rpm -ivh mcrypt-2.6.8-10.el6.x86_64.rpm &>/dev/null
tar xf php-5.3.28.tar.gz -C /usr/src/
cd /usr/src/php-5.3.28/
./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp &>/dev/null
[ $? -eq 0 ] && echo "php編譯成功" || echo "php編譯失敗"
touch ext/phar/phar.phar
make && make install &>/dev/null
[ $? -eq 0 ] && echo "php搭建成功" || echo "php搭建失敗"
ln -s /usr/local/php5.3.28/ /usr/local/php
cd /usr/src/php-5.3.28/
/bin/cp php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc/
/bin/cp php-fpm.conf.default php-fpm.conf
- 配置notify通知指令碼
[[email protected] php]# ls
files handlers tasks templates vars
[[email protected] php]# cd handlers/
[[email protected] handlers]# ls
main.yaml
[[email protected] handlers]# cat main.yaml
---
- name: start_php
shell: /usr/local/php/sbin/php-fpm
- name: stop_php
shell: pkill php-fpm
- 配置tasks的執行指令碼
[[email protected] php]# ls
files handlers tasks templates vars
[[email protected] php]# cd tasks/
[[email protected] tasks]# ls
main.yaml
[[email protected] tasks]# cat main.yaml
---
- name: check alived
ping:
- name: cp install_source1
copy: src=libiconv-1.14.tar.gz dest=/root/
- name: cp install_source2
copy: src=mcrypt-2.6.8-10.el6.x86_64.rpm dest=/root/
- name: cp install_source3
copy: src=libmcrypt-2.5.8-9.el6.x86_64.rpm dest=/root/
- name: cp install_source4
copy: src=libmcrypt-devel-2.5.8-9.el6.x86_64.rpm dest=/root/
- name: cp install_source5
copy: src=mhash-0.9.9.9-3.el6.x86_64.rpm dest=/root/
- name: cp install_source6
copy: src=php-5.3.28.tar.gz dest=/root/
- name: php install
script: php.sh
register: p_result
notify:
- start_php
- debug: var=p_result
[[email protected] php]# ls templates/
[[email protected] php]# ls vars/
- 執行php.yaml劇本,一鍵安裝測試
[[email protected] myroles]# ansible-playbook php.yaml
- 檢視客戶端
[[email protected] ~]# netstat -antup | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 65105/php-fpm
配置nginx支援PHP程式請求訪問
- 配置主變數main.yaml劇本
[[email protected] myroles]# ls
main.retry mysql.retry nginx.retry php.retry roles
main.yaml mysql.yaml nginx.yaml php.yaml tomcat.yaml
[[email protected] myroles]# cat main.yaml
---
- hosts: all
gather_facts: True
roles:
- main
- 配置主變數安裝指令碼
[[email protected] roles]# ls
main mysql nginx php tomcat
[[email protected] roles]# cd main/
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd files/
[[email protected] files]# ls
main.sh
[[email protected] files]# cat main.sh
#!/bin/bash
cd /usr/local/nginx/html
[ -d blog ] || mkdir -p blog
echo "`hostname -I` blog.yunjisuan.com" > blog/index.html
echo "`hostname -I` blog.yunjisuan.com" >> /etc/hosts
- 配置notify通知指令碼
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd handlers/
[[email protected] handlers]# ls
main.yaml
[[email protected] handlers]# cat main.yaml
---
- name: start_nginx
shell: /usr/local/nginx/sbin/nginx
- name: stop_nginx
shell: /usr/local/nginx/sbin/nginx -s stop
- name: reload_nginx
shell: /usr/local/nginx/sbin/nginx -s reload
- 配置tasks的執行指令碼
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd tasks/
[[email protected] tasks]# ls
main.yaml
[[email protected] tasks]# cat main.yaml
---
- name: cd
shell: cd /usr/local/nginx/conf/
- name: nginx conf
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
notify:
- reload_nginx
- name:
script: main.sh
notify: reload_nginx
- 配置template模組下發可變的配置檔案
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd templates/
[[email protected] templates]# ls
nginx.conf
[[email protected] templates]# cat nginx.conf
worker_processes {{ ansible_processor_count }};
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog;
index index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
- 執行main.yaml劇本,一鍵安裝測試
[[email protected] myroles]# ansible-playbook main.yaml
- 測試客戶端
[[email protected] nginx]# curl blog.yunjisuan.com
192.168.239.141 blog.yunjisuan.com
WordPress部落格程式的搭建準備
- 配置主變數main.yaml劇本
[[email protected] myroles]# ls
main.retry mysql.retry nginx.retry php.retry roles
main.yaml mysql.yaml nginx.yaml php.yaml tomcat.yaml
[[email protected] myroles]# cat main.yaml
---
- hosts: all
gather_facts: True
roles:
- main
- 配置主變數安裝指令碼
[[email protected] roles]# ls
main mysql nginx php tomcat
[[email protected] roles]# cd main/
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd files/
[[email protected] files]# ls
main.sh wordpress-4.7.4-zh_CN.tar.gz
[[email protected] files]# cat main.sh
#!/bin/bash
cd /usr/local/nginx/html
[ -d blog ] || mkdir -p blog
echo "`hostname -I` blog.yunjisuan.com" > blog/index.html
echo "`hostname -I` blog.yunjisuan.com" >> /etc/hosts
cd ~
tar xf wordpress-4.7.4-zh_CN.tar.gz
cd /usr/local/nginx/html/blog/
rm -rf *
mv ~/wordpress .
/bin/mv wordpress/* .
rm -rf wordpress/
chown -R www.www /usr/local/nginx/html/blog
- 配置notify通知指令碼
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd handlers/
[[email protected] handlers]# ls
main.yaml
[[email protected] handlers]# cat main.yaml
---
- name: start_nginx
shell: /usr/local/nginx/sbin/nginx
- name: stop_nginx
shell: /usr/local/nginx/sbin/nginx -s stop
- name: reload_nginx
shell: /usr/local/nginx/sbin/nginx -s reload
- 配置tasks的執行指令碼
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd tasks/
[[email protected] tasks]# ls
main.yaml
[[email protected] tasks]# cat main.yaml
---
- name:
shell: mysql -u{{ user }} -p{{ password }} -e "create database wordpress"
- name:
shell: mysql -u{{ user }} -p{{ password }} -e " grant all on wordpress.* to [email protected]'localhost' identified by '{{ passwd }}'"
- name:
shell: mysql -u{{ user }} -p{{ password }} -e "flush privileges"
- name: cd
shell: cd /usr/local/nginx/conf/
- name: nginx conf
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
notify:
- reload_nginx
- name:
copy: src=wordpress-4.7.4-zh_CN.tar.gz dest=/root/
- name:
script: main.sh
notify: reload_nginx
- 配置template模組下發可變的配置檔案
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd templates/
[[email protected] templates]# ls
nginx.conf
[[email protected] templates]# cat nginx.conf
worker_processes {{ ansible_processor_count }};
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog;
index index.php index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
- 配置自定義變數指令碼
[[email protected] main]# ls
files handlers tasks templates vars
[[email protected] main]# cd vars/
[[email protected] vars]# ls
main.yaml
[[email protected] vars]# cat main.yaml
user: "root"
password: "123456"
passwd: "123456"
- 執行main.yaml劇本,一鍵安裝測試
[[email protected] myroles]# ansible-playbook main.yaml
- 安裝blog部落格程式