amy_888 发表于 2018-11-18 10:42:26

8.Apache服务器

  http://www.apache.org
  1、概述
  (1)WEB服务器也称为WWW(WORLD WIDE WEB,万维网)服务器,主要功能是提供
  网上信息浏览服务。
  (2)常用web服务器
  PHP:httpd(Apache)、nginx
  tomcat:jsp+html
  win:IIS
  客户端:IE、firefox、chrome、手机(浏览器)browser
  (3)端口
  # vim /etc/services
  80:http
  443:https
  2、安装
   # yum install -y httpd-y
  或:
  httpd-2.2.15-29.el6_4.x86_64.rpm         #apache服务的主程序包
  httpd-devel-2.2.15-29.el6_4.x86_64.rpm       #apache开发程序包
  httpd-manual-2.2.15-29.el6_4.noarch.rpm    #Apache手册文档
  # yum install -yhttpd-devel
  如果在Xshell中检测apache服务,则需要安装以下软件包:
  # yum install elinks –y       #安装字符界面下浏览器的客户端
  ##测试
  # elinks192.168.18.129
  # service httpd start
  # netstat -antup |grep 80
  tcp       0      0 :::80               :::*         LISTEN   2624/httpd
  3、实战演练
  例1:搭建一台测试web服务器
  要求:部门内部搭建一台web服务器,采用的IP地址和端口为192.168.18.129:80,首页采用index.html文件。管理员E-mail地址为test@zhang.cn,网页编码类型采用UTF-8,网站资源存放在/var/www/html目录下,并将apache的配置文件根目录设置为/etc/httpd目录。
  (1)安装服务器
  # yum install -yhttpd -y
  (2)编辑配置文件
  修改配置文件相关参数:
57 ServerRoot "/etc/httpd"                   #apache配置文件的根目录
  70Timeout 60                                  #超时时间
   136Listen 80                                    #监听端口
   262ServerAdmin root@localhost          #设置管理员e-mail地址
   276ServerName 192.168.18.129:80    #服务器主机名
   292DocumentRoot "/var/www/html"#网站页面根目录
   302
   303    Options FollowSymLinks         #当一个目录没有默认首页时,允许显示此目录列表
   304    AllowOverride None
   305
   402DirectoryIndex index.html index.html.var      #指定默认首页
  # service httpd restart
  #测试
  
  
  #取消默认显示页面
  # vim/etc/httpd/conf.d/welcome.conf
  将文件内容注释掉
  # service httpd restart
  再次刷新网页,会出现以下界面:
  
  
  #创建首页
  # echo 'welcome towww.zhang.cn!' > /var/www/html/index.html
  # service httpd restart
  再次刷新页面,出现以下界面:
  
  
  例2:搭建LAMP环境
  # yum install -y httpdmysql-server php php-mysql
  # /etc/init.d/mysqld start
  #测试mysql
  # mysql
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | mysql            |
  | test               |
  +--------------------+
  mysql> exit
  #测试php
  # cat/var/www/html/index.php
  
  # /etc/init.d/httpd restart
  
  
  例3:修改根目录及对应参数,设置访问权限
  # cd /var/www/html/
  # mkdir bbs
  # cp -r /boot/grub/ bbs/
  # /etc/init.d/httpdrestart
  #测试
  # vim /etc/httpd/conf/httpd.conf
  292 DocumentRoot"/var/www/html/bbs"
  317                     #子目录会继承这个目录的属性
  331    Options Indexes FollowSymLinks            #目录浏览;可以用连接
  338    AllowOverride None
  343    Order allow,deny
  344    Allow from 192.168.18.0/24                      #从哪里来的允许
  345    #Deny from 192.168.18.0/24                  #从哪里来的拒绝
  346
  347
  #allow,deny都会读取,如果有冲突和未说明的时候按照order选项逗号后面的那个为准,谁写在后面,谁的优先级高。
  # service httpd restart

  
  当第345行加上后,刷新后出现以下界面:
  
  
  例4:别名,虚拟目录
  作用:引用网站根目录以外的网站
  # vim/etc/httpd/conf/httpd.conf
   343   Order allow,deny
   344#    Allow from 192.168.18.0/24
   345#    Deny from 192.168.18.0/24
   563
   564    Options -Indexes FollowSymLinks
   565    AllowOverride None
   566    Order deny,allow
   567    Allow from 192.168.18.129
   568    Deny from all
   569
  # mkdir/usr/local/phpmyadmin/
  # cp/var/www/html/index.html /usr/local/phpmyadmin/
   # /etc/init.d/httpdrestart
  #测试
  
  
  例5:当一个目录下没有默认首页时,访问http://192.168.18.129/phpmyadmin/禁止显示默认目录列表
  (1)不安全情况
  # cd/usr/local/phpmyadmin/
  # ls
  index.html
  # rm-fr index.html
  # touch 1.html
  # cp -r/boot/grub/ /usr/local/phpmyadmin/
  # ls
  1.html grub
  # vim/etc/httpd/conf/httpd.conf
   563
   564    Options Indexes -FollowSymLinks
   565    AllowOverride None
   566    Order deny,allow
   567    Allow from 192.168.18.129
   568    Deny from all
   569
  #/etc/init.d/httpd restart
  #测试
  
  
  (2)安全情况
   563
   564    Options -Indexes FollowSymLinks
   565    AllowOverride None
   566    Order deny,allow
   567    Allow from 192.168.18.129
   568    Deny from all
   569
  # /etc/init.d/httpd restart
  #测试
  
  
  
  
  可以发现:-Indexes,表示目录下没有默认首页时,禁止显示目录
  例6:打开软连接功能,通过软连接直接引用网站根目录以外的内容
  # mkdir /web2
  # echo test1 >/web2/a.html
  # ln -s /web2/ /var/www/html/web2
  # vim/etc/httpd/conf/httpd.conf
  打开FollowSymLininks参数
  #测试
  http://192.168.18.129/
  会发现出现web2目录
  例7:通过用户认证的方式,对网站下/usr/local/phpmyadmin/目录进行保护。/usr/local/phpmyadmin/目录,只能通过用户名和密码的方式进行访问。
  恢复为以下配置:
# vim /etc/httpd/conf/httpd.conf
   563
   564    Options Indexes FollowSymLinks
   565    AllowOverride None
   566    Order deny,allow
   567    Allow from all   
   568
  # /etc/init.d/httpd restart
  #测试
  
  
  方法一:
# vim /etc/httpd/conf/httpd.conf
  
  
  # htpasswd -cm/etc/httpd/conf/passwd.secret tom
  New password:
  Re-type new password:
  Adding password for user tom
  # htpasswd -m /etc/httpd/conf/passwd.secretbob
  New password:
  Re-type new password:
  Adding password for user bob
  # cat/etc/httpd/conf/passwd.secret
  tom:$apr1$BAHUtWCb$Ctm2kg5DdXFZV2qPAPPR51
  bob:$apr1$7VX2sDiT$lq6kyWoiu/68aAH7P4Lb0/
  # /etc/init.d/httpd restart
  #测试
  
  
  
  方法二:
  # vim/etc/httpd/conf/httpd.conf
  # vim/usr/local/phpmyadmin/.htaccess
  # cat/usr/local/phpmyadmin/.htaccess
  authtype basic
  authname "hello world..."
  authuserfile /etc/httpd/conf/passwd.secret
  require valid-user
  # htpasswd -cm/etc/httpd/passwd.secret tom
  New password:
  Re-type new password:
  Adding password for user tom
  # htpasswd -m/etc/httpd/passwd.secret pop
  New password:
  Re-type new password:
  Adding password for user pop
  # cat/etc/httpd/passwd.secret
  tom:$apr1$fQNh06Oc$53CTIqQ3PCnHY05VaBpuO.
  pop:$apr1$uRIwnTht$FRh1i3wIhgihustGMRyOI1
  # service httpd restart
  #测试

  
  例8:配置apache虚拟主机,实现在一台服务器上运行多个网站
  Apache虚拟主机实现有三种方法:
  (1)通过不同的IP地址;
  (2)通过不同的域名;
  (3)通过不同的端口号;
  三种方法都需要打开虚拟主机功能
  1000 NameVirtualHost *:80
  方法1:通过不同的IP地址,解析不同域名
  # ifconfig eth0:1192.168.18.220 netmask 255.255.255.0
  # ls /var/www/html/bbs/
  grub
  # echo 'bbs.zhang.cn' >/var/www/html/bbs/index.html
  # ls /var/www/html/
  bbs index.htmlindex.phprhel6.5 web2
  # vim/etc/httpd/conf/httpd.conf
  1020
  1021    ServerAdmin webmaster@dummy-host.example.com
  1022    DocumentRoot /var/www/html/
  1023    ServerName dummy-host.example.com
  1024    ErrorLog logs/dummy-host.example.com-error_log
  1025    CustomLog logs/dummy-host.example.com-access_log common
  1026
  1027
  1028    ServerAdmin webmaster@dummy-host.example.com
  1029    DocumentRoot /var/www/html/bbs/
  1030    ServerName dummy-host.example.com
  1031    ErrorLog logs/dummy-host.example.com-error_log
  1032    CustomLog logs/dummy-host.example.com-access_log common
  1033
  # /etc/init.d/httpd restart
  #测试
  
  
  再访问192.168.18.220,会出现测试页面
  方法2:通过不同的域名解析同一个IP地址
  # vim/etc/httpd/conf/httpd.conf
  1034
  1035    ServerAdmin webmaster@dummy-host.example.com
  1036    DocumentRoot /var/www/html/
  1037    ServerName www.example.com
  1038    ErrorLog logs/www.example.com-error_log
  1039    CustomLog logs/www.example.com-access_log common
  1040
  1041
  1042    ServerAdmin webmaster@dummy-host.example.com
  1043    DocumentRoot /var/www/html/bbs/
  1044    ServerName bbs.example.com
  1045    ErrorLog logs/bbs.example.com-error_log
  1046    CustomLog logs/bbs.example.com-access_log common
  1047
  # vim /etc/hosts
  192.168.18.129 server1.example.com
  192.168.18.129www.example.com
  192.168.18.129bbs.example.com
  # /etc/init.d/httpd restart
  #测试
  # elinks 192.168.18.129
  # elinks www.example.com
  # elinks bbs.example.com
  方法3:基于端口配置虚拟主机
  # vim/etc/httpd/conf/httpd.conf
  136 Listen 80
  137 Listen 81
  1034
  1035    ServerAdmin webmaster@dummy-host.example.com
  1036    DocumentRoot /var/www/html/
  1037    ServerName www.example.com
  1038   ErrorLog logs/www.example.com-error_log
  1039    CustomLog logs/www.example.com-access_log common
  1040
  1041
  1042    ServerAdmin webmaster@dummy-host.example.com
  1043    DocumentRoot /var/www/html/bbs/
  1044    ServerName bbs.example.com
  1045    ErrorLog logs/bbs.example.com-error_log
  1046    CustomLog logs/bbs.example.com-access_log common
  1047
  # /etc/init.d/httpd restart
  #测试
  http://192.168.18.129/
  http://192.168.18.129:81/
  




页: [1]
查看完整版本: 8.Apache服务器