Linux学习作业---第十七周(8.10-8.16)

一、使用ansible的playbook实现自动化安装httpd
1、环境描述
ansible:172.16.31.7 centos7.4
httpd:172.16.31.6 centos7.4
2、安装ansible

ansible服务端
yum install -y ansible

3、打通ansible服务端至客户端ssh免密登陆

ansible服务端
ssh-keygen
ssh-copy-id root@172.16.31.6

4、编写yml
配置/etc/ansible/hosts

vi  /etc/ansible/hosts
[httpd]
172.16.31.6

编写playbook

vi /data/httpd.yml
---
- hosts: httpd
  remote_user: root
  tasks:
    - name: install
      yum: name=httpd
    - name: start
      service: name=httpd state=started enabled=yes

检查配置文件内容

ansible-playbook -C /data/httpd.yml 

PLAY [httpd] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]

TASK [install] *******************************************************************************************
changed: [172.16.31.6]

TASK [start] *********************************************************************************************
changed: [172.16.31.6]

PLAY RECAP ***********************************************************************************************
172.16.31.6                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

5、运行

ansible-playbook -C /data/httpd.yml 

PLAY [httpd] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]

TASK [install] *******************************************************************************************
changed: [172.16.31.6]

TASK [start] *********************************************************************************************
changed: [172.16.31.6]

PLAY RECAP ***********************************************************************************************
172.16.31.6                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible ansible]# ansible-playbook /data/httpd.yml 

PLAY [httpd] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]

TASK [install] *******************************************************************************************
changed: [172.16.31.6]

TASK [start] *********************************************************************************************
changed: [172.16.31.6]

PLAY RECAP ***********************************************************************************************
172.16.31.6                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

6、检查httpd客户机状态

ps -ef |grep httpd
root     14905     1  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   14906 14905  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   14907 14905  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   14908 14905  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   14909 14905  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   14910 14905  0 15:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     14918  1328  0 15:22 pts/0    00:00:00 grep --color=auto httpd

二、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、创建目录/web/vhosts/x /web/vhosts/y,添加index文件

mkdir -p /web/vhosts/{x,y}
echo "ServerName www.X.com" >/web/vhosts/x/index.html
echo "ServerName www.Y.com" >/web/vhosts/y/index.html

2、创建日志目录/var/log/httpd

mkdir -p /var/log/httpd

3、开启httpd-vhosts
修改httpd主配置文件

vi /etc/httpd/conf/httpd.conf
#注释掉默认document以及默认directory设置
#DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
#    AllowOverride None
#    # Allow open access:
#    Require all granted
#</Directory>      
#<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
#    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
#    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
#    Require all granted
#</Directory>

#最后行添加引入vhost配置文件
Include conf/extra/*.conf

4、创建vhosts配置文件

vi /etc/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost www.X.com:80>                #主机名www.X.com 
    DocumentRoot "/web/vhosts/x"         #根目录
    ServerName www.X.com     
    ErrorLog "/var/log/httpd/x.err"           #错误日志
    CustomLog "/var/log/httpd/x.access" common   #访问日志
    <Directory "/web/vhosts/x">
        options FollowSymLinks                #禁止目录浏览
        AllowOverride All                           
        Require all granted                       #允许访问目录权限                  
        DirectoryIndex index.html index.htm    
    </Directory>
</VirtualHost>

<VirtualHost www.Y.com:80>
    DocumentRoot "/web/vhosts/y"
    ServerName www.Y.com
    ErrorLog "/var/log/httpd/y.err"
    CustomLog "/var/log/httpd/y.access" common
    <Directory "/web/vhosts/y">
        options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.html index.htm
    </Directory>
</VirtualHost>

5、检查配置文件

httpd -t
Syntax OK

6、修改hosts

vi /etc/hosts
172.16.31.6     httpd-qas.sh-ctmc.com httpd-qas www.X.com  www.Y.com

7、重启服务

systemctl restart httpd

8、测试结果

[root@httpd-qas vhosts]# curl http://www.X.com
ServerName www.X.com
[root@httpd-qas vhosts]# curl http://www.Y.com
ServerName www.Y.com
root@httpd-qas vhosts]# cd /var/log/httpd/
[root@httpd-qas httpd]# ll -tr
总用量 24
-rw-r--r-- 1 root root 4345 8月  14 15:47 access_log
-rw-r--r-- 1 root root  320 8月  14 15:51 x.err
-rw-r--r-- 1 root root    0 8月  14 15:57 y.err
-rw-r--r-- 1 root root 3708 8月  14 15:57 error_log
-rw-r--r-- 1 root root  358 8月  14 16:09 x.access
-rw-r--r-- 1 root root  138 8月  14 16:09 y.access
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341