用apache搭建web服务器

 一。基于域名的虚拟主机

1,

  1. [root@localhost ~]# uname -r 
  2. 2.6.32-71.el6.i686 
  1. [root@localhost ~]# cat /etc/redhat-release 
  2. Red Hat Enterprise Linux Server release 6.0 (Santiago) 

2,安装httpd

  1. yum install httpd -y 
3,编辑配置文件
  1. vim /etc/httpd/conf/httpd.conf 
  1. NameVirtualHost *:80将这一行的#去掉 
  1. <VirtualHost *:80> 复制这一行下面的7行 粘贴至最后(Y7Y P) 
  2. 修改成为 
  3. <VirtualHost *:80> 
  4.     ServerAdmin  [email protected] 管理员的邮箱 
  5.     DocumentRoot /var/www/html 发布目录 
  6.     ServerName www.test1.com 一个虚拟主机的域名 
  7.     (其余均为日志,可以不要) 
  8. </VirtualHost> 
  9. <VirtualHost *:80> 
  10.     ServerAdmin  [email protected] 
  11.     DocumentRoot /www/virtual 另一个虚拟主机的发布目录 
  12.     ServerName www.test2.com 另一个虚拟主机的域名 
  13. </VirtualHost> 
  14.  
  15. [root@localhost virtual]# mkdir /www/virtual -p 
  16. [root@localhost virtual]# vi /www/virtual/index.html 
  17. www.test2.com 
  1. [root@localhost virtual]# vi /var/www/html/index.html 
  2. www.test1.com 
  1. [root@localhost virtual]# ll -Z /var/www/html/ 查看这个目录的安全上下文
  2. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 \ 
  3. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html 
  1. [root@localhost virtual]# chcon index.html -R -t httpd_sys_content_t /www  修改安全上下文
  1. [root@localhost virtual]# /etc/init.d/httpd restart 重启服务 

WINDOW下

C:\Windows\System32\drivers\etc\hosts用记事本打开,添加

192.168.1.12 www.test1.com

192.168.1.12 www.test2.com

LINUX下 vi /etc/hosts

 

192.168.1.12 www.test1.com

192.168.1.12 www.test2.com

测试:在浏览器重输入www.test1.com会显示虚拟机1的发布目录里的东西

      在浏览器重输入www.test2.com会显示虚拟机2的发布目录里的东西

 

二,用户认证

在/www/virtual目录下建secret目录,在secret目录下建index.html

 

  1. [root@localhost ~]# vi /www/virtual/index.html 
  2. This  is a secret! 

 

在发布目录为、www/virtal的块内的后面加上

 

  1. <Directory /www/virtual/secret> 
  2.    authuserfile   /etc/httpd/conf/passwd 
  3.    authname  "secret" 
  4.    authtype    basic 
  5.    require   valid-user 
  6.    require  user 123 
  7.  </Directory> 

这样这个块就变成了

  1. <VirtualHost *:80> 
  2.     ServerAdmin  [email protected] 
  3.     DocumentRoot /www/virtual 
  4.     ServerName www.test2.com 
  5.    <Directory /www/virtual/secret> 
  6.     authuserfile   /etc/httpd/conf/passwd 
  7.     authname  "secret" 
  8.     authtype    basic 
  9.     require   valid-user 
  10.     require  user 123 用户名
  11.   </Directory> 
  12. </VirtualHost> 
  1. [root@localhost ~]# htpasswd -cm /etc/httpd/conf/passwd 123 
  2. 给用户设置密码 

 

  1. [root@localhost ~]# /etc/init.d/httpd restart 

在浏览器输入http://www.test2.com/secret/

输入用户,密码 访问,若没有权限,修改安全上下文

三。基于端口号的虚拟主机

