1. 最终效果及说明

1)主机说明:

ansible: 192.168.20.121
webserver: 192.168.20.122

2)最终访问效果如下

结果与test2.php里边的内容一致,证明LNMP平台完好

3)剧本文件展示及说明

[root@localhost lnmp]# ls
modifynginx.yml  mysql.yml  php.yml       startservice.yml  test2.php
modifyphp.yml    nginx.yml  sequence.txt  test1.php         testpage.yml[root@localhost lnmp]# cat sequence.txt
sequence of these yml file:
1. nginx.yml
2. mysql.yml
3. php.yml
4. modifyphp.yml
5. modifynginx.yml
6. startservice.yml
7. testpage.ymlnotice:
1. you can use command in ansible's shell to test the lnmp, normally, test test2.php is enough if it show correct message.
2. you can write thest yml file in one combined yml file in order, I split these for convience of checking error

2. 各个yml文件内容展示

1) nginx.yml

[root@localhost lnmp]# cat nginx.yml
- hosts: webserverremote_user: roottasks:- name: add useruser: name=nginx shell=/sbin/nologin create_home=no- name: resolve dependencyyum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++- name: unarchive tarballunarchive: src=/root/nginx-1.15.4.tar.gz dest=/usr/src- name: configure and install nginxshell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module && make -j4 && make installargs:chdir: /usr/src/nginx-1.15.4- name: make soft link filefile: path=/usr/sbin/nginx src=/usr/local/nginx/sbin/nginx state=link

2)mysql.yml

