ansible部署lnmp详细流程
时间: 2023-09-19 14:10:18 浏览: 91
好的,以下是ansible部署lnmp的详细流程:
1. 在本地安装好ansible,并且确保能够连接到需要部署的远程主机。
2. 在ansible安装目录下,创建一个新的目录,用于存放lnmp的相关文件。
3. 在该目录下,新建一个名为hosts的文件,将需要部署的远程主机IP地址或者主机名添加到该文件中。
4. 编写一个名为playbook.yml的ansible playbook文件,用于定义lnmp的安装和配置过程。例如:
```
---
- name: Deploy LNMP
hosts: all
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
tags: [nginx]
- name: Install MySQL
apt:
name: mysql-server
state: present
tags: [mysql]
- name: Install PHP
apt:
name: php-fpm
state: present
tags: [php]
- name: Configure Nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0644
notify:
- reload nginx
tags: [nginx]
- name: Configure PHP
template:
src: php.ini.j2
dest: /etc/php/7.4/fpm/php.ini
mode: 0644
notify:
- restart php-fpm
tags: [php]
handlers:
- name: reload nginx
service:
name: nginx
state: restarted
- name: restart php-fpm
service:
name: php7.4-fpm
state: restarted
```
该playbook文件中定义了在所有远程主机上安装nginx、mysql和php,并且配置了nginx和php的相关文件。
5. 编写Nginx和PHP的配置文件模板,例如:
nginx.conf.j2:
```
user www-data;
worker_processes auto;
events {
worker_connections 768;
# multi_accept on;
}
http {
server {
listen 80;
server_name example.com;
root /var/www/example.com;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
```
php.ini.j2:
```
[PHP]
short_open_tag = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log
[Date]
date.timezone = Asia/Shanghai
```
6. 在ansible控制机上,执行以下命令,开始部署lnmp:
```
ansible-playbook playbook.yml -t nginx,php
```
该命令将只部署nginx和php,并且在运行过程中,根据需要要求输入远程主机的root密码或者sudo密码。
7. 部署完成后,可以通过浏览器访问Nginx服务器的IP地址,验证是否成功安装和配置lnmp。
希望这些对你有帮助!
阅读全文