ansible源码安装apache
准备两台主机
hostname | ip |
---|---|
ansible | 192.168.153.129 |
httpd | 192.168.153.132 |
添加域名解析
[root@ansible ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.153.132 httpd
配合SSH免密登陆
[root@ansible ~]# ssh-keygen -t rsa #生成密钥,指定加密方式 ,下面默认一路回车即可
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:m9UgEbkFLID4MnlRrsmn76K0ARRhb6NEScTM1etAfEM root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|*O=+=E .++ |
|+==+ +. o.. |
| =.=o o..o. |
|*.=+.. .. o |
|.=+ + S . . |
|. o . + |
| o. o |
|. +. |
|.o oo |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id root@httpd #传递公钥
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'httpd (192.168.153.130)' can't be established.
ECDSA key fingerprint is SHA256:+wH81RHiBmLpbkuk2OWGZxVRziiaNwJ9KAVjGtEM8zs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@httpd's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@httpd'"
and check to make sure that only the key(s) you wanted were added.
创建项目文件夹
[root@ansible ~]# tree /opt/
/opt/
├── ansible.cfg
├── host_vars
│ └── chenshunli.yml # 变量文件
├── inventory
├── packages #源码包
│ ├── apr-1.7.0.tar.gz
│ ├── apr-util-1.6.1.tar.gz
│ └── httpd-2.4.48.tar.gz
└── playbook #一键部署
└── httpd_site.yml
下载源码包
apr源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
httpd源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
apr_util源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@ansible ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@ansible ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
[root@ansible ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@ansible ~] mv apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz /opt/packages/
编写主机变量文件 chenshunli.yml文件
[root@ansible ~]# vim /opt/host_vars/chenshunli.yml
---
# 编译工具
tools: gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre
# 编译安装apr
apr_install: " cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install "
# 编译安装apr-util
apr_util_intall: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "
# 编译安装httpd
httpd_install: " cd /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util && make && make install "
编写源码安装apache文件httpd_site.yml文件
[root@ansible ~]# vim /opt/playbook/httpd_site.yml
---
- hosts: httpd
vars_files:
- /opt/host_vars/chenshunli.yml #指定变量文件
tasks:
- name: install server tools #安装编译工具
yum:
name: "{{ tools }}"
state: present
- name: copy package apr #将本地压缩包解压后,传到远程主机上
unarchive:
src: /opt/packages/apr-1.7.0.tar.gz
dest: /root/
copy: yes
- name: copy package apr-util
unarchive:
src: /opt/packages/apr-util-1.6.1.tar.gz
dest: /root/
copy: yes
- name: copy package httpd
unarchive:
src: /opt/packages/httpd-2.4.48.tar.gz
dest: /root/
copy: yes
- name: create group apache # 创建apache使用的用户和组
group:
name: apache
system: yes
state: present
- name: create user apache
user:
name: apache
system: yes
state: present
- name: install apr
shell: " {{ apr_install }}" #运行变量
- name: intall apr-util
shell: " {{ apr_util_intall }}"
- name: install httpd
shell: " {{ httpd_install }} "
- name: start httpd service #开启服务
shell: " /usr/local/httpd/bin/apachectl start "
执行playbook文件
[root@ansible opt]# ansible-playbook playbook/httpd_site.yml
在浏览器用HTTP主机IP访问
额外补充,手动重启httpd服务是报错
[root@localhost ~]# /usr/localtpd/bin/apachectl start #此报错虽然不影响开启服务,但是不报错最好
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
解决办法
[root@httpd ~]# vi /usr/local/httpd/conf/httpd.conf
过滤出此行取消注释更改
#ServerName www.example.com:80
更改后
ServerName localhost:80