Web服务器——Apache

来源:互联网 发布:西门子840d基础编程 编辑:程序博客网 时间:2024/05/11 04:30

一 Web服务器相关知识

Web服务器是Linux最常用的应用。Web服务器是一种BS结构,亦即浏览器/服务器模式,具有通用性,是CS(客户端/服务器)结构的一种抽象。Web服务器通常采用HTTP协议,亦即超文本传输协议。动态语言的发展从CGI到以后的JSP、PHP、ASP.NET,后面几种技术现在得到了广泛地应用。Apache服务器是由Apache基金会开发、运行和维护的一种Web服务器,本身支持PHP,对应的访问名是httpd。为了支持JSP,在apache的基础上进行改善,出现了tomcat,也就是Apache的加强版。想必读者非常清楚,Java体系被分为J2SE(Java Standard Edition)、J2EE(Java Enterprise Edition,包括JSP、Servlet、EJB、JNDI、Struts、Hibernate、Spring等技术)、J2ME(Java Micro Edition)。其他公司也出现了可以支持Java技术的Web服务器,比如Jboss,现属于RedHat(三大产品线:Linux系统、虚拟机、JBoss),BEA公司的WebLogic,IBM公司的Websphere等等。当然Windows下支持ASP.NET的Web服务器有微软的IIS。

 

二 什么是Apache

Apache,当今市场上主流的Web服务器。Apache基本配置文件的httpd.conf。apache可以和php集成。


三 apache安装及参数讲解

