当前位置: 首页 > news >正文

ansible-lnmp

 
需求:
本地master服务器安装ansible服务给远程两台机器node11、node12部署lnmp服务。
 
环境:
三台机器都是centos7系统,关闭防火墙,关闭selinux,配置主机名,都能联网互相通信。三台机器主机名信息如下:
# cat /etc/hosts
192.168.1.10 master
192.168.1.11 node11
192.168.1.12 node12
 
============================================================================
过程:
 

image

 

master端
yum 配置SSH,导入epel-release源用于安装nginx,然后yum安装lnmp。
在/home/ansible_file/目录下准备四个文件nginx.conf,8080.conf,my.cnf,test.php
  • nginx.conf:nginx主配置文件
  • 8080.conf:nginx以端口8080命名的站点配置文件
  • my.cnf:数据库mariadb配置文件
  • test.php:用于php测试文件
 
配置ansible_playbook,执行playbook
 
node端
测试,查看端口和网页。
# ps -ef | grep nginx
# ps -ef | grep mariadb
# ps aux | grep nginx
# ps aux | grep mariadb
# lsof -i :8080
http://192.168.1.11:8080/test.html
http://192.168.1.12:8080/test.php
 
============================================================================
master上安装
master端配置SSH与node11、node12节点建立互信。
# ssh-keygen -t rsa 在master端生成rsa的公钥和私钥
# ls /root/.ssh/
id_rsa id_rsa.pub
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.11 将master端公钥复制到node11上
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.12 将master端公钥复制到node12上
 
查看结果,注意服务器时间同步
# ssh root@192.168.1.11 'date' master上查看node11的时间
# ssh root@192.168.1.12 'date' master上查看node12的时间
 
=============================================================================
master端导入epel-release源安装nginx,然后yum安装mariadb和php。
# yum install epel-release -y
# yum install nginx -y
# yum install mariadb mariadb-server -y
# yum install -y php php-gd php-mysql gd php-fpm
============================================================================
master端配置node节点需要的四个文件:
nginx默认只有主配置文件/etc/nginx/nginx.conf生效,站点配置文件目录/etc/nginx/conf.d/为空,这里将主配置文件和每个网站站点分开,站点配置文件目录里专门放站点配置文件。
一般站点配置文件以站点命名,这里以8080端口命名/etc/nginx/conf.d/8080.conf。
 
