apache服务

实例一:

要求:

  • 从http://ldap.example.com/pub/example.html下载文件,并重命名为index.html,不要修改文件内容
  • 将文件index.html拷贝到你的DocumentRoot目录下
  • 来自于example.com的客户端可以访问该web服务器
  • 来自于my133t.org的客户端的访问会被拒绝

解答:

//安装服务

[root@server30 ~]# yum -y install httpd
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
xx                                                       | 4.1 kB     00:00     
Resolving Dependencies
略...
Installed:
  httpd.x86_64 0:2.4.6-17.el7                                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                 apr-util.x86_64 0:1.5.2-6.el7       
  httpd-tools.x86_64 0:2.4.6-17.el7        mailcap.noarch 0:2.1.41-2.el7       

Complete!

//进入目录,下载文件并重命名

[root@server30 ~]# cd /var/www/html/
[root@server30 html]# ls
[root@server30 html]# wget http://ldap.example.com/pub/example.html
--2019-01-17 09:01:30--  http://ldap.example.com/pub/example.html
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21 [text/html]
Saving to: ‘example.html’

100%[======================================>] 21          --.-K/s   in 0s      

2019-01-17 09:01:30 (4.26 MB/s) - ‘example.html’ saved [21/21]

[root@server30 html]# mv example.html index.html
[root@server30 html]# ls
index.html

//启动服务并设置开机自启,关闭iptables和ebtables

[root@server30 html]# systemctl start httpd
[root@server30 html]# systemctl enable httpd.service 
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@server30 html]# systemctl mask iptables.service ebtables.service 
ln -s '/dev/null' '/etc/systemd/system/iptables.service'
ln -s '/dev/null' '/etc/systemd/system/ebtables.service'
[root@server30 html]# systemctl stop iptables ebtables
[root@server30 html]# 

//过滤http服务,并设置防火墙

