编写Ansible角色实现分布式LNMP安装
# 在Ansible的roles/lnmp/tasks/目录下创建一个install.yml文件
---
# 安装Nginx
- name: Install Nginx
apt:
name: nginx
state: present
# 启动并启用Nginx服务
- name: Start and Enable Nginx Service
systemd:
name: nginx
state: started
enabled: yes
# 安装MySQL
- name: Install MySQL
apt:
name: mysql-server
state: present
# 启动并启用MySQL服务
- name: Start and Enable MySQL Service
systemd:
name: mysql
state: started
enabled: yes
# 安装PHP及常用扩展
- name: Install PHP and Extensions
apt:
name: "{{ item }}"
state: present
loop:
- php-fpm
- php-mysql
- php-xml
- php-curl
- php-gd
- php-imap
# 启动并启用PHP-FPM服务
- name: Start and Enable PHP-FPM Service
systemd:
name: php{{ php_version }}-fpm
state: started
enabled: yes
# 复制配置文件并重启服务
- name: Copy Configuration Files and Restart Services
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
notify:
- Restart Nginx Service
- Restart PHP-FPM Service
loop:
- { src: "files/nginx.conf", dest: "/etc/nginx/nginx.conf" }
- { src: "files/php-fpm.conf", dest: "/etc/php/{{ php_version }}/fpm/php-fpm.conf" }
- { src: "files/www.conf", dest: "/etc/php/{{ php_version }}/fpm/pool.d/www.conf" }
# 文件变更后需要重启的任务
handlers:
- name: Restart Nginx Service
systemd:
name: nginx
state: restarted
- name: Restart PHP-FPM Service
systemd:
name: php{{ php_version }}-fpm
state: restarted
# 在Ansible的roles/lnmp/files/目录下准备相应的配置文件
# nginx.conf, php-fpm.conf, www.conf等配置文件
这个Ansible角色实现了LNMP架构的分布式安装,包括安装Nginx、MySQL和PHP及常用的PHP模块,并配置PHP-FPM和Nginx。在实际使用时,需要调整相关的配置文件路径和版本号等变量。
评论已关闭