[sql] view plaincopy
  1.  #安装  
  2. [root@serv01 ~]# yum install httpd -y  
  3. [root@serv01 ~]# rpm -qa|grep httpd  
  4. httpd-tools-2.2.15-9.el6.x86_64  
  5. httpd-2.2.15-9.el6.x86_64  
  6.    
  7.  [root@serv01 ~]# rpm -ql httpd|grep conf  
  8. /etc/httpd/conf  
  9. /etc/httpd/conf.d  
  10. /etc/httpd/conf.d/README  
  11. /etc/httpd/conf.d/welcome.conf  
  12. /etc/httpd/conf/httpd.conf  
  13. /etc/httpd/conf/magic  
  14. /etc/sysconfig/httpd  
  15. /usr/lib64/httpd/modules/mod_log_config.so  
  16.    
  17.  [root@serv01 ~]# cd /etc/httpd/  
  18. [root@serv01 httpd]# ll  
  19. total 8  
  20. drwxr-xr-x. 2 root root 4096 Aug 14 22:18conf  
  21. drwxr-xr-x. 2 root root 4096 Aug 14 22:18 conf.d  
  22. lrwxrwxrwx. 1 root root   19 Aug 14 22:18 logs ->../../var/log/httpd  
  23. lrwxrwxrwx. 1 root root   29 Aug 14 22:18 modules ->../../usr/lib64/httpd/modules  
  24. lrwxrwxrwx. 1 root root   19 Aug 14 22:18 run ->../../var/run/httpd  
  25. [root@serv01 httpd]# ls conf.d/  
  26. README welcome.conf  
  27. [root@serv01 httpd]# ls logs/  
  28. access_log error_log  
  29.    
  30. URL:http://httpd.apache.org/docs/2.2/  
  31.    
  32. /etc/httpd/conf/httpd.conf  
  33.    
  34. #参数ServerTokens  
  35.    
  36. 浏览器访问:http://192.168.1.11/aaa.html  
  37.    
  38. ServerTokens OS  
  39. Not Found  
  40. The requested URL /aaa.html wasnot found on this server.  
  41. Apache/2.2.15 (Red Hat)Server at 192.168.1.11 Port 80  
  42.    
  43. ServerTokens major  
  44. Not Found  
  45. The requested URL /aaa.html wasnot found on this server.  
  46. Apache/2 Server at192.168.1.11 Port 80  
  47.    
  48. ServerTokens full  
  49.    
  50. Not Found  
  51. The requested URL /aaa.html wasnot found on this server.  
  52. Apache/2.2.15 (Red Hat)DAV/2 Server at 192.168.1.11 Port 80  
  53.    
  54.    
  55. #根目录  
  56. ServerRoot "/etc/httpd"  
  57.    
  58. #PID  
  59. PidFile run/httpd.pid  
  60.    
  61. #连接超时  
  62. Timeout 60  
  63.    
  64. #不支持有状态连接  
  65. KeepAlive Off  
  66.    
  67. #KeepAlive ON时生效,最大请求连接的时间  
  68. MaxKeepAliveRequests 100  
  69.    
  70. #两次请求之间的延时  
  71. KeepAliveTimeout 15  
  72.    
  73. #MPM:多路处理模块  
  74. #prefork worker  
  75.    
  76. #处理请求两种方式:进程 线程  
  77. #进程:prefork 每个用户启动一个进程,一个进程处理一个客户端 安全好  
  78. #worker:线程处理每个连接,一个进程中有多个线程 性能好  
  79. #RPM包不支持worker,源码编译  
  80. # prefork MPM  
  81. # StartServers: number of server processes tostart  
  82. # MinSpareServers: minimum number of serverprocesses which are kept spare  
  83. # MaxSpareServers: maximum number of serverprocesses which are kept spare  
  84. # ServerLimit: maximum value for MaxClientsfor the lifetime of the server  
  85. # MaxClients: maximum number of serverprocesses allowed to start  
  86. # MaxRequestsPerChild: maximum number ofrequests a server process serves  
  87. <IfModule prefork.c>  
  88. #启动服务器时的进程数目  
  89. StartServers       8  
  90. #最小空闲进程  
  91. MinSpareServers    5  
  92. #最大空闲进程  
  93. MaxSpareServers   20  
  94. #服务器限制 ServerLimit MaxClients保持一致  
  95. ServerLimit      256  
  96. #最多支持客户端访问  
  97. MaxClients       256  
  98. #最大的请求数量  
  99. MaxRequestsPerChild  4000  
  100. </IfModule>  
  101.    
  102. [root@serv01 conf]# ps -ef|grep http  
  103. root     1341     1  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  104. apache   1343  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  105. apache   1344  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  106. apache   1345  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  107. apache   1346  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  108. apache   1347  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  109. apache   1348  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  110. apache   1349  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  111. apache   1350  1341  0 22:44 ?        00:00:00 /usr/sbin/httpd  
  112. root     1351  1149  0 22:44 pts/0    00:00:00 vim httpd.conf  
  113. root     1360  1266  0 22:58 pts/1    00:00:00 grep http  
  114.    
  115. # worker MPM  
  116. # StartServers: initial number of serverprocesses to start  
  117. # MaxClients: maximum number of simultaneousclient connections  
  118. # MinSpareThreads: minimum number of workerthreads which are kept spare  
  119. # MaxSpareThreads: maximum number of workerthreads which are kept spare  
  120. # ThreadsPerChild: constant number of workerthreads in each server process  
  121. # MaxRequestsPerChild: maximum number ofrequests a server process serves  
  122. <IfModule worker.c>  
  123. #开始启动的  
  124. StartServers         4  
  125. #最小大的客户端  
  126. MaxClients         300  
  127. #每个进程最小支持的线程  
  128. MinSpareThreads     25  
  129. #每个进程最大支持的线程  
  130. MaxSpareThreads     75  
  131. #每个服务进程支持的线程数  
  132. ThreadsPerChild     25  
  133. #最大的服务次数,为0不限制  
  134. MaxRequestsPerChild  0  
  135. </IfModule>  
  136.    
  137. [root@serv01 conf]# httpd -l  
  138. Compiled in modules:  
  139.  core.c  
  140.  prefork.c  
  141.  http_core.c  
  142.  mod_so.c  
  143.         
  144. #监听  
  145. Listen 80  
  146.    
  147.        [root@serv01 conf]# netstat -langput|grephttpd  
  148. tcp       0      0 :::80                       :::*                        LISTEN      2414/httpd  
  149.    
  150. http://192.168.1.11:8080/  
  151. [root@serv01 conf]# netstat -langput|grephttpd  
  152. tcp       0      0 :::8080                     :::*                        LISTEN      2437/httpd  
  153.    
  154. #动态加载模块  
  155. LoadModule  
  156.         
  157. #包含文件    
  158. Include conf.d/*.conf  
  159.         
  160. #运行进程的用户  
  161. User apache  
  162. #运行进程的用户组  
  163. Group apache  
  164.    
  165. [root@serv01 conf]# ps -ef|grep http  
  166. root     2510     1  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  167. larry    2512  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  168. larry    2513  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  169. larry    2514  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  170. larry    2515  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  171. larry    2516  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  172. larry    2517  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  173. larry    2518  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  174. larry    2519  2510  0 23:28 ?        00:00:00 /usr/sbin/httpd  
  175. root     2521  1266  0 23:28 pts/1    00:00:00 grep http  
  176.    
  177. #如果用户不存在,会报错  
  178. [root@serv01 conf]# /etc/init.d/httpd restart  
  179. Stopping httpd:                                           [  OK  ]  
  180. Starting httpd: httpd: bad user name justdb  
  181.                                                           [FAILED]  
  182.    
  183. #服务器邮件地址  
  184. ServerAdmin root@localhost  

四 启动不报警告解决

[plain] view plaincopy
  1. [root@serv01 conf]# /etc/init.d/httpd restart  
  2. Stopping httpd:                                           [  OK  ]  
  3. Starting httpd: httpd:apr_sockaddr_info_get() failed for serv01.host.com  
  4. httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName  
  5.                                                           [  OK  ]  
  6.         
  7. [root@serv01 conf]# vim httpd.conf  
  8. [root@serv01 conf]# cat httpd.conf |grepServerName  
  9. # ServerName gives the name and port that theserver uses to identify itself.  
  10. #ServerName www.example.com:80  
  11. ServerName serv01.host.com  
  12. # ServerName directive.  
  13. #   ServerName dummy-host.example.com  
  14. [root@serv01 conf]# /etc/init.d/httpd restart  
  15. Stopping httpd:                                            [  OK  ]  
  16. Starting httpd:                                           [  OK  ]  

五 Web根目录和别名设置

[sql] view plaincopy
  1. #别名:Alias 真实路径不在Web根目录下  
  2. --第一步,修改DocumentRoot  
  3. [root@serv01 conf]# cat httpd.conf | grepDocumentRoot  
  4. DocumentRoot "/web"  
  5.    
  6. Alias /upload "/upload"  
  7. Alias /up "/upload"  
  8.    
  9. --第二步,创建测试文件  
  10. [root@serv01 conf]# mkdir /web  
  11. [root@serv01 conf]# vim /web/index.html  
  12. [root@serv01 conf]# cat /web/index.html  
  13. this is index.html  
  14. [root@serv01 conf]# mkdir /upload  
  15. [root@serv01 conf]# mkdir /web/upload/  
  16. [root@serv01 conf]# vim /upload/index.html  
  17. [root@serv01 conf]# cat /upload/index.html  
  18. this is /upload index.html  
  19. [root@serv01 conf]# vim/web/upload/index.html  
  20. [root@serv01 conf]# cat/web/upload/index.html  
  21. this is /web/upload/ index.html  
  22.    
  23. --第三步,设置别名  
  24. [root@serv01 conf]# tail -n1 httpd.conf  
  25. Alias /up "/upload"  
  26. [root@serv01 conf]# /etc/init.d/httpd restart  
  27. Stopping httpd:                                           [  OK  ]  
  28. Starting httpd:                                           [  OK  ]  
  29.    
  30. --第四步,测试  
  31. 浏览器输入:http://192.168.1.11/up/, 页面显示this is /upload index.html;浏览器输入http://192.168.1.11/,页面显示this is index.html;浏览器输入http://192.168.1.11/upload,页面显示this is /web/upload/ index.html  

六 对目录权限的限制

[plain] view plaincopy
  1. <Directory />  
  2.    Options FollowSymLinks  
  3.    AllowOverride None  
  4. </Directory>  
  5.    
  6. #配置文件中,关键字不区分大小写  
  7.        <directory "/upload"/>  
  8.               Option Indexes FollowSymLinks  
  9. </directory>  
  10.    
  11. #假如文件夹下没有index文件,那么以文件列表显示  
  12. Indexes  
  13. #显示软链接文件  
  14. FollowSymLinks  
  15. #如果没有index.html文件,则不显示(Forbidden)  
  16.        None  
  17.    
  18. [root@serv01 conf]# tail -n5 httpd.conf  
  19. Alias /up "/upload"  
  20.    
  21. <directory "/upload">  
  22.        OptionsIndexes FollowSymLinks  
  23. </directory>  
  24.    
  25.  [root@serv01 conf]# cd /upload/  
  26. [root@serv01 upload]# mkdir user  
  27. [root@serv01 upload]# cp /boot/* user/  
  28. oot@serv01 user]# cd /  
  29. [root@serv01 /]# dd if=/dev/zero of=aa01.isobs=1M count=5  
  30. 5+0 records in  
  31. 5+0 records out  
  32. 5242880 bytes (5.2 MB) copied, 0.0260953 s,201 MB/s  
  33. [root@serv01 /]# cd -  
  34. /upload/user  
  35. [root@serv01 user]# ln -s /aa01.iso aa01.iso  
  36.    
  37. [root@serv01 conf]# vim httpd.conf  
  38. [root@serv01 conf]# tail -n5 httpd.conf  
  39. Alias /up "/upload"  
  40.    
  41. <directory "/upload">  
  42.        Optionsnone FollowSymLinks  
  43. </directory>  
  44. [root@serv01 conf]# /etc/init.d/httpd restart  
  45. Stopping httpd:                                           [  OK  ]  
  46. Starting httpd:                                           [  OK  ]  
  47.    
  48. #子目录继承上级目录的权限  
  49. [root@serv01 conf]# tail -n5 httpd.conf  
  50. Alias /up "/upload"  
  51.    
  52. <directory "/upload/user">  
  53.        OptionsIndexes FollowSymLinks  
  54. </directory>  

七 参数AllowOverride做权限控制 all(授权用户)

[sql] view plaincopy
  1. AllowOverride all  
  2. AllowOverride  
  3.    
  4. --第一步,编辑httpd.conf文件,加入AllowOverride参数,并设置成all  
  5. [root@serv01 conf]# vim httpd.conf  
  6. [root@serv01 conf]# tail -n6 httpd.conf  
  7. Alias /up "/upload"  
  8.    
  9. <directory "/upload/user">  
  10.        OptionsIndexes FollowSymLinks  
  11.        AllowOverrideall  
  12. </directory>  
  13.    
  14. --第二步,在限制的文件夹里新建.htaccess文件,编辑如下内容  
  15. [root@serv01 user]# vim .htaccess  
  16. [root@serv01 user]# cat .htaccess  
  17. authuserfile"/etc/httpd/conf/htpasswd"  
  18. authtype basic  
  19. authname "please input your name andpassword"  
  20. require valid-user  
  21. [root@serv01 user]# pwd  
  22. /upload/user  
  23.    
  24. --第三步,创建测试用户,这里创建的用户根系统帐号无关,所以无需配置成系统用户  
  25. [root@serv01 user]# htpasswd -c/etc/httpd/conf/htpasswd justdb  
  26. New password:  
  27. Re-type new password:  
  28. Adding password for user justdb  
  29. --第四步,重启服务  
  30. [root@serv01 conf]# /etc/init.d/httpd restart  
  31. Stopping httpd:                                           [  OK  ]  
  32. Starting httpd:                                           [  OK  ]  
  33.    
  34. --第五步,测试  
  35. http://192.168.1.11/up/user/  

八 网段IP限制

[plain] view plaincopy
  1. #先允许,后禁止,那么192.168.1.1不能访问  
  2. Order Allow,Deny  
  3. Allow from 192.168.1.1  
  4. Deny from all  
  5. #先禁止,后允许,那么192.168.1.1可以访问  
  6. Order Deny,Allow  
  7. Allow from 192.168.1.1  
  8. Deny from all  
  9.    
  10. [root@serv01 conf]# tail -n11 httpd.conf  
  11. Alias /up "/upload"  
  12.    
  13. <directory "/upload/user">  
  14.        OptionsIndexes FollowSymLinks  
  15.        AllowOverrideall  
  16.        OrderAllow,Deny  
  17.        Allowfrom 192.168.1.1  
  18.        #Denyfrom 192.168.1.1  
  19.        #Denyfrom all  
  20.        #Allowfrom 192.168.1.0/24  
  21. </directory>  

九 用户个性化主页

[sql] view plaincopy
  1. --第一步,修改配置文件  
  2. [root@serv01 conf]# vim httpd.conf  
  3. [root@serv01 conf]# cat httpd.conf |grepUserDir -n  
  4. 351:# UserDir: The name of the directory thatis appended onto a user's home  
  5. 364:   # UserDir is disabled by default since it can confirm the presence  
  6. 368:   #UserDir disabled  
  7. 372:   # directory, remove the "UserDir disabled" line above, anduncomment  
  8. 375:   UserDir public_html  
  9. 380:# Control access to UserDirdirectories.  The following is an example  
  10.    
  11. #  
  12. # Control access to UserDir directories.  The following is an example  
  13. for a site where these directories arerestricted to read-only.  
  14. #  
  15.    
  16. #以下可以打开或者保持注释,都可以正常完成功能  
  17. <Directory /home/*/public_html>  
  18.    AllowOverride FileInfo AuthConfig Limit  
  19.    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec  
  20.    <Limit GET POST OPTIONS>  
  21.        Order allow,deny  
  22.        Allow from all  
  23.    </Limit>  
  24.    <LimitExcept GET POST OPTIONS>  
  25.        Order deny,allow  
  26.        Deny from all  
  27.    </LimitExcept>  
  28. </Directory>  
  29.    
  30. --第二步,添加用户  
  31. [root@serv01 home]# useradd larry01  
  32. [root@serv01 home]# useradd larry02  
  33. [root@serv01 home]# useradd larry03  
  34.    
  35. --第三步,进入用户主目录,分别创建public_html文件夹,然后分别创建index.html文件,内容如下  
  36. [root@serv01 home]# cd larry01  
  37. [root@serv01 larry01]# mkdir public_html  
  38. [root@serv01 public_html]# vim index.html  
  39.    
  40. [root@serv01 public_html]# cat/home/larry01/public_html/index.html  
  41. this is larry01 home dir  
  42.    
  43. [root@serv01 home]# cd larry02  
  44. [root@serv01 larry01]# mkdir public_html  
  45. [root@serv01 public_html]# vim index.html  
  46. [root@serv01 public_html]# cat/home/larry02/public_html/index.html  
  47. this is larry02 home dir  
  48.    
  49. [root@serv01 home]# cd larry03  
  50. [root@serv01 larry01]# mkdir public_html  
  51. [root@serv01 public_html]# vim index.html  
  52. [root@serv01 public_html]# cat/home/larry03/public_html/index.html  
  53. this is larry03 home dir  
  54.    
  55. --第四步,修改家录权限(755或者777或者711,只需要有x的权限即可)  
  56. [root@serv01 public_html]# chmod 755/home/larry01  
  57. [root@serv01 public_html]# ll -d/home/larry01  
  58. drwxr-xr-x. 4 larry01 larry01 4096 Aug 1517:56 /home/larry01  
  59.    
  60. [root@serv01 public_html]# chmod 755/home/larry02  
  61. [root@serv01 public_html]# ll -d/home/larry02  
  62. drwxr-xr-x. 4 larry01 larry02 4096 Aug 1517:56 /home/larry02  
  63.    
  64. [root@serv01 public_html]# chmod 755/home/larry03  
  65. [root@serv01 public_html]# ll -d/home/larry03  
  66. drwxr-xr-x. 4 larry01 larry03 4096 Aug 1517:56 /home/larry03  
  67.    
  68. --第五步,重启服务  
  69. [root@serv01 conf]# /etc/init.d/httpd restart  
  70. Stopping httpd:                                           [  OK  ]  
  71. Starting httpd:                                           [  OK  ]  
  72.    
  73. --第六步,测试  
  74. 输入:http://192.168.1.11/~larry01/  
  75. 浏览器显示:this is larry01 home dir  
  76.    
  77. 输入:http://192.168.1.11/~larry02/  
  78. 浏览器显示:this is larry02 home dir  
  79.    
  80. 输入:http://192.168.1.11/~larry03/  
  81. 浏览器显示:this is larry03 home dir  

