论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
应用中心
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com技术社区
»
论坛
›
大数据
›
数据仓库与分析
›
使用 Ansible 通过源码编译安装 Nginx 的完备步骤。 ...
使用 Ansible 通过源码编译安装 Nginx 的完备步骤。
农民
论坛元老
|
2025-3-26 10:08:56
|
显示全部楼层
|
阅读模式
楼主
主题
1632
|
帖子
1632
|
积分
4896
服务器清单
服务器 IP操纵系统192.168.153.221Anolis OS 7.9192.168.153.222Anolis OS 7.9
步骤1 安装 Ansible
在
控制节点
(运行 Ansible 的呆板)上安装 Ansible
sudo yum install epel-release -y
sudo yum install ansible -y
复制代码
验证安装:
ansible --version
复制代码
步骤2:配置 Ansible 主机清单
编辑 Ansible 的主机清单文件 /etc/ansible/hosts,添加目标服务器
sudo vim /etc/ansible/hosts
复制代码
添加以下内容:
[webservers]
192.168.153.221
192.168.153.222
复制代码
步骤3:配置 SSH 免密登录
在
控制节点
上生成 SSH 密钥对,(一直回车)
ssh-keygen -t rsa -b 4096
复制代码
将公钥复制到目标服务器:
ssh-copy-id root@192.168.153.221
ssh-copy-id root@192.168.153.222
复制代码
步骤4:创建 Ansible Playbook
创建一个 Playbook 文件
nginx.yml
sudo vim nginx.yml
复制代码
添加以下内容:
---
- hosts: webservers
become: yes
tasks:
- name: Ensure /opt directory exists
file:
path: /opt
state: directory
- name: Install dependencies
yum:
name:
- gcc-c++
- pcre
- pcre-devel
- zlib
- zlib-devel
- openssl
- openssl-devel
state: present
- name: Download Nginx source code
get_url:
url: https://nginx.org/download/nginx-1.26.2.tar.gz
dest: /opt/nginx-1.26.2.tar.gz
- name: Extract Nginx source code
unarchive:
src: /opt/nginx-1.26.2.tar.gz
dest: /opt
remote_src: yes
- name: Configure Nginx
command: ./configure --prefix=/usr/local/nginx
args:
chdir: /opt/nginx-1.26.2
- name: Compile Nginx
command: make
args:
chdir: /opt/nginx-1.26.2
- name: Install Nginx
command: make install
args:
chdir: /opt/nginx-1.26.2
- name: Start Nginx
command: /usr/local/nginx/sbin/nginx
- name: Ensure firewall allows HTTP and HTTPS
firewalld:
service: "{{ item }}"
permanent: yes
state: enabled
loop:
- http
- https
notify:
- Reload firewall
handlers:
- name: Reload firewall
command: firewall-cmd --reload
复制代码
步骤5:运行 Playbook
使用以下下令运行 Playbook:
ansible-playbook nginx.yml
复制代码
步骤6:验证安装
在浏览器中访问以下地址,验证 Nginx 是否正常运行:
http://192.168.153.221
http://192.168.153.222
如果看到 Nginx 的默认接待页面,说明安装乐成。
步骤7:把 Nginx 服务添加到systemd管理
创建一个 Playbook 文件
nginx-systemd.yml
sudo vim nginx-systemd.yml
复制代码
添加以下内容:
---
- hosts: webservers # 指定目标主机组
become: yes # 使用 root 权限执行任务
tasks: # 定义任务列表
- name: Create Nginx systemd service file
copy:
dest: /etc/systemd/system/nginx.service
content: |
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- name: Reload systemd daemon
command: systemctl daemon-reload
- name: Enable and start Nginx service
service:
name: nginx
state: started
enabled: yes
复制代码
运行 Playbook:
ansible-playbook nginx-systemd.yml
复制代码
验证:
systemctl status nginx
复制代码
systemd管理下令
# 重新加载 systemd 管理器配置,以便使新的服务文件或配置变更生效
sudo systemctl daemon-reload
# 设置 Nginx 服务在系统启动时自动启动
sudo systemctl enable nginx
# 启动 Nginx 服务
sudo systemctl start nginx
# 查看 Nginx 服务的当前状态(是否运行中)
sudo systemctl status nginx
#重启Nginx 服务
sudo systemctl restart nginx
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
使用道具
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
农民
论坛元老
这个人很懒什么都没写!
楼主热帖
数据库入门
肝了五万字把SQL数据库从基础到高级所 ...
java反射大白话
iOS WebRTC 点对点实时音视频流程介绍 ...
Java中set集合简介说明
【R语言数据科学】(十二):有趣的概 ...
每日算法之数组中的逆序对
消息队列常见的使用场景
flume基本安装与使用
CentOS 7.9 安装 rocketmq-4.9.2
标签云
国产数据库
集成商
AI
运维
CIO
存储
服务器
浏览过的版块
分布式数据库
快速回复
返回顶部
返回列表