ansible-playbook编译安装nginx怎么执行访问nginx
时间: 2024-10-09 19:12:03 浏览: 46
使用Ansible playbook编译并安装Nginx通常涉及以下几个步骤:
1. **创建playbook文件**:首先,你需要在`ansible-playbooks`目录下创建一个YAML格式的playbook文件,比如叫做`install_nginx.yml`。在这个文件里,你可以定义任务序列,如下载、解压、配置以及启动服务。
```yaml
---
- name: Install Nginx
hosts: servers
become: true
vars:
nginx_version: "1.20.1"
tasks:
- name: Download Nginx package
unarchive:
src: "https://nginx.org/download/nginx/{{ nginx_version }}.tar.gz"
dest: /usr/src/nginx
- name: Configure Nginx
copy:
src: "conf/nginx.conf" # 指定你的nginx配置文件位置
dest: /etc/nginx/nginx.conf
mode: '0644'
- name: Make and install Nginx
command: "cd /usr/src/nginx && ./configure --prefix=/etc/nginx && make && make install"
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yes
- name: Check Nginx status
command: "curl http://localhost"
```
2. **运行playbook**:在命令行中,导航到包含playbook的目录,然后使用`ansible-playbook`命令执行它:
```
ansible-playbook install_nginx.yml
```
3. **访问Nginx**:如果一切顺利,playbook结束后,你应该能在浏览器中输入服务器IP地址(或域名)加上默认端口80,例如 `http://your_server_ip` 来访问新安装的Nginx服务。如果你在`command`任务中设置了其他端口,需要相应地替换。
阅读全文