-
前言
基於 Ansible playbook 快速構建起LNMP環境的實戰的第一篇 。
1、環境描述
軟件 | 版本 |
OS |
CentOS 7.4 |
Ansible |
2.4.2 (extra倉庫) |
Nginx |
1.16.1(epel倉庫) |
PHP |
5.4.16 |
Mysql |
5.5.64 |
節點 | 規格 | 地址 | 角色 |
node1 |
2CPU 2G |
192.168.3.175 |
Ansible管理節點及被管理節點,部署nginx |
node2 |
2CPU 2G |
192.168.3.176 |
Ansible被管理節點,部署php、mysql |
2、準備操作
1.配置YUM源
yum install epel release yum clean all && yum makecache
2.安裝Ansible
yum install ansible
3.配置Ansible
修改host文件
vim /etc/hosts
192.168.3.175 node01
192.168.3.176 node02
配置免密登錄
ssh-keygen ssh-copy-id root@node01 ssh-copy-id root@node02
修改inventory文件
vim /etc/ansible/hosts
[nginx]
node01
[phpmysql]
node02
[LNMP]
node01
node02
測試Ansible
ansible all -m ping
3、準備所需文件
文件及目錄描述
需要準備的文件如下:
1) phpmyadmin,phpMyAdmin-4.4.15-all-languages.tar.gz、node01-pma.sh、node02-pma.sh,config.inc.php
2)nginx,index.html ,www.jjf.com.conf
3)php-fpm,index.php,www.conf
4) mariadb,server.cnf
具體的生成步驟可以參考下文,也可以根據需要自行生成或修改。
文章中所用的文件的打包,可以通過如下鏈接進行獲取
https://github.com/junfengjiang/
創建一個目錄存稍後放這些文件,這些文件在稍後playbook當中都會用到
# mkdir /ansible
# cd /ansible/
準備phpMyAdmin所需文件
phpMyAdmin,簡單來說是一個以php爲基礎的web服務器程序,讓管理者可用Web接口 管理MySQL數據庫。文章最後我們將通過phpMyAdmin來驗證我們NMP環境可用性。
我們是已RPM包的形式部署的LNMP環境,考慮到phpMyAdmin對NMP版本的依賴,我們這裏使用的是phpMyAdmin-4.4.15,其他軟件本可以參考文章開頭軟件信息說明。
要說明的是其中phpMyAdmin配置腳本要兩個,因爲phpMyAdmin的動態內容和靜態內容不是分開的,所以最簡單的方式是到時node1和node2上都要有一份。
下載phpMyAdmin - > phpMyAdmin-4.4.15-all-languages.tar.gz
curl -O https://files.phpmyadmin.net/phpMyAdmin/4.4.15/phpMyAdmin-4.4.15-all-languages.tar.gz
提供phpMyAdmin配置腳本 -> node01-pma.sh
vim node01-pma.sh
#!/bin/bash
ln -sv /appdata/phpMyAdmin-4.4.15-all-languages /data/nginx/html/pma
提供phpMyAdmin配置腳本 -> node02-pma.sh
vim node02-pma.sh
#!/bin/bash ln -sv /appdata/phpMyAdmin-4.4.15-all-languages /appdata/pma
提供phpMyAdmin配置文件 ->config.inc.php
獲取phpMyAdmin配置文件模板
tar -xvf phpMyAdmin-4.4.15-all-languages.tar.gz
cp phpMyAdmin-4.4.15-all-languages/config.sample.inc.php ./config.inc.php
rm phpMyAdmin-4.4.15-all-languages -rf
生成一段隨機數
openssl rand -base64 10 YyL82Wu2J4EjIw==
vim config.inc.php
只需修改這一項爲上面生成的隨機數
$cfg['blowfish_secret'] = 'YyL82Wu2J4EjIw'
準備nginx所需文件
我們需要提供一個nginx的測試頁面,後面來驗證nginx的安裝情況。
而後需要定義一個server,其中定義兩個location,所有以.php結尾的動態內容都通過fastcgi模塊代理至後端的fpmserver,其餘所有靜態內容都由nginx自身來進行處理。
提供nginx測頁 -> index.html
我們需要提供一個nginx的測試頁面,後面來驗證nginx的安裝情況。
vim index.html
<h1>hello nginx !!</h1>
提供nginx子配置文件 -> jjf.conf
server {
listen 80;
server_name www.jjf.com;
index index.php index.html;
location / {
root /data/nginx/html;}
location ~* \.php$ {
fastcgi_pass 192.168.3.176:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /appdata$fastcgi_script_name;
include fastcgi_params;
}}
準備php-fpm所需文件
同樣提供一個php的簡單測試頁面(phpinfo函數),後面來驗證php的安裝情況。
而後提供一個www.conf的配置文件,簡單修改www這個連接池的listen參數。
提供php測頁 -> index.php
vim index.php
<?php
phpinfo();?>
提供php服務子配置文件 -> www.conf
這個配置文件內容太多就不貼出來了,可以跑一個php容器獲取或者通過其他安裝php的機器獲取,文章中所有文件的打包上文也有鏈接
只需修改其中listene這一項
listen = 0.0.0.0:9000
準備mysql所需文件
最後我們也爲mysql提供一個配置文件,但mysql就不做過多配置,只簡單給出兩個參數驗證可行性。
提供mysql配置文件 -> server.cnf
這個配置文件內容太多就不貼出來了,可以跑一個mysql容器獲取或者通過其他安裝mysql的機器獲取,文章中所有文件的打包上文也有鏈接。
簡單修改[mysqld]這個配置段中,如下兩個參數
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
到此我們需要的文件已經準備完成。
4、編寫playbook
vim lnmp.yaml
# 安裝軟件包
- hosts: node01
remote_user: root
tasks:
- name: install epel
yum: name=epel-release state=latest
- name: install nginx
yum: name=nginx state=latest
- hosts: node02
remote_user: root
tasks:
- name: install epel
yum: name=epel-release state=latest
- name: install php mysql
yum: name={{ item }} state=latest
with_items:
- php-fpm
- php-mysql
- php-mbstring
- php-mcrypt
- mariadb-server
#配置並啓動服務
- hosts: node01
remote_user: root
tasks:
- name: mkdir nginx root
shell: mkdir -pv /data/nginx/html
- name: copy nginx config
copy: src=/ansible/index.html dest=/data/nginx/html/
- name: copy nginx config
copy: src=/ansible/phpMyAdmin-4.4.15-all-languages.tar.gz dest=/data/nginx/html/
- name: tar zxvf phpMyAdmin-4.4.15-all-languages.tar.gz
shell: tar zxvf /data/nginx/html/phpMyAdmin-4.4.15-all-languages.tar.gz -C /data/nginx/html/
- name: copy pma.sh
copy: src=/ansible/node01-pma.sh dest=/data/nginx/html/
- name: exec pma.sh
shell: bash /data/nginx/html/node01-pma.sh
- name: copy config inc php
copy: src=/ansible/config.inc.php dest=/data/nginx/html/pma/
- name: copy nginx config jjf.conf
copy: src=/ansible/jjf.conf dest=/etc/nginx/conf.d
- name: start nginx service
service: name=nginx state=restarted
- hosts: node02
remote_user: root
tasks:
- name: mkdir /var/lib/php/session
shell: mkdir -pv /var/lib/php/session/ && chown apache:apache /var/lib/php/session/
- name: copy www.conf
copy: src=/ansible/www.conf dest=/etc/php-fpm.d/
- name: start php-fpm
service: name=php-fpm state=restarted
- name: copy nginx config
copy: src=/ansible/phpMyAdmin-4.4.15-all-languages.tar.gz dest=/appdata/
- name: tar zxvf phpMyAdmin-4.4.15-all-languages.tar.gz
shell: tar zxvf /appdata/phpMyAdmin-4.4.15-all-languages.tar.gz -C /appdata/
- name: copy pma.sh
copy: src=/ansible/node02-pma.sh dest=/appdata
- name: bash pma.sh
shell: bash /appdata/node02-pma.sh
- name: copy config.inc.php
copy: src=/ansible/config.inc.php dest=/appdata/pma
- name: copy config index.php
copy: src=/ansible/index.php dest=/appdata/
tags: copyindexphp
- name: copy mariadb config
copy: src=/ansible/server.cnf dest=/etc/my.cnf.d/
- name: start mysql
service: name=mariadb state=restarted
5、運行playbook
語法檢查和dryrun測試一下
ansible-playbook --syntax-check lnmp.yaml
ansible-playbook -C lnmp.yaml
執行playbook
ansible-playbook lnmp.yaml
6、驗證結果
測試nginx安裝情況
測試php安裝情況
最後測試phpmyadmin安裝情況
phpmyadmin默認情況下,如果mysql的root爲空是拒絕登錄的。如果需要登錄root需要有密碼
mysqladmin -u root -p password '你的密碼'
設置root密碼後可以登錄到web界面上,查看驗證我們LAMP的各種版本信息