十 其他参数

 

[sql] view plaincopy
  1. #主页是index.html index.html.var的参数控制  
  2. DirectoryIndex index.html index.html.var  
  3.    
  4. #用户访问控制的文件名  
  5. AccessFileName .htaccess  
  6.    
  7. #对文件的限制,拒绝访问.htaccess文件  
  8. <Files ~ "^\.ht">  
  9.    Order allow,deny  
  10.    Deny from all  
  11.    Satisfy All  
  12. </Files>  
  13.    
  14. #类型配置文件  
  15. TypesConfig /etc/mime.types  
  16.    
  17. #Web服务器支持传送的文件类型  
  18. [root@serv01 public_html]# cat/etc/mime.types  
  19.    
  20. #默认类型  
  21. DefaultType text/plain  
  22.    
  23. #错误日志文件存放位置  
  24.  ErrorLog logs/error_log  
  25.         
  26. #日志警告级别  
  27.  LogLevel warn  
  28.         
  29. #日志格式,后面的名字用于引用  
  30. LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined  
  31. LogFormat "%h %l %u %t \"%r\"%>s %b" common  
  32. LogFormat "%{Referer}i -> %U" referer  
  33. LogFormat "%{User-agent}i" agent  
  34.         
  35. #自定义日志格式  
  36.  CustomLog logs/access_log combined  
  37.         
  38. #服务器签名       
  39. ServerSignature On  
  40.  #别名  
  41. Alias /icons/ "/var/www/icons/"  
  42.    
  43. #语言支持  
  44. AddLanguage ca .ca  
  45.    
  46. BrowserMatch "Mozilla/2"nokeepalive  
  47. BrowserMatch "MSIE 4\.0b2;"nokeepalive downgrade-1.0 force-response-1.0  
  48. BrowserMatch "RealPlayer 4\.0"force-response-1.0  
  49. BrowserMatch "Java/1\.0"force-response-1.0  
  50. BrowserMatch "JDK/1\.0"force-response-1.0  
  51.    
  52. #错误的记录目录  
  53. Alias /error/ "/var/www/error/"  

