使用ansible自动部署LNMP架构
时间: 2024-02-05 15:05:12 浏览: 130
1. 安装ansible
在一台主控机上安装ansible,并将需要部署的目标机器的IP地址加入到ansible的hosts文件中。
2. 编写playbook
使用yaml格式编写ansible playbook文件,包括以下任务:
- 安装Nginx、MySQL和PHP
- 修改Nginx配置文件,配置虚拟主机和反向代理
- 修改MySQL配置文件,设置root密码和字符集
- 部署PHP应用程序文件和配置文件
示例playbook文件如下:
```yaml
---
- hosts: webservers
become: true
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- mysql
- mysql-server
- php
- php-mysql
- php-fpm
- name: Start services
service:
name: "{{ item }}"
state: started
enabled: true
with_items:
- nginx
- mysqld
- php-fpm
- name: Configure Nginx
copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
notify:
- Reload Nginx
- name: Configure MySQL
copy:
src: files/my.cnf
dest: /etc/my.cnf
notify:
- Restart MySQL
- name: Deploy PHP application
copy:
src: files/php_app
dest: /usr/share/nginx/html
- name: Configure PHP
copy:
src: files/php.ini
dest: /etc/php.ini
notify:
- Restart PHP-FPM
handlers:
- name: Reload Nginx
service:
name: nginx
state: reloaded
- name: Restart MySQL
service:
name: mysqld
state: restarted
- name: Restart PHP-FPM
service:
name: php-fpm
state: restarted
```
3. 准备文件
将需要部署的应用程序文件和配置文件打包成tar.gz文件,并放置在主控机上。
4. 执行playbook
在主控机上执行ansible-playbook命令,指定playbook文件和目标机器的IP地址,以及需要部署的应用程序文件和配置文件的路径。
```bash
ansible-playbook -i hosts playbook.yml --extra-vars "app_file=/path/to/app.tar.gz"
```
5. 验证部署
访问Nginx的虚拟主机地址,验证应用程序是否正常运行。同时,使用MySQL客户端连接数据库,验证数据库是否正常运行并包含正确的数据。
阅读全文
相关推荐
















