用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

重啓網卡即可  這是永久添加網卡

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