Linux基础及总结16之ansible和httpd

2020-02-29  本文已影响0人  牵挂包含一种欣赏

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

    1)创建存放playbook文件目录

        mkdir -p /etc/ansible/playbook

    2)生成httpd安装的playbook文件

        vim /etc/ansible/playbook/httpd.yml

        ---

        - hosts: 192.168.2.240

          remote_user: root

          tasks:

            - name: install httpd

              yum: name=httpd

            - name: config

              copy: src=/etc/ansible/playbook/httpd.conf dest=/etc/httpd/conf/

            - name: service

              service: name=httpd state=started enabled=yes

    3)复制对应版本的httpd.conf文件到/etc/ansible/playbook目录下修改httpd监听端口80为88

    4)执行playbook安装httpd

        ansible-playbook  /etc/ansible/playbook/httpd.yml

    5)登录目标机器验证安装情况

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,内容分别为其对应的主机名

    1)创建网站文件存放目录,创建两个虚拟主机的页面文件

        mkdir -p /web/vhosts/{web1,web2}

        echo "www.web1.com" > /web/vhosts/web1/index.html

         echo "www.web2.com" > /web/vhosts/web1/index.html

    2)定义虚拟主机配置文件

        vim /etc/httpd/conf.d/website.conf

        <VirtualHost *:80>

        DocumentRoot /web/vhosts/web1

        servername www.web1.com

        CustomLog "/var/log/httpd/web1_access_log.log"  combined

        CustomLog "/var/log/httpd/web1_error_log.log"  combined

        <Directory "/web/vhosts/web1">

        Require all  granted

        </Directory>

        </VirtualHost>

        <VirtualHost *:80>

        DocumentRoot /web/vhosts/web2

        servername www.web2.com

        CustomLog "/var/log/httpd/web2_access_log.log"  combined

        CustomLog "/var/log/httpd/web2_error_log.log"  combined

        <Directory "/web/vhosts/web2">

        Require all  granted

        </Directory>

        </VirtualHost>

    3)重启httpd服务,查看下日志

        systemctl restarthttpd

    4)在客户端机器上添加httpd server的IP地址与域名解析,并且验证

        vim /etc/hosts

         192.168.2.241 www.web1.com www.web2.com

        

    

上一篇下一篇

猜你喜欢

热点阅读