十一 CGI示例

[sql] view plaincopy
  1. --第一步,查看配置文件,不需要任何修改  
  2. ScriptAlias /cgi-bin/"/var/www/cgi-bin/"  
  3.    
  4. --第二步,进入/var/www/cgi-bin/目录,编辑一个可执行的脚本  
  5. [root@serv01 public_html]# cd/var/www/cgi-bin/  
  6. [root@serv01 cgi-bin]# ll  
  7. total 0  
  8. [root@serv01 cgi-bin]# vim test.sh  
  9. [root@serv01 cgi-bin]# chmod a+x ./*  
  10. [root@serv01 cgi-bin]# cat test.sh  
  11. #!/bin/bash  
  12.    
  13. echo "Content-type:text/html"  
  14. #小写亦可  
  15. echo "content-type:text/html"  
  16.    
  17. echo ""  
  18.    
  19. echo "hello,world"  
  20. [root@serv01 cgi-bin]# vim test.sh  
  21. [root@serv01 cgi-bin]# cat test.sh  
  22. #!/bin/bash  
  23.    
  24. echo "Content-type:text/html"  
  25. echo ""  
  26.    
  27. #echo "hello,world"  
  28. ps -ef  
  29.    
  30. --第三步,测试  
  31. http://192.168.1.11/cgi-bin/test.sh  

