First, the use ansible's playbook automate installation httpd
Host: two, one ansible master 192.168.27.7/24, a host 192.168.27.17/24
(1) master ansible installation services, using Ali cloud EPEL source, combined with CD yum source
[root@ansible ~]# cat /etc/yum.repos.d/base.repo [development] name=dvdbase repo baseurl=file:///mnt/cdrom/ enabled=1 gpgcheck=1 gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7 [aliyunEpel] name=aliyun epel baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever #安装Ansible [root@ansible ~]# yum install -y ansible
(2) configure the host inventory, configuration and connection authentication key based ssh
[root@ansible ~]# vim /etc/ansible/hosts #添加以下内容 [websrvs] 192.168.27.17 #基于Key验证 [root@ansible ~]# ssh-keygen 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:qehCr9s+C0MGZ4U17a+9FyqIsKJMAoGuRYFJRFxSK4s root@ansible The key's randomart image is: +---[RSA 2048]----+ |=B+=+. | |+.oo... | |+.+. . | |o*o . . | |Eo+ .S | |++. . .. . | |o=oo...o . . | |* o=+ o o . | |+.o==o ..o | +----[SHA256]-----+ [root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.27.17 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.27.17 (192.168.27.17)' can't be established. ECDSA key fingerprint is SHA256:TX1biesR1B8Gsz8MuCTPDZKPX3i4E051OkhQK/C9kBI. ECDSA key fingerprint is MD5:83:8c:bb:e5:b9:a7:45:99:38:fc:5d:c8:89:cf:fb:5f. Are you sure you want to continue connecting (yes/no)? 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@192.168.27.17's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added.
(3) establish playbook file
[root@ansible ~]# ll files/httpd.conf #准备httpd的配置文件 -rw-r--r-- 1 root root 11753 Feb 25 19:43 files/httpd.conf [root@ansible ~]# vim httpd.yml --- - hosts: websrvs remote_user: root tasks: - name: Install httpd yum: name=httpd state=present - name: Install configure file copy: src=files/httpd.conf dest=/etc/httpd/conf/ - name: start service service: name=httpd state=started enabled=yes
(4) using the playbook file to install httpd service, and tested
[root@ansible ~]# ansible-playbook httpd.yml PLAY [websrvs] ********************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************** ok: [192.168.27.17] TASK [Install httpd] **************************************************************************************************************************** changed: [192.168.27.17] TASK [Install configure file] ******************************************************************************************************************* ok: [192.168.27.17] TASK [start service] **************************************************************************************************************************** changed: [192.168.27.17] PLAY RECAP ************************************************************************************************************************************** 192.168.27.17 : ok=4 changed=2 unreachable=0 failed=0 Skipped = 0 Rescued = 0 ignored = 0 [root @ centos7 ~] # SS -ntl | grep 80 # 80 View host port has been opened LISTEN 0 128 ::: 80 ::: * [root @ centos7 ~] # PS -aux | grep httpd # service has also been launched root 7900 0.0 0.2 230 408 5148 Ss? 19 : 48 0 : 00 / usr / sbin / httpd - DFOREGROUND the Apache 7902 0.0 0.1 232492 3152 ? S 19:48 0:00 /usr/sbin/httpd -DFOREGROUND apache 7903 0.0 0.1 232492 3152 ? S 19:48 0:00 /usr/sbin/httpd -DFOREGROUND apache 7904 0.0 0.1 232492 3152 ? S 19:48 0:00 /usr/sbin/httpd -DFOREGROUND apache 7905 0.0 0.1 232492 3152 ? S 19:48 0:00 /usr/sbin/httpd -DFOREGROUND apache 7906 0.0 0.1 232492 3152 ? S 19:48 0:00 /usr/sbin/httpd -DFOREGROUND root 7972 0.0 0.0 112708 976 pts/0 S+ 19:51 0:00 grep --color=auto httpd
Second, the establishment httpd server, require two name-based virtual hosts
1, www.x.com, page file directory / web / vhosts / x, the error log is /var/log/httpd/x.err, access logs for the /var/log/httpd/x.access
2, www.y.com, page file directory / web / vhosts / y, the error log is /var/log/httpd/www2.err, access logs for the /var/log/httpd/y.access
3, establish their own home page as index.html file on two virtual hosts, were content to their corresponding host name
Establishing host web directory # [@ centos7 the root ~] # mkdir -p / Web / vhosts / {X, Y} # apache user authorized to access [centos7 the root @ ~] # chown -R & lt the root: apache / Web # establish each virtual host the home page file index.html [root @ centos7 ~] # echo www.x.com> / Web / vhosts / the X-/ index.html [root @ centos7 ~] # echo www.y.com> / Web / vhosts / the y- / index.html # establish a virtual host configuration [root @ centos7 ~] # vim the /etc/httpd/conf.d/ vhosts.conf <VirtualHost *: 80 > ServerName www.x.com DocumentRoot " / Web / vhosts / the X- " ErrorLog "/var/log/httpd/x.err" #错误日志 CustomLog "/var/log/httpd/x.access" combined #访问日志 <Directory "/web/vhosts/x"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName www.y.com DocumentRoot "/web/vhosts/y" ErrorLog "/var/log/httpd/www2.err" # Error log CustomLog " /var/log/httpd/y.access " Combined access log # <Directory " / Web / vhosts / the y- " > Options None AllowOverride None the Require All granted </ Directory> </ VirtualHost> # service httpd restart [root @ centos7 ~ ] # systemctl restart httpd # create a local parsing [root @ centos7 ~] # vim / etc / hosts 127.0 . 0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 :: 1 localhost6 localhost6.localdomain6 localhost.localdomain localhost 192.168. 27.17 www.x.com # add this line to 192.168 . 27.17 www.y.com add this line # # Local access test [root @ centos7 ~ ] # curl www.x.com www.x.com [root @ centos7 ~ ] # curl www.y.com www.y.com