[root@server30 ~]# firewall-cmd --get-services |grep http
amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
[root@server30 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 service name=http accept' --permanent
success
[root@server30 ~]# firewall-cmd --reload
success

//访问web服务器
在这里插入图片描述

实例二

要求

  • 已签名证书从http://ldap.example.com/pub/server30.crt获取
  • 证书的秘钥从http://ldap.example.com/pub/server30.key获取
  • 证书的签名授权信息从http://ldap.example.com/pub/group30.crt获取

解答

//安装ssl软件包

[root@server30 ~]# yum -y install mod_ssl
过程略

//下载证书,秘钥,证书授权信息

[root@server30 conf.d]# cd /etc/pki/tls
[root@server30 tls]# ls
cert.pem  certs  misc  openssl.cnf  private
[root@server30 tls]# cd certs/
[root@server30 certs]# ls
ca-bundle.crt        localhost.crt    Makefile
ca-bundle.trust.crt  make-dummy-cert  renew-dummy-cert
[root@server30 certs]# wget http://ldap.example.com/pub/server30.crt
--2019-01-17 09:33:49--  http://ldap.example.com/pub/server30.crt
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3147 (3.1K)
Saving to: ‘server30.crt’

100%[======================================>] 3,147       --.-K/s   in 0s      

2019-01-17 09:33:49 (417 MB/s) - ‘server30.crt’ saved [3147/3147]

[root@server30 certs]# wget http://ldap.example.com/pub/group30.crt
--2019-01-17 09:34:02--  http://ldap.example.com/pub/group30.crt
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3256 (3.2K)
Saving to: ‘group30.crt’

100%[======================================>] 3,256       --.-K/s   in 0s      

2019-01-17 09:34:02 (537 MB/s) - ‘group30.crt’ saved [3256/3256]

[root@server30 tls]# cd private/
[root@server30 private]# ls
localhost.key
[root@server30 private]# wget http://ldap.example.com/pub/server30.key
--2019-01-17 09:34:26--  http://ldap.example.com/pub/server30.key
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 887
Saving to: ‘server30.key’

100%[======================================>] 887         --.-K/s   in 0s      

2019-01-17 09:34:26 (192 MB/s) - ‘server30.key’ saved [887/887]

//进入目录,修改配置文件并重启服务

[root@server30 ~]# cd /etc/httpd/conf.d/
[root@server30 conf.d]# vim ssl.conf
#General setup for the virtual host, inherited from global configuration
#DocumentRoot "/var/www/html"
#ServerName server30.example.com:443

SSLCertificateFile /etc/pki/tls/certs/server30.crt

SSLCertificateKeyFile /etc/pki/tls/private/server30.key

SSLCACertificateFile /etc/pki/tls/certs/group30.crt
[root@server30 conf.d]# systemctl restart httpd
[root@server30 conf.d]#

//设置防火墙

[root@server30 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 service name=https accept' --permanent
success
[root@server30 ~]# firewall-cmd --reload
success

//访问web服务
在这里插入图片描述

实例三

要求

  • 为站点http://www.example.com创建一个虚拟主机
  • 设置DocumentRoot为/var/www/virtual
  • 从http://ldap.example.com/pub/www.html下载文件,并重命名为index.html,不要修改文件内容
  • 将文件index.html拷贝到DocumentRoot目录下
  • 确保floyd用户能够在/var/www/virtual下创建文件

解答

//创建virtual目录,下载www.html到此目录,把属组给到/var/www/下

[root@server30 ~]# mkdir /var/www/virtual
[root@server30 ~]# ls /var/www
cgi-bin  html  virtual
[root@server30 ~]# cd /var/www
[root@server30 www]# ls
cgi-bin  html  virtual
[root@server30 www]# wget -O virtual/index.html http://ldap.example.com/pub/www.html
--2019-01-17 10:09:48--  http://ldap.example.com/pub/www.html
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16 [text/html]
Saving to: ‘virtual/index.html’

100%[======================================>] 16          --.-K/s   in 0s      

2019-01-17 10:09:48 (2.93 MB/s) - ‘virtual/index.html’ saved [16/16]

[root@server30 www]# chown -R apache.apache /var/www/
[root@server30 www]# 

//创建floyd用户,给权限

[root@server30 www]# useradd floyd
[root@server30 www]# setfacl -m u:floyd:rwx virtual/
[root@server30 www]#

//在根目录下面搜索vhost,复制到conf.d下面

[root@server30 ~]# find / -name *vhost*
/dev/vhost-net
/etc/selinux/targeted/modules/active/modules/vhostmd.pp
/usr/lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/vhost
/usr/lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/vhost/vhost_net.ko
/usr/lib64/httpd/modules/mod_vhost_alias.so
/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
[root@server30 ~]# cd /etc/httpd/
[root@server30 httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run
[root@server30 httpd]# cd conf.d
[root@server30 conf.d]# ls
autoindex.conf  README  ssl.conf  userdir.conf  welcome.conf
[root@server30 conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .
[root@server30 conf.d]# 

//编辑此文件,并重启服务

[root@server30 conf.d]# vim httpd-vhosts.conf
#VirtualHost example:
#Almost any Apache directive may go into a VirtualHost container.
#The first VirtualHost section is used for all requests that do not
#match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    DocumentRoot "/var/www/html
    ServerName server30.example.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/virtual"
    ServerName www.example.com
</VirtualHost>
[root@server30 conf.d]# systemctl restart httpd
[root@server30 conf.d]#

//到客户端ping www.example.com

[root@desktop30 ~]# ping www.example.com
PING www.example.com (172.16.30.130) 56(84) bytes of data.
64 bytes from alt.example.com (172.16.30.130): icmp_seq=1 ttl=64 time=1.27 ms
64 bytes from alt.example.com (172.16.30.130): icmp_seq=2 ttl=64 time=0.377 ms
64 bytes from server30.example.com (172.16.30.130): icmp_seq=3 ttl=64 time=0.658 ms
64 bytes from server30.example.com (172.16.30.130): icmp_seq=4 ttl=64 time=0.319 ms
64 bytes from www.example.com (172.16.30.130): icmp_seq=5 ttl=64 time=0.600 ms
^C
--- www.example.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 0.319/0.645/1.272/0.339 ms

//客户端访问web服务
在这里插入图片描述
//回到服务端切换到用户floyd查看能否创建文件

[root@server30 ~]# su - floyd 
[floyd@server30 ~]$ cd /var/www/virtual/
[floyd@server30 virtual]$ ls
index.html
[floyd@server30 virtual]$ touch aa
[floyd@server30 virtual]$ ls
aa  index.html
[floyd@server30 virtual]$ ll
total 4
-rw-rw-r--. 1 floyd  floyd   0 Jan 17 10:31 aa
-rw-r--r--. 1 apache apache 16 Nov 28  2014 index.html
[floyd@server30 virtual]$ 

实例四

要求

  • 在你server上的web服务器的DocumentRoot目录下创建一个名为private的目录
  • 从http://ldap.example.com/pub/private.html下载文件到这个目录,并重命名为index.html,不要修改文件内容
  • 从server上,任何人都可以浏览private的内容,但是从其他系统不能访问这个目录的内容

解答

//进入html目录下创建private目录,下载文件到刚创建的目录下

[root@server30 ~]# cd /var/www/html/
[root@server30 html]# mkdir private
[root@server30 html]# wget -O private/index.html http://ldap.example.com/pub/private.html
--2019-01-17 10:52:02--  http://ldap.example.com/pub/private.html
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8 [text/html]
Saving to: ‘private/index.html’

100%[======================================>] 8           --.-K/s   in 0s      

2019-01-17 10:52:03 (1.62 MB/s) - ‘private/index.html’ saved [8/8]

[root@server30 html]#

//修改配置文件,重启服务

 match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    DocumentRoot "/var/www/html
    ServerName server30.example.com
    <Directory "/var/www/html/private">
        Require ip 172.16.30.130
    </Directory>
</VirtualHost>
[root@server30 html]# systemctl restart httpd
[root@server30 html]#

//到客户端浏览server30.example.com/private,然后回到服务端重复操作
在这里插入图片描述
在这里插入图片描述

实例五

要求

  • 动态内容由名为alt.example.com的虚拟主机提供
  • 虚拟主机侦听端口为8909
  • 从http://ldap.example.com/pub/webapp.wsgi下载一个脚本,然后放在适当的位置,不要修改文件内容
  • 客户端访问http://alt.example.com:8909时,应该搜到动态生成的web页面
  • 此http://alt.example.com:8909必须能被example.com内所有的系统访问

解答

//创建目录,下载webapp.wsgi到此目录,并修改此属组

[root@server30 ~]# cd /var/www
[root@server30 www]# mkdir wsgi
[root@server30 www]# ls
cgi-bin  html  virtual  wsgi
[root@server30 www]# wget -O wsgi/webapp.wsgi http://ldap.example.com/pub/webapp.wsgi
--2019-01-17 11:31:09--  http://ldap.example.com/pub/webapp.wsgi
Resolving ldap.example.com (ldap.example.com)... 172.16.30.254
Connecting to ldap.example.com (ldap.example.com)|172.16.30.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 277
Saving to: ‘wsgi/webapp.wsgi’

100%[======================================>] 277         --.-K/s   in 0s      

2019-01-17 11:31:09 (46.0 MB/s) - ‘wsgi/webapp.wsgi’ saved [277/277]

[root@server30 www]# chown -R apache.apache wsgi/
[root@server30 www]

//修改配置文件

[root@server30 www]# vim /etc/httpd/conf.d/httpd-vhosts.conf
Listen 8909
<VirtualHost *:8909>
    WSGIScriptAlias / "/var/www/wsgi/webapp.wsgi"
    ServerName alt.example.com
</VirtualHost>

//安装msgi软件包

[root@server30 www]# yum -y install mod_wsgi
过程略

//查看端口,发现没有刚刚添加的端口,然后添加刚刚配置的端口

[root@server30 www]# semanage port -l |grep http
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@server30 www]# semanage port -a -t http_port_t -p tcp 8909
[root@server30 www]# semanage port -l |grep http
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      8909, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989

//重启httpd服务

[root@server30 www]# systemctl start httpd
[root@server30 www]# 

//防火墙放行8909端口,重启防火墙

[root@server30 www]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 port protocol=tcp port=8909 accept' --permanent
success
[root@server30 www]# firewall-cmd --reload
success
[root@server30 www]# 

//到客户端ping alt.example.com

[root@desktop30 ~]# ping alt.example.com
PING alt.example.com (172.16.30.130) 56(84) bytes of data.
64 bytes from www.example.com (172.16.30.130): icmp_seq=1 ttl=64 time=0.300 ms
64 bytes from alt.example.com (172.16.30.130): icmp_seq=2 ttl=64 time=0.556 ms
64 bytes from www.example.com (172.16.30.130): icmp_seq=3 ttl=64 time=0.560 ms
^C
--- alt.example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.300/0.472/0.560/0.121 ms

//到客户端浏览器访问alt,出现Hello World,就代表成功
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值