十二 浏览器调用C语言程序

[sql] view plaincopy
  1. --第一步,编写C语言程序  
  2.  [root@serv01 cgi-bin]# vim main.c  
  3. [root@serv01 cgi-bin]# cat main.c  
  4. #include <stdio.h>  
  5.    
  6. int main(void)  
  7. {  
  8.        printf("Context-type:text/html\n\n\n");  
  9.        printf("helloworld!!!\n");  
  10.        return0;  
  11. }  
  12. --第二步,安装gcc和c++相关的rpm包  
  13. [root@serv01 cgi-bin]# yum install gcc c++*-y  
  14.    
  15. --第三步,编译  
  16. [root@serv01 cgi-bin]# gcc main.c -o main  
  17. [root@serv01 cgi-bin]# ./main  
  18. Context-type:text/html  
  19.    
  20.    
  21. hello world!!!  
  22.    
  23. --第四步,测试  
  24. http://192.168.1.11/cgi-bin/main  
  25.    
  26. hello world!!!  


十三 虚拟主机——不同的域名访问不同的主页——域名实现

 

#一个apache实现多个域名,www.larrywen.com和www.justdb.com

有三种实现方式,通过域名、IP地址和端口。其中基于域名实现虚拟主机最常用。

 

[sql] view plaincopy
  1. #不同的域名访问不同的主页——域名实现  
  2.    
  3. --第一步,搭建DNS  
  4. --1.安装DNS相关RPM包  
  5. [root@serv02 ~]# yum install bind* -y  
  6. --2.编辑named.conf和named.rfc1912.zones文件  
  7.    
  8. [root@serv02 etc]# vim named.conf  
  9. [root@serv02 etc]# vim named.rfc1912.zones  
  10. [root@serv02 named]# cat /etc/named.conf  
  11. options {  
  12.        listen-onport 53 { any; };  
  13.        allow-query     { any; };  
  14. };  
  15.    
  16. [root@serv02 named]# tail -n11/etc/named.rfc1912.zones  
  17. zone "larrywen.com" IN {  
  18.        type master;  
  19.        file "larrywen.com.zone";  
  20.        allow-update { none; };  
  21. };  
  22.    
  23. zone "justdb.com" IN {  
  24.        type master;  
  25.        file "justdb.com.zone";  
  26.        allow-update { none; };  
  27. };  
  28. --3.编辑larrywen.com.zone和justdb.com.zone文件  
  29. [root@serv02 named]# cp named.localhostlarrywen.com.zone -a  
  30. [root@serv02 named]# vim larrywen.com.zone  
  31. [root@serv02 named]# cp named.localhostjustdb.com.zone -a  
  32. [root@serv02 named]# vim justdb.com.zone  
  33. [root@serv02 named]# cat larrywen.com.zone  
  34. $TTL 1D  
  35. @    INSOA   dns.larrywen.com. root.larrywen.com.(  
  36.                                    0     ; serial  
  37.                                    1D   ; refresh  
  38.                                    1H   ; retry  
  39.                                    1W  ; expire  
  40.                                    3H)       ; minimum  
  41.        NS   dns.larrywen.com.  
  42. dns  IN    A     192.168.1.12  
  43. www      IN    A     192.168.1.11  
  44. [root@serv02 named]# cat justdb.com.zone  
  45. $TTL 1D  
  46. @    INSOA   dns.justdb.com. root.justdb.com. (  
  47.                                    0     ; serial  
  48.                                    1D   ; refresh  
  49.                                    1H   ; retry  
  50.                                    1W  ; expire  
  51.                                    3H)       ; minimum  
  52.        NS   dns.justdb.com.  
  53. dns  IN    A     192.168.1.12  
  54. www      IN    A     192.168.1.11  
  55. --4.重启服务  
  56. [root@serv02 named]# /etc/init.d/named start  
  57. Starting named:                                           [  OK  ]  
  58. --5.虚拟机做测试,安装bind-utils  
  59. [root@serv01 cgi-bin]# yum install bind-utils-y  
  60. [root@serv01 cgi-bin]# vim /etc/resolv.conf  
  61. [root@serv01 cgi-bin]# cat /etc/resolv.conf  
  62. nameserver 192.168.1.12  
  63. [root@serv01 cgi-bin]# dig www.larrywen.com+short  
  64. 192.168.1.11  
  65. [root@serv01 cgi-bin]# dig www.justdb.com+short  
  66. 192.168.1.11  
  67.    
  68. --第二步,修改配置文件httpd.conf  
  69. [root@serv01 conf]# cat httpd.conf  
  70. NameVirtualHost *:80  
  71.    
  72. <VirtualHost *:80>  
  73.    ServerAdmin root@larrywen.com  
  74.    DocumentRoot /larrywen  
  75.    ServerName www.larrywen.com  
  76.    ErrorLog logs/dummy-host.larrywen.com-error_log  
  77.    CustomLog logs/dummy-host.larrywen.com-access_log common  
  78. </VirtualHost>  
  79. <VirtualHost *:80>  
  80.    ServerAdmin root@justdb.com  
  81.    DocumentRoot /justdb  
  82.    ServerName www.justdb.com  
  83.    ErrorLog logs/dummy-host.justdb.com-error_log  
  84.    CustomLog logs/dummy-host.justdb.com-access_log common  
  85. </VirtualHost>  
  86.    
  87. --第三步,真实机加上DNS解析  
  88. [root@larrywen 0815]# echo "nameserver192.168.1.12" > /etc/resolv.conf  
  89. [root@larrywen 0815]# cat /etc/resolv.conf  
  90. nameserver 192.168.1.12  
  91. #真实机做测试  
  92. [root@larrywen 0815]# dig www.larrywen.com+short  
  93. 192.168.1.11  
  94. [root@larrywen 0815]# dig www.justdb.com+short  
  95. 192.168.1.11  
  96.    
  97. --第四步,虚拟机(Serv01)重启服务  
  98. [root@serv01 conf]# /etc/init.d/httpd restart  
  99. Stopping httpd:                                           [  OK  ]  
  100. Starting httpd:                                           [  OK  ]  
  101.    
  102. --第五步,测试  
  103. http://www.justdb.com/  
  104. http://www.larrywen.com/  