1.注释主配置文件/etc/nginx/nginx.conf里server的全部内容,然后把/etc/nginx/conf.d/8080.conf复制到/home/目录下。
# cp /etc/nginx/nginx.conf /home/nginx.conf
2.编辑/home/8080.conf站点配置文件。
# vim /home/8080.conf
server {
listen {{ nginx_port }};
server_name {{ ansible_fqdn }};
index index.php index.html index.htm default.html default.htm default.php;
root /usr/share/nginx/html;
location / {
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
 
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
 
3.复制并编辑数据库mariadb配置文件。
# cp /etc/my.cnf /home/my.cnf
# vim /home/my.cnf
10 port={{ mysql_port }}
 
4.编辑php网页测试文件。
# vim /home/test.php
phpinfo();
?>
 
============================================================================
master端安装配置ansible。
# cd /etc/yum.repos.d/
# wget http://mirrors.aliyun.com/repo/epel-7.repo
# yum clean all
# yum install ansible -y 安装ansible
 
# yum list all "*ansible*" 查看ansible安装包
# yum info ansible 查看ansible安装信息
# ls
ansible.cfg hosts roles
 
# vim /etc/ansible/hosts
12 [websrvs]
13 192.168.1.11 nginx_port=8080 mysql_port=6666
14 192.168.1.12 nginx_port=8080 mysql_port=6666
 
# mkdir -pv /etc/ansible/ansible_playbooks/roles/websrvs/{tasks,files,templates,meta,handlers,vars}
# ls /etc/ansible/ansible_playbooks/roles/websrvs/
files handlers meta tasks templates vars
 
# vim /etc/ansible/ansible_playbooks/site.yml
- hosts: websrvs
remote_user: root
roles:
- websrvs
 
# cp /home/nginx.conf /etc/ansible/ansible_playbooks/roles/websrvs/templates/nginx.conf.j2
# cp /home/my.cnf /etc/ansible/ansible_playbooks/roles/websrvs/templates/my.cnf.j2
# cp /home/8080.conf /etc/ansible/ansible_playbooks/roles/websrvs/templates/8080.conf.j2
# cp /home/test.php /etc/ansible/ansible_playbooks/roles/websrvs/files/test.php
 
# vim /etc/ansible/ansible_playbooks/roles/websrvs/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
- name: restart mariadb
service: name=mariadb state=restarted
- name: restart php-fpm
service: name=php-fpm state=restarted
 
 
# vim /etc/ansible/ansible_playbooks/roles/websrvs/tasks/main.yml
- name: install epel-release
yum: name=epel-release
 
- name: install nginx package
yum: name=nginx
- name: copy nginx.conf file
template: src=/etc/ansible/ansible_playbooks/roles/websrvs/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: copy 8080.conf file
template: src=/etc/ansible/ansible_playbooks/roles/websrvs/templates/8080.conf.j2 dest=/etc/nginx/conf.d/8080.conf
tags:
- nginx
notify:
- restart nginx
- name: start nginx
service: name=nginx enabled=true state=started
 
- name: copy test.php
copy: src=test.php dest=/usr/share/nginx/html/test.php
 
- name: install mariadb packages
yum: name={{ packages }} state=latest
vars:
packages:
- mariadb
- mariadb-server
- name: copy my.cnf file
template: src=/etc/ansible/ansible_playbooks/roles/websrvs/templates/my.cnf.j2 dest=/etc/my.cnf
tags:
- mariadb
notify:
- restart mariadb
- name: start mariadb service
service: name=mariadb enabled=true state=started
 
- name: install php-fpm packages
yum: name={{ packages }} state=latest
vars:
packages:
- php
- php-fpm
- php-mysql
- php-gd
- gd
tags:
- php-fpm
notify:
- restart php-fpm
- name: start php-fpm service
service: name=php-fpm enabled=true state=started
 
# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── ansible_playbooks
│   ├── roles
│   │   └── websrvs
│   │   ├── files
│   │   │   └── test.php
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   ├── 8080.conf.j2
│   │   │   ├── my.cnf.j2
│   │   │   └── nginx.conf.j2
│   │   └── vars
│   └── site.yml
├── hosts
└── roles
 
# cd /etc/ansible/ansible_playbooks
# ansible-playbook -C site.yml
# ansible-playbook site.yml
 
node上测试:
远程主机上查看结果
# ps -ef | grep nginx
# ps -ef | grep mariadb
# ps aux | grep nginx
# ps aux | grep mariadb
# lsof -i :8080
# lsof -i :6666
 
浏览器加端口访问
http://192.168.1.11:8080/test.php
http://192.168.1.12:8080/test.php
 
 
 
http://www.wuyegushi.com/news/1013.html

相关文章:

  • STRING 类型字段创建索引总结
  • 更改用户名
  • B2025 输出字符菱形
  • 常见的慢SQL示例及其优化后的SQL脚本
  • B2002 Hello,World!
  • python 中 ORM 是什么 如何使用?
  • 盈鹏飞嵌入式带你玩转T113系列tina5 SDK(1)-Tina SDK开发环境搭建
  • 揭秘LummaStealer恶意软件:虚假验证码如何攻陷预订网站
  • 2025年最佳网络监控工具Top8
  • salt安装配置
  • 代码雨
  • WIFI模块-ESP芯片开发框架(ESP32/ESP8266)
  • IQ-tree绘制进化树3-验证
  • 9.回文数
  • Linux 核心目录说明
  • PCIe扫盲——Ack Nak 机制详解(一)
  • OI 数学定理(提高级)
  • BSC系统合约详解 - 若
  • 机器学习,深度学习,神经网络三者的联系和区别
  • 软考系统分析师每日学习卡 | [日期:2025-07-21] | [今日主题:成本效益分析]
  • B2007 A + B 问题
  • 虚拟内存(交换分区)
  • 【langchain】检索
  • win11无法自动休眠
  • RocketMq集群docker部署(2主2从+Dashboard)
  • 带团队后的日常思考(十七)
  • k8s里的taints 和 tolerations
  • 【 IEEE出版】第五届先进算法与神经网络国际学术会议(AANN 2025)
  • 统信 UOS 安装 svn 指南
  • 【IEEE出版】2025年能源技术与电气工程国际学术会议(ETEE 2025)