httpd实战

1、使用ansible的playbook实现自动化安装httpd

ansible管理主控与被管理机器已实现ssh免密登录

主机清单中新增如下信息
[root@ansible ansible-playbook]# cat /etc/ansible/hosts 
[httpd]
192.168.0.222
#查看httpd组内机器连接状态
[root@ansible ~]# ansible httpd -m ping 
192.168.0.222 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
配置ansible-playbook  yml文件
[root@ansible ansible-playbook]# cat install-httpd.yml 
- hosts: httpd
  remote_user: root
  tasks:
  - name: install the latest version of Apache
    yum:
        name: httpd
        state: latest
安装httpd
[root@ansible ansible-playbook]# ansible-playbook install-httpd.yml 

执行结果:
httpd实战_第1张图片

2、建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

[root@ansible-client ~]# cat /etc/httpd/conf.d/x.conf 

 DirectoryIndex index.html
 ServerName www.X.com
 DocumentRoot "/web/vhosts/x"
 ErrorLog "/var/log/httpd/x.err"
 CustomLog  /var/log/httpd/x.access  combined
 
  Options -Indexes +FollowSymlinks
  AllowOverride All
  Require all granted
 


[root@ansible-client ~]# cat /etc/httpd/conf.d/y.conf 

 DirectoryIndex index.html
 ServerName www.Y.com
 DocumentRoot "/web/vhosts/y"
 ErrorLog "/var/log/httpd/y.err"
 CustomLog  /var/log/httpd/y.access  combined
 
  Options -Indexes +FollowSymlinks
  AllowOverride All
  Require all granted
 


[root@ansible-client ~]# mkdir -p /web/vhosts/{x,y}
[root@ansible-client ~]# echo 'www.X.com' > /web/vhosts/x/index.html
[root@ansible-client ~]# echo 'www.Y.com' > /web/vhosts/y/index.html

[root@ansible-client]# httpd -t
Syntax OK

[root@ansible-client ~]# systemctl  restart httpd

[root@ansible-client ~]#  curl www.X.com
www.X.com

[root@ansible-client ~]#  curl www.Y.com
www.Y.com

你可能感兴趣的:(linux)