使用ansible一键部署LNMP
时间: 2023-11-23 16:51:39 浏览: 116
LNMP平台一键部署脚本
您可以使用Ansible来实现一键部署LNMP(Linux + Nginx + MySQL + PHP)环境。以下是一个简单的Ansible playbook示例,用于在目标主机上安装和配置LNMP环境:
```yaml
---
- name: Deploy LNMP
hosts: target_hosts
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install required packages
apt:
name: "{{ item }}"
state: present
with_items:
- nginx
- mysql-server
- php-fpm
- php-mysql
- name: Configure Nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
- name: Configure PHP
template:
src: php.ini.j2
dest: /etc/php/7.4/fpm/php.ini
notify: restart php-fpm
- name: Start and enable services
service:
name: "{{ item }}"
state: started
enabled: true
with_items:
- nginx
- php7.4-fpm
- mysql
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
- name: restart php-fpm
service:
name: php7.4-fpm
state: restarted
```
将上述的 playbook 保存为 `deploy_lnmp.yml` 文件,
然后可以使用以下命令来运行Ansible playbook:
```
ansible-playbook -i inventory.ini deploy_lnmp.yml
```
请确保将 `target_hosts` 替换为您的目标主机,例如使用Ansible的 inventory 文件指定目标主机。
阅读全文