[root@localhost lnmp]# cat mysql.yml
- hosts: webserverremote_user: roottasks:- name: resolve dependencyyum: name=ncurses-devel,bison,cmake- name: unarchive tarballunarchive: src=/root/mysql-5.5.22.tar.gz dest=/usr/src- name: configure and install mysqlshell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make -j4 && make installargs:chdir: /usr/src/mysql-5.5.22- name: add user mysqluser: name=mysql shell=/sbin/nologin create_home=no- name: change owner of mysql directoryfile: path=/usr/local/mysql owner=mysql group=mysql recurse=yes- name: link lib filefile: path=/usr/lib/libmysqlclient.so.18 src=/usr/local/mysql/lib/libmysqlclient.so.18 state=link- name: copy configuration filecopy: src=/usr/src/mysql-5.5.22/support-files/my-large.cnf dest=/etc/my.cnf remote_src=yes- name: copy command scriptcopy: src=/usr/src/mysql-5.5.22/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes- name: modify command scriptreplace: path=/etc/init.d/mysqld regexp="^basedir=" replace="basedir=/usr/local/mysql/"- name: modify command scriptreplace: path=/etc/init.d/mysqld regexp="^datadir=" replace="datadir=/usr/local/mysql/data/"- name: give executable privilege to scripttfile: path=/etc/init.d/mysqld mode=0755- name: initialize mysql serviceshell: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql- name: make soft link of mysql commandshell: ln -s /usr/local/mysql/bin/* /usr/bin- name: start mysqlshell: /etc/init.d/mysqld start- name: change passwordshell: mysqladmin -uroot password 123456

3)php.yml

[root@localhost lnmp]# cat php.yml
- hosts: webserverremote_user: roottasks:- name: resolve dependencyyum: name=libxml2-devel,gd,libjpeg-devel,libpng-devel- name: unarchive tarballunarchive: src=/root/php-7.2.0.tar.gz dest=/usr/src- name: configure phpshell: ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-jpeg-dir=/usr/lib --with-pdo-mysql=mysqlnd --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php --enable-mbstring --enable-fpmargs:chdir: /usr/src/php-7.2.0- name: install phpshell: make -j8 && make install args:chdir: /usr/src/php-7.2.0- name: copy configuration filecopy: src=/usr/src/php-7.2.0/php.ini-development dest=/usr/local/php/php.ini remote_src=yes- name: copy configuration filecopy: src=/usr/src/php-7.2.0/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes- name: copy configuration filecopy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes- name: copy configuration filecopy: src=/usr/local/php/etc/php-fpm.d/www.conf.default dest=/usr/local/php/etc/php-fpm.d/www.conf remote_src=yes- name: give executable privilegefile: path=/etc/init.d/php-fpm mode=0755- name: make soft link of command scriptshell: ln -s /usr/local/php/bin/* /usr/bin- name: make soft link of command scriptshell: ln -s /usr/local/php/sbin/* /usr/sbin

4)modifyphp.yml

[root@localhost lnmp]# cat modifyphp.yml
- hosts: webserverremote_user: roottasks:- name: modify php.inireplace: path=/usr/local/php/php.ini regexp="^short_open_tag = Off" replace="short_open_tag = On"- name: modify php-fpm.conf 1replace: path=/usr/local/php/etc/php-fpm.conf regexp="^;pid = run/php-fpm.pid" replace="pid = run/php-fpm.pid"- name: modify php-fpm.conf 2replace: path=/usr/local/php/etc/php-fpm.conf regexp="^; process.max = 128" replace="process.max = 128"- name: modify php-fpm.conf 3replace: path=/usr/local/php/etc/php-fpm.conf regexp="^;events.mechanism = epoll" replace="events.mechanism = epoll"

5) modifynginx.yml

[root@localhost lnmp]# cat modifynginx.yml
- hosts: webserverremote_user: roottasks:- name: modify 1replace: path=/usr/local/nginx/conf/nginx.conf regexp="^            index  index.html index.htm;" replace="            index  index.php index.html index.htm;"- name: remove "#"shell: sed -i '65,71s/#/\ /g' /usr/local/nginx/conf/nginx.conf- name: modify php locationreplace: path=/usr/local/nginx/conf/nginx.conf regexp="fastcgi_params" replace="fastcgi.conf"

6)startservice.yml

[root@localhost lnmp]# cat startservice.yml
- hosts: webserverremote_user: roottasks:- name: start nginxshell: nginx- name: start phpshell: /etc/init.d/php-fpm start

7)testpage.yml

[root@localhost lnmp]# cat testpage.yml
- hosts: webserverremote_user: roottasks:- name: copy test1.phpcopy: src=/root/test1.php dest=/usr/local/nginx/html/- name: copy test2.phpcopy: src=/root/test2.php dest=/usr/local/nginx/html/

8) test page

[root@localhost lnmp]# cat test1.php
<?php
phpinfo();
?>
[root@localhost lnmp]# cat test2.php
<?php
$con = new mysqli('127.0.0.1','root','123456','test');
if (!$con)die("connect error:".mysqli_connect_error());
elseecho "connect mysql successfully\n";
?>

3. ansible playbook使用说明

  • 不要太在乎warning,有的时候使用在shell模块中使用sed命令比直接使用replace模块更高效
  • 尽量用专门的模块做专门的事情,比如用unarchive模块解压,用file模块做连接,用user模块添加用户等
  • 上边写这些的时候是分开写的 便于测试可用性 如果是想要一个整体的 把他们写在一起就好了

使用ansible自动化搭建LNMP应用平台相关推荐

  1. 用ansible自动化搭建web、sql服务器、lvs调度器

    一.服务器角色 二.部署ansible 1.ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.f ...

  2. python django ansible自动化运维管理平台源码收藏

    yianjiajia / django_web_ansible GitHub - yianjiajia/django_web_ansible: 自动化运维管理系统 纳兰秋水 / AnsibleUI A ...

  3. Ansible搭建LNMP

    用ansible剧本搭建lnmp 首先在主服务器上搭建ansible直接用云yum装就可以, yum -y install ansible 如果copy报错一下的语句 "msg": ...

  4. docker搭建lnmp环境

    1.搭建lnmp网站平台 1.创建mysql数据库容器 docker run -it -d --name lnmp_mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD= ...

  5. 编写shell脚本实现自动化搭建安装LNMP平台全过程配置详解

    注意:如果是输入的是字母的或者是输入等于0时,则会出现以下两种情况!!! 查看端口: 进到Nginx根目录查看创建好的测试网页: 注意:关闭防火墙或者设置防火墙规则!!! 访问Nginx网页: 访问P ...

  6. 搭建LNMP平台加NFS文件共享部署wordpress博客

    搭建LNMP平台部署wordpress博客 安装LNMP平台 安装nginx 安装PHP 搭建mysql数据 搭建NFS 安装LNMP平台 安装nginx 1)下载官方源 [root@Web ~]# ...

  7. LNMP网站平台搭建

    一.搭建LNMP平台 案例需求:在ip地址 192.168.1.10的服务器上搭建LNMP平台 系统环境准备: 配置固定Ip地址.关闭 iptables     selinux 配置yum源 安装开发 ...

  8. 手把手教你搭建数据库服务器平台 | DBA VS 自动化运维,究竟谁与争锋?

    现代化的程度越高,对数据库的依赖性越大.数据安全性和系统的安全性也就越大,比如公司业务系统.数据库是直接的存储地方,宕机带来的损失可能是按分钟或者秒算的.而谁对这些数据库负责--DBA.所以很多公司, ...

  9. K8S实战集训第一课 Ansible自动化部署k8s、弹性伸缩、Helm包管理、k8s网络模型介绍

    Ansible自动化部署K8S集群 一.Ansible自动化部署K8S集群 1.1 Ansible介绍 Ansible是一种IT自动化工具.它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部 ...

最新文章

  1. Maven 新版本 3.8.1 打包报错 maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories
  2. Mysql HA实现MYSQL的高可用
  3. 为什么说企业做网站不要选择或找低价便宜的网站建设制作公司?
  4. 工作中用到的设计模式?
  5. 线程池状态和使用注意点
  6. MongoDB:使用Spring数据添加计数器
  7. swift基础之_swift调用OC/OC调用swift
  8. 笔记本电脑触摸板的正确使用方法 --转摘
  9. SDUT_2012省赛选拔赛2 部分题目
  10. 邢台市初中计算机考试,2019年邢台中考总分多少分,邢台中考各个科目多少分
  11. java中jsp table标签属性_JSP自定义标签-属性
  12. 数据库文件和文件组的类别
  13. 阿里云ECS后台CPU占用100%,top却找不到
  14. linux 校园网 自动认证,使用 Padavan 路由器实现校园网自动 Web 认证
  15. 2023年Android现代开发
  16. npm 安装淘宝镜像报错 npm ERR! code EEXIST
  17. 南京师范大学生物学考研经验分享
  18. 如何参与linux内核开发
  19. HyperLedger Fabric中Fabric-CA的使用
  20. 3D打印机(Prusa I2)DIY经验分享(Part I)

热门文章

  1. 【读论文】点云信息提取研究进展和展望(2017)
  2. 简单生活之选:6个减少劳碌、提高产出的模式
  3. 搭建智能语音交互系统
  4. 计算机对象的概念对象的特性,面向对象的基本概念
  5. DNS 负载均衡、硬件负载均衡和软件负载均衡
  6. 求平均查找长度(成功+失败)
  7. iOS-Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d
  8. excel日历弄到html,Excel中进行添加日历控件的两种方法
  9. 秀刻Showlike+
  10. 理清ACP/IPOC 2020会议背后的细节——会议论文不再神秘