因为在/etc/httpd/conf/httpd.conf 里包含 Include conf.d/*.conf,故在 /etc/httpd/conf.d目录下

vim port.conf

 

  1. [root@vm1 www]# vim /etc/httpd/conf.d/port.conf 
  2. Listen 81 开启端口81
  3. Listen 82  开启82
  4. <VirtualHost 192.168.122.11:81> 
  5. ServerName 81.qq.com   可以不要
  6. ServerAdmin  [email protected]  省略日志文件
  7. DocumentRoot /www/virtual 
  8. </VirtualHost> 
  9. <VirtualHost 192.168.122.11:82 > 
  10. ServerName 81.qq.com 
  11. ServerAdmin  [email protected] 
  12. DocumentRoot /var/www/html 
  13. </VirtualHost> 
  1. [root@vm1 ~]# /etc/init.d/httpd restart 
  2. Stopping httpd:                                            [FAILED] 
  3. Starting httpd: Warning: DocumentRoot [/www/virtual] does not exist 
  4. (13)Permission denied: make_sock: could not bind to address [::]:81 
  5. (13)Permission denied: make_sock: could not bind to address 0.0.0.0:81 
  6. no listening sockets available, shutting down 
  7. Unable to open logs 
  8.                                                            [FAILED] 
    1. [root@vm1 www]# ll -Z 
    2. drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin 
    3. drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error 
    4. drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html 
    5. drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons 
    6. [root@vm1 www]# chcon -R -t httpd_sys_content_t /www 
    7. [root@vm1 www]# /etc/init.d/httpd restart 
    8. Stopping httpd:                                            [FAILED] 
    9. Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:81 
    10. (13)Permission denied: make_sock: could not bind to address 0.0.0.0:81 
    11. no listening sockets available, shutting down 
    12. Unable to open logs 
    13.                                                            [FAILED] 
    14. [root@vm1 www]# semanage port -a -t http_port_t  -p tcp 81 
    15. -bash: semanage: command not found 找不到这个命令 
    16. [root@vm1 www]# yum install policycoreutils-python -y 安装命令 
    17. [root@vm1 www]# semanage port -l|grep http 
    18. http_cache_port_t              tcp      3128, 8080, 8118, 10001-10010 
    19. http_cache_port_t              udp      3130 
    20. http_port_t                    tcp      80, 443, 488, 8008, 8009, 8443 
    21. pegasus_http_port_t            tcp      5988 
    22. pegasus_https_port_t           tcp      5989 
    23. [root@vm1 www]# semanage port -a -t http_port_t  -p tcp 81 
    24. [root@vm1 www]# semanage port -a -t http_port_t  -p tcp 82 
    25. [root@vm1 www]# /etc/init.d/httpd restart 
    26. Stopping httpd:                                            [FAILED] 
    27. Starting httpd:                                            [  OK  ] 好了 
    28. 在两个主机的目录里写上东西,浏览器访问 

四。基于IP地址的虚拟主机

 

  1. [root@vm1 www]# ifconfig eth0:1 192.168.122.111 netmask 255.255.255.0 
  2. [root@vm1 www]# ifconfig eth0:1 up  临时添加子接口IP 
  3. [root@vm1 www]# vim /etc/httpd/conf/httpd.conf  
  4. <VirtualHost 192.168.122.11:80> 
  5.     ServerAdmin [email protected] 
  6.     DocumentRoot /www/virtual 
  7.     ServerName 11.example.com 
  8.     ErrorLog logs/dummy-host.example.com-error_log 
  9.     CustomLog logs/dummy-host.example.com-access_log common 
  10. </VirtualHost> 
  11. <VirtualHost 192.168.122.111:80> 
  12.     ServerAdmin [email protected] 
  13.     DocumentRoot /var/www/html 
  14.     ServerName 111.example.com 
  15.     ErrorLog logs/dummy-host.example.com-error_log 
  16.     CustomLog logs/dummy-host.example.com-access_log common 
  17. </VirtualHost> 
  18. 重启HTTP OK 

cp ifconfig-eth0  ifconfig-eth0:1

vi ifconfig-eth0:1

DEVICE=eth0:1

IPADDR=192.168.122.111

重启网卡即可  这是永久添加网卡

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章