阅读量:0
使用Ansible Playbooks进行分布式部署LNMP(Linux, Nginx, MySQL, PHP)是一种高效的方法。以下是一个详细的指南,包含Ansible Playbook的创建和配置,以便在多个服务器上自动化部署LNMP栈。
1. 安装Ansible
首先,在控制节点(通常是你的管理机器)上安装Ansible。
sudo yum install epel-release -y sudo yum install ansible -y
2. 设置主机清单
创建一个主机清单文件,定义你的服务器。
# /etc/ansible/hosts [webservers] webserver1 ansible_host=192.168.1.101 webserver2 ansible_host=192.168.1.102 [dbservers] dbserver ansible_host=192.168.1.103
3. 创建Playbooks
3.1. 全局变量文件
创建一个包含全局变量的文件。
# group_vars/all.yml --- nginx_version: "1.20.1" mysql_root_password: "your_root_password" mysql_database: "example_db" mysql_user: "example_user" mysql_password: "example_password" php_version: "7.4"
3.2. 安装Nginx的Playbook
创建用于安装和配置Nginx的Playbook。
# playbooks/nginx.yml --- - hosts: webservers become: yes tasks: - name: 安装Nginx yum: name: nginx state: present - name: 启动并启用Nginx服务 systemd: name: nginx state: started enabled: yes - name: 配置Nginx template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: - restart nginx handlers: - name: restart nginx systemd: name: nginx state: restarted
创建一个Nginx配置模板。
# templates/nginx.conf.j2 user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
3.3. 安装MySQL的Playbook
创建用于安装和配置MySQL的Playbook。
# playbooks/mysql.yml --- - hosts: dbservers become: yes tasks: - name: 安装MySQL yum: name: mysql-server state: present - name: 启动并启用MySQL服务 systemd: name: mysqld state: started enabled: yes - name: 设置MySQL root密码 mysql_user: name: root host: localhost password: "{{ mysql_root_password }}" login_unix_socket: /var/lib/mysql/mysql.sock - name: 创建数据库 mysql_db: name: "{{ mysql_database }}" state: present login_user: root login_password: "{{ mysql_root_password }}" - name: 创建数据库用户并授予权限 mysql_user: name: "{{ mysql_user }}" password: "{{ mysql_password }}" priv: "{{ mysql_database }}.*:ALL" state: present login_user: root login_password: "{{ mysql_root_password }}"
3.4. 安装PHP的Playbook
创建用于安装和配置PHP的Playbook。
# playbooks/php.yml --- - hosts: webservers become: yes tasks: - name: 安装EPEL仓库 yum: name: epel-release state: present - name: 安装Remi仓库 yum: name: http://rpms.remirepo.net/enterprise/remi-release-7.rpm state: present - name: 启用Remi PHP仓库 yum_repository: name: remi-php{{ php_version | replace('.', '') }} enabled: yes - name: 安装PHP和扩展 yum: name: "{{ item }}" state: present with_items: - php - php-fpm - php-mysqlnd - name: 启动并启用PHP-FPM服务 systemd: name: php-fpm state: started enabled: yes
4. 运行Playbooks
运行Ansible Playbooks来部署LNMP。
ansible-playbook playbooks/nginx.yml ansible-playbook playbooks/mysql.yml ansible-playbook playbooks/php.yml
5. 检查和验证
确认Nginx、MySQL和PHP已正确安装和配置。你可以访问Nginx默认页面并通过MySQL命令行工具检查数据库和用户的创建。
总结
通过上述步骤,你可以使用Ansible自动化工具在多个服务器上分布式部署LNMP栈。这种方法确保了安装过程的一致性,并简化了大规模部署和管理的复杂性。