十四 虚拟主机——不同的IP地址访问不同的主页——IP地址实现

[sql] view plaincopy
  1.    
  2. 192.168.1.11对应http://www.larrywen.com/  
  3.    
  4. 172.16.1.11对应http://www.justdb.com/  
  5.    
  6. --第一步,添加网卡,修改IP地址  
  7. [root@serv01 cgi-bin]# ifconfig eth1172.16.1.11 netmask 255.255.255.0  
  8. [root@serv01 cgi-bin]# ifconfig eth1|grep"inet addr"  
  9.          inet addr:172.16.1.11 Bcast:172.16.1.255  Mask:255.255.255.0  
  10.    
  11. --第二步,修改配置文件  
  12. [root@serv01 conf]# vim httpd.conf  
  13. [root@serv01 conf]# cat httpd.conf  
  14. #<VirtualHost *:80>  
  15.    
  16.    <VirtualHost 192.168.1.11:80>  
  17.    DocumentRoot /larrywen  
  18.    ServerName serv01.host.com  
  19. </VirtualHost>  
  20. <VirtualHost 172.16.1.11:80>  
  21.    DocumentRoot /justdb  
  22.    ServerName serv01.host.com  
  23. </VirtualHost>  
  24.    
  25. --第三步,重启服务  
  26. [root@serv01 conf]# /etc/init.d/httpd restart  
  27. Stopping httpd:                                           [  OK  ]  
  28. Starting httpd:                                            [  OK  ]  
  29.    
  30. --第四步,测试  
  31. http://192.168.1.11  
  32. this is larrywen  
  33. http://172.16.1.11/  
  34. this is justdb  

十五 虚拟主机——不同的IP地址访问不同的主页——端口实现

[sql] view plaincopy
  1. --第一步,修改配置文件  
  2. [root@serv01 conf]# cat httpd.conf  
  3. Listen 80  
  4. Listen 8080  
  5. <VirtualHost *:80>  
  6.    DocumentRoot /larrywen  
  7. </VirtualHost>  
  8. <VirtualHost *:8080>  
  9.    DocumentRoot /justdb  
  10. </VirtualHost>  
  11. [root@serv01 conf]# netstat -lanput|grephttpd  
  12. tcp       0      0 :::8080                     :::*                        LISTEN      1950/httpd           
  13. tcp       0      0 :::80                       :::*                        LISTEN      1950/httpd  
  14.    
  15. --第二步,重启服务  
  16. [root@serv01 conf]# /etc/init.d/httpd restart  
  17. Stopping httpd:                                           [  OK  ]  
  18. Starting httpd:                                           [  OK  ]  
  19.    
  20. --第三步,测试  
  21. http://192.168.1.11/  
  22. this is larrywen  
  23.    
  24. http://192.168.1.11:8080/  
  25. this is justdb  


0 0