httpd批量安装与基于域名建立虚拟主机
Posted chaoyiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpd批量安装与基于域名建立虚拟主机相关的知识,希望对你有一定的参考价值。
Table of Contents
ansible 安装 httpd
- 注意:提前进行完公钥复制
- 安装 ansible 并进行配置
- 安装
yum install -q -y ansible
配置
vim /etc/ansible/hosts
[sg1] 192.168.10.7 192.168.10.17 [sg2] 192.168.10.27
- 安装
- 在/root 下建立一个 httpd 文件夹
mkdir /root/httpd
进入文件夹并在文件中创建一个主 yaml 文件
vim httpd_role.yml
--- - hosts: all roles: - role: httpd
- 创建 role 文件夹及子文件夹
- roles
- httpd
- templates
用于放模板文件httpd.conf.j2
将 http 的配置文件/etc/httpd/conf/httpd.conf 拷贝为 httpd.conf.j2Listen {{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
- templates
- httpd
- roles
使用命令
ansible-playbook /root/httpd_role.yml
建立 httpd 服务器,要求提供两个基于名称的虚拟主机:
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>
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>
为两个虚拟主机建立各自的主页文件 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
重启 httpd
systemctl restart httpd
以上是关于httpd批量安装与基于域名建立虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章