ansible服务器 192.168.145.15
nginx 服务器 192.168.145.30
mysql 服务器 192.168.145.45
php 服务器 192.168.145.60
#管理端安装 ansible
yum install -y epel-release #先安装 epel 源
yum install -y ansible
#配置密钥对验证
ssh-keygen -t rsa
vim /etc/ssh/ssh_config
-----35行,取消注释-----
StrictHostKeyChecking no
sshpass -p '123' ssh-copy-id root@192.168.145.30
sshpass -p '123' ssh-copy-id root@192.168.145.45
sshpass -p '123' ssh-copy-id root@192.168.145.60
#配置主机清单
cd /etc/ansible
vim hosts
[nginx]
192.168.145.30
[mysql]
192.168.145.45
[php]
192.168.145.60
#创建一个目录用来存放 playbooks 的文件
cd /etc/ansible/
mkdir playbooks
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#准备nginx源,进行yum安装
cd /opt/
mv nginx.repo /etc/ansible/playbooks/
cd /etc/ansible/playbooks/
#配置 nginx 支持 PHP 解析
cd /etc/nginx/conf.d
vim default.conf
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
......
location ~ \.php$ {
root html;
fastcgi_pass 192.168.145.60:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; # $document_root 和 root 属性一样
include fastcgi_params;
}
cp default.conf /etc/ansible/playbooks/default.conf.j2
#准备php文件
vim /etc/ansible/playbooks/index.php
<?php
phpinfo();
?>
#测试mysql是否能连接成功
vim /etc/ansible/playbooks/test.php
<?php
$link=mysqli_connect('192.168.145.45','root','Admin@123');
if($link) echo "Success!!
";
else echo "Fail!!";
?>
vim lnmp.yaml
- name: nginx play
hosts: nginx
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: true
- name: disable firewalld
service: name=firewalld state=stopped
- name: copy nginx yum repo file
copy: src=nginx.repo dest=/etc/yum.repos.d/
- name: install nginx
yum: name=nginx state=latest
- name: copy index.php
copy: src=index.php dest=/usr/share/nginx/html
- name: copy nginx template configuration file
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
- name: start nginx
service: name=nginx state=started enabled=yes
#在ansible服务器运行
cd /etc/ansible/playbooks/
ansible-playbook lnmp.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook lnmp.yaml
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HHMJQfy8-1690849321990)(C:/Users/86138/AppData/Roaming/Typora/typora-user-images/image-20230731130407255.png)]
#在 nginx 服务器查看
systemctl status nginx
#安装 mysql 5.7
cd /etc/yum.repos.d
wget -