環境準備
服務器系統 |
Centos6.8 |
內存 |
1G |
CPU |
2核 |
IP地址 |
10.0.0.20 |
服務 |
MysqL數據庫 |
服務器系統 |
Centos6.8 |
內存 |
1G |
CPU |
2核 |
IP地址 |
10.0.0.21 |
服務 |
Ansible管理主機 |
服務器系統 |
Centos6.8 |
內存 |
1G |
CPU |
2核 |
IP地址 |
10.0.0.22 |
服務 |
LNMP環境的搭建並上線電商項目 |
在21Ansible管理主機上操作,我們需要準備Nginx源碼包和電商包
創建目錄
mkdir lnmp
進入目錄
cd lnmp/
解壓上傳到服務器上的nginx包
tar zxf nginx-1.12.0.tar.gz
進入解壓過後的nginx目錄
cd nginx-1.12.0/conf/
拷貝Nginx配置文件至lnmp目錄下
cp nginx.conf ../../
cd ../../
刪除nginx目錄
rm -rf nginx-1.12.0
編寫php測試頁面
vim index.php
<?php
phpinfo();
?>
將php測試頁面文件改名
mv index.php info.php
vim /etc/ansible/hosts
編輯nginx配置文件
vim /root/lnmp/nginx.conf
編輯yml文件
vim lnmp.yml
---
- hosts: webservers
vars:
- NGINX_USER: www
- PORT: 8000
- ROOT: /opt/html
tasks:
- name: "分發nginx源碼包"
unarchive: src=nginx-1.12.0.tar.gz dest=/root/
- name: "安裝nginx依賴庫和環境"
yum: name=gcc,gcc-c++,pcre-devel,zlib-devel,php,php-mysql,php-gd,php-fpm,unzip state=latest
- name: "編譯安裝nginx"
shell: cd /root/nginx-1.12.0 && ./configure && make && make install
- name: "創建{{NGINX_USER}}用戶"
user: name={{NGINX_USER}} shell=/sbin/nologin
- name: "推送nginx配置文件"
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
tags: nginx-config
notify: reload-nginx
- name: "創建{{ROOT}}"
file: path={{ROOT}} state=directory owner=www group=www mode=777
- name: "關閉防火牆"
service: name=iptables state=stopped
- name: "啓動nginx"
shell: /usr/local/nginx/sbin/nginx
- name: "啓動php-fpm"
service: name=php-fpm state=started
- name: "分發測試頁面"
copy: src=info.php dest={{ROOT}}
- name: "推送電商源碼包到服務器"
unarchive: src=tinyshopV2.5_data.zip dest={{ROOT}} mode=777
handlers:
- name: "reload-nginx"
shell: /usr/local/nginx/sbin/nginx -s reload
- hosts: dbservers
tasks:
- name: "安裝mysql"
yum: name=mysql,mysql-server state=latest
- name: "啓動MySQL"
service: name=mysqld state=restarted enabled=yes
- name: "關閉防火牆"
service: name=iptables state=stopped
- name: "創建數據庫"
shell: mysql -e "create database ds charset utf8;"
- name: "給LNMP環境的主機授權遠程登錄賬號"
shell: mysql -e "grant all on ds.* to 'aaronszm'@'10.0.0.%' identified by '123';"
檢查yml文件的語法是否正確
ansible-playbook --syntax-check lnmp.yml
執行ansible劇本命令
ansible-playbook lnmp.yml