httpd批量安装与基于域名建立虚拟主机

Table of Contents

  1. ansible 安装 httpd
  2. 建立 httpd 服务器,要求提供两个基于名称的虚拟主机:

ansible 安装 httpd

  1. 注意:提前进行完公钥复制

  2. 安装 ansible 并进行配置

    1. 安装 yum install -q -y ansible

    2. 配置 vim /etc/ansible/hosts

      [sg1]
      192.168.10.7
      192.168.10.17
      [sg2]
      192.168.10.27
      
  3. 在/root 下建立一个 httpd 文件夹
    mkdir /root/httpd

  4. 进入文件夹并在文件中创建一个主 yaml 文件
    vim httpd_role.yml

    ---
    - hosts: all
      roles:
        - role: httpd
    
  5. 创建 role 文件夹及子文件夹

    • roles
      • httpd
        • templates
          用于放模板文件
          • httpd.conf.j2
            将 http 的配置文件/etc/httpd/conf/httpd.conf 拷贝为 httpd.conf.j2

            Listen {{port}}
            
        • handlers
          用于放触发性的操作
          • main.yml

            - name: restart service
              service: name=httpd state=restarted
            
        • tasks
          主要的操作
          • main.yml
            引入各小操作

            ---
            - include: install.yml
            - include: config.yml
            - include: service.yml
            
          • install.yml
            通过 yum 进行安装

            - name: install
              yum:
                name: httpd
                state: present
            
          • config.yml
            将模板文件变为配置文件并拷贝到主机

            - name: config
              template:
                src: httpd.conf.j2
                dest: /etc/httpd/conf/httpd.conf
              notify: restart service
            
          • service.yml
            开启 httpd 服务

            - name: service
              service:
                name: httpd
                state: started
                enabled: yes
            
        • var
          存放变量号及其它可能变更的配置
          • main.yml

            port: 8888
            
  6. 使用命令
    ansible-playbook /root/httpd_role.yml

建立 httpd 服务器,要求提供两个基于名称的虚拟主机:

  1. www.X.com,页面文件目录为/web/vhosts/x;错误日志为
    /var/log/httpd/x.err,访问日志为/var/log/httpd/x.access

    <VirtualHost *:80>
        ServerName www.X.com
        DocumentRoot /web/vhosts/x
        <Directory "/web/vhosts/x">
            Require all granted
        </Directory>
        CustomLog "/var/log/httpd/x.access"
        ErrorLog "/var/log/httpd/x.err"
    </VirtualHost>
    
  2. www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err
    ,访问日志为/var/log/httpd/y.access4. 重启 httpd
    systemctl restart httpd

    <VirtualHost *:80>
        ServerName www.Y.com
        DocumentRoot /web/vhosts/y
        <Directory "/web/vhosts/y">
            Require all granted
        </Directory>
        CustomLog "/var/log/httpd/y.access"
        ErrorLog "/var/log/httpd/y.err"
    </VirtualHost>
    
  3. 为两个虚拟主机建立各自的主页文件 index.html,内容分别为其对应的主机名

    mkdir -p /web/vhosts/{x,y}
    echo '<h1>www.x.com</h1>' > /web/vhosts/x/index.html
    echo '<h1>www.y.com</h1>' > /web/vhosts/y/index.html
    
  4. 重启 httpd
    systemctl restart httpd

posted @ 2020-01-18 09:24  stars_wisper  阅读(126)  评论(0编辑  收藏  举报