第6章 配置Apache做Web服务器_第1页
第6章 配置Apache做Web服务器_第2页
第6章 配置Apache做Web服务器_第3页
第6章 配置Apache做Web服务器_第4页
第6章 配置Apache做Web服务器_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第5章 配置Apache做Web服务器Linux Apache MySQL PHP的组合简称为LAMP。Apache简介Apache不仅在Windows上占有不少市场,在Linux上更是使用最多的Web服务器软件。Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。Apache的特点是简单、速度快、性能稳定,并可做代理服务器来使用。本来它只用于小型或试验Internet网络,后来逐步扩充到各种Unix系统中,尤其对Linux的支持相当完美。Apache有多种产品,可以支持SSL技术,支持

2、多个虚拟主机。Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适合于多处理器环境,因此,在一个Apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。到目前为止Apache仍然是世界上用的最多的Web服务器,市场占有率达60%左右。世界上很多著名的网站如Amazon、Yahoo!、W3 Consortium、Financial Times等都是Apache的产物,它的成功之处主要在于它的源代码开放、有一支开放的开发队伍、支持跨平台的应用(可以运行在几乎所有的Unix、Windows、Linux系统平台上)以及它的可移植性等方面。练习1:安装Apac

3、he2确保服务器能够访问Internet。apt-get install apache2查看apache2的安装rootWeb:# dpkg -l apache2输入netstat antp,查看侦听的端口,可以看到Apache2侦听的端口禁用防火墙,默认情况下Ubuntu的防火墙启用,没有打开80端口,本章禁用防火墙。在Windows上测试到Apache的访问在Windows上访问Apache默认网站。练习2:创建虚拟Web主机本练习会禁用默认Apache默认Web站点,创建两个基于域名的虚拟Web主机1. 禁用默认网站rootubuntuS

4、erver:# a2dissite default2. 准备网页在Windows上访问和将网页保存为htm格式分别重名为baidu.htm和sohu.htm,保存类型一定要按图示红色部分所框。3. 准备网站网页使用WinSCP在连接Ubuntu服务器,在默认情况下,Apache把网站放在/var/www目录下,在该目录创建里两个文件夹和。建议将所有网站放到该目录下,目录结构如下安排/var/www//var/www/使用WinSCP将Window

5、s上的baidu和souhu的网页和相关文件夹拖拽到相应目录,别忘了相关文件夹。Sohu.htm网页和相关文件夹,将其拖拽到目录下。4. 创建网站配置文件针对和两个网站创建两个网站配置文件。在网站配置文件中绑定域名、指定网站目录。cp /etc/apache2/sites-available/default /etc/apache2/sites-available/cp /etc/apache2/sites-available/default /etc/apache2/sites-availab

6、le/编辑配置文件,红色部分是需要修改或添加部分,其他的都是默认,注意DirectoryIndex baidu.htm设置网站首页。rootubuntuServer:# vi /etc/apache2/sites-available/ ServerName ServerAdmin webmasterlocalhost DocumentRoot /var/www/ Options FollowSymLinks AllowOverride None Options Indexes FollowS

7、ymLinks MultiViews AllowOverride None Order allow,deny allow from all DirectoryIndex baidu.htmErrorLog $APACHE_LOG_DIR/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog $APACHE_LOG_DIR/access.log combined Alias /doc/ /usr/share/doc/ Opti

8、ons Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from / :1/128 给搜狐网站绑定域名和网站目录rootubuntuServer:# vi /etc/apache2/sites-available/ ServerName ServerAdmin webmasterlocalhost DocumentRoot /var/www/www.soh

9、 Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all DirectoryIndex so

10、hu.htmErrorLog $APACHE_LOG_DIR/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog $APACHE_LOG_DIR/access.log combined Alias /doc/ /usr/share/doc/ Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all

11、Allow from / :1/128 启用默认网站和网站rootubuntuServer:# a2ensite rootubuntuServer:# a2ensite 重启apache服务rootubuntuServer:# /etc/init.d/apache2 restart5. 在物理机上测试使用域名标识的网站只能使用域名访问,不支持23这种IP地址的访问,为了确保物理机能够将和www.baid

12、域名解析为Ubuntu服务器,你需要更改hosts文件,添加两条记录。修改物理机C:WindowsSystem32driversetc文件,使用记事本打开,添加到和两个网站的IP地址和域名对应关系,保存。测试一名解析结果,可以看到这两个域名已经都解析到Ubuntu的网站地址了。使用域名访问网站练习3:密码认证网站的某些网页需要身份验证后才能访问,下面的练习将会为网站的private目录设置为基本身份验证。以虚拟Web主机为例创建一个基本认证的目录private1. 创建需要身份验证的目录

13、rootubuntuServer:# mkdir /var/www//private2. 编辑配置文件编辑网站配置文件指定网站的那个目录需要身份验证。rootubuntuServer:# vi /etc/apache2/sites-available/配置文件红色部分定义了private目录需要身份验证,注意红色部分,hao123.htm是虚拟目录的首页,需要添加上。 ServerName ServerAdmin webmasterlocalhost DocumentRoot /var/www/

14、Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all DirectoryIndex sohu.htm

15、 hao123.htmErrorLog $APACHE_LOG_DIR/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog $APACHE_LOG_DIR/access.log combined Alias /doc/ /usr/share/doc/ Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from

16、 all Allow from / :1/128 Options Indexes FollowSymLinks MultiViews AllowOverride AuthConfig Order allow,deny allow from all 3. 重新加载Apache服务rootubuntuServer:# /etc/init.d/apache2 reload4. 建立认证文件建立认证文件,先创建一个目录,用来存放认证文件rootubuntuServer:# mkdir /var/www//auth以下命令创建认证文件,并创建了

17、用户han 密码是a1!,查看创建的用户。rootubuntuServer:# chmod a+rx /var/www//auth/rootubuntuServer:# cd /var/www//auth/rootubuntuServer:/var/www//auth# htpasswd -bc private.passwords han a1!Adding password for user usernamerootubuntuServer: /var/www//auth#lsprivate.pa

18、sswordsrootubuntuServer:/var/www//auth# cat private.passwordshan:$apr1$msubiOhJ$q3LM1FxJRFZEfz2UWBHlV/5. 在private目录下创建一个.htaccess文件,该文件为隐藏文件rootubuntuServer:# cd /var/www//private/rootubuntuServer:/var/www//private# vi .htaccessAuthName Password NeededAuthType Bas

19、icAuthUserFile /var/www//auth/private.passwordsRequire valid-user6. 准备网页将网页拷贝一份到private目录,在这里将hao123的网页拷贝到重启Apache服务rootubuntuServer:/var/www/private# /etc/init.d/apache2 restart在Windows上访问访问成果练习4:禁用和启用Apache模块的配置本练习,你将学会查看apache2所有可用的模块,禁用模块基本身份验证模块和启用模块基本身份验证模块。1. 以下命令查看可用的模块rootubuntu

20、Server:# ls /etc/apache2/mods-availableactions.conf cern_meta.load ident.load proxy_ftp.loadactions.load cgi.load imagemap.load proxy_http.loadalias.conf cgid.conf include.load proxy_scgi.loadalias.load cgid.load info.conf reqtimeout.confasis.load charset_lite.load info.load reqtimeout.loadauth_basi

21、c.load dav.load ldap.conf rewrite.loadauth_digest.load dav_fs.conf ldap.load setenvif.confauthn_alias.load dav_fs.load log_forensic.load setenvif.loadauthn_anon.load dav_lock.load mem_cache.conf speling.loadauthn_dbd.load dbd.load mem_cache.load ssl.confauthn_dbm.load deflate.conf mime.conf ssl.load

22、authn_default.load deflate.load mime.load status.confauthn_file.load dir.conf mime_magic.conf status.loadauthnz_ldap.load dir.load mime_magic.load substitute.loadauthz_dbm.load disk_cache.conf negotiation.conf suexec.loadauthz_default.load disk_cache.load negotiation.load unique_id.loadauthz_groupfi

23、le.load dump_io.load proxy.conf userdir.confauthz_host.load env.load proxy.load userdir.loadauthz_owner.load expires.load proxy_ajp.load usertrack.loadauthz_user.load ext_filter.load proxy_balancer.conf version.loadautoindex.conf file_cache.load proxy_balancer.load vhost_alias.loadautoindex.load fil

24、ter.load proxy_connect.loadcache.load headers.load proxy_ftp.conf2. 禁用基本身份验证模块rootubuntuServer:# a2dismod auth_basicrootubuntuServer:# /etc/init.d/apache2 restart3. 验证练习3设置的密码身份验证是否工作正常4. 使用a2enmod查看可用的模块(apache2 enable module)rootubuntuServer:# a2enmod auth_basic5. 再次访问又支持身份验证。练习5:创建虚拟目录在www.sohu.c

25、om网站下面创建一个虚拟目录/sofang1. 创建目录存放sofang网页/var/www/sofang6. 访问,将网页保存到该目录7. 更改网站的配置文件,红色部分是添加部分,指定了别名和设置首页。 ServerName ServerAdmin webmasterlocalhost DocumentRoot /var/www/ Options FollowSymLinks AllowOverride None Options Indexes Follo

26、wSymLinks MultiViews AllowOverride None Order allow,deny allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all Alias /sofang /var/www/sofang DirectoryIndex sohu.htm hao123.htm sofang.htmErrorLog $A

27、PACHE_LOG_DIR/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog $APACHE_LOG_DIR/access.log combined Alias /doc/ /usr/share/doc/ Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.

28、0.0/ :1/128 Options Indexes FollowSymLinks MultiViews AllowOverride AuthConfig Order allow,deny allow from all DirectoryIndex hao123.htm 8. 重启apache2服务9. 访问网站测试练习6:配置网站安全配置/sofang目录只允许/28网段能够访问2. 更改搜狐网站配置文件,添加红框部分内容 ServerName ServerAdmin we

29、bmasterlocalhost DocumentRoot /var/www/ Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Ord

30、er allow,deny Allow from all Alias /sofang /var/www/sofang AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from /28 DirectoryIndex sohu.htm hao123.htm sofang.htmErrorLog $APACHE_LOG_DIR/error.log# Possible values include: debug, i

31、nfo, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog $APACHE_LOG_DIR/access.log combined Alias /doc/ /usr/share/doc/ Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from / :1/128 Options Indexes FollowSymLinks MultiV

32、iews AllowOverride AuthConfig Order allow,deny allow from all DirectoryIndex hao123.htm 3. 重启Apache服务4. 在Windows上测试,将VMNet8的地址设置为,你能打开10. 将VMNet8的地址设置为30,你不能打开练习6:查看Apache日志在Apache2.conf中定义了访问日志和错误日志存放位置和日志中记录的格式。对我们排除Apache故障提供线索。1. 查看错误日志定义和位置,Apache2.conf全局配置了网站错误日志格式和位置ro

33、otubuntuServer:# vi /etc/apache2/apache2.conf. ErrorLog $APACHE_LOG_DIR/error.log. LogFormat %v:%p %h %l %u %t %r %s %O %Refereri %User-Agenti vhost_combinedLogFormat %h %l %u %t %r %s %O %Refereri %User-Agenti combinedLogFormat %h %l %u %t %r %s %O commonLogFormat %Refereri - %U refererLogFormat %U

34、ser-agenti agent.2. 网站配置文件定义了错误日志存放位置rootubuntuServer:# vi /etc/apache2/sites-available/default.ErrorLog $APACHE_LOG_DIR/error.logCustomLog $APACHE_LOG_DIR/access.log combined.3. 查看网站访问日志打开错误日志打开访问日志,可以看到记录网站的访问情况。实现https(选作)启用mod_ssl模块生成证书、安装证书修改虚拟主机配置文件,重启Apache服务启用mod_ssl模块rootubuntuServer:# a2en

35、mod ssl安装openssl5. 创建CA签名的证书rootubuntuServer:# apt-get install openssl6. 创建Key,需要你输入密码,改密码在启动apache服务时需要输入。rootubuntuServer:# openssl genrsa -des3 -out server.key 10247. 创建证书签发请求(CSR),你可以将该请求发送给CA,也可以用来创建自签名证书rootubuntuServer:# openssl req -new -key server.key -out server.csrrootubuntuServer:# opens

36、sl req -new -key server.key -out server.csrEnter pass phrase for server.key: 输入第二步设置的密码You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you

37、 can leave some blankFor some fields there will be a default value,If you enter ., the field will be left blank.-Country Name (2 letter code) AU:cnState or Province Name (full name) Some-State:hebeiLocality Name (eg, city) :shijiazhuangOrganization Name (eg, company) Internet Widgits Pty Ltd:onestOr

38、ganizational Unit Name (eg, section) :eduCommon Name (eg, YOUR name) : 输入网站的域名Email Address :Please enter the following extra attributesto be sent with your certificate requestA challenge password :An optional company name :8. 创建自签名证书rootubuntuServer:# openssl x509 -re

39、q -days 365 -in server.csr -signkey server.key -out server.crt9. 生成的server.crt文件,就是自签名证书下面的操作将会安装自签名证书rootubuntuServer:# cp server.crt /etc/ssl/certs/rootubuntuServer:# cp server.key /etc/ssl/private/10. 配置虚拟主机配置文件,让其启用SSL即可,我们将修改虚拟主机配置文件,红色部分是需要更改或添加的部分。 ServerName ServerA

40、dmin webmasterlocalhost DocumentRoot /var/www SSLEngine on SSLOptions +StrictRequire SSLCertificateFile /etc/ssl/certs/server.crt SSLCertificateKeyFile /etc/ssl/private/server.key Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny

41、allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 11. 重启apache服务rootubuntuServer:# /etc/init.d/apache2 restart* Restarting web server apache2 apache2: Could not reliably determine the servers fully qualified domain name, using for ServerName . waiting apache2: Could not reliably deter

42、mine the servers fully qualified domain name, using for ServerNameApache needs to decrypt your SSL Keys for :443 (RSA)Please enter passphrase:输入密码 第二步指定的密码 测试https 能够访问Apache性能优化(选作)选择正确的多道处理模块(MPM)Apache2.0中影响性能的最核心特性:MPM(Multi -Processing Modules,多道处理模块)的基本工作原理,MPM的引入是Apache

43、2.0最重要的变化。Apache是基于模块化的设计,而Apache 2.0更扩展了模块化设计到Web服务器的最基本功能。服务器装载了一种多道处理模块,负责绑定本机网络端口、接受请求,并调度子进程来处理请求。扩展模块化设计有两个重要好处:1. Apache可以更简洁、有效地支持多种操作系统; 2. 服务器可以按站点的特殊需要进行自定制。我们主要阐述prefork和worker这两种和性能关系最大的产品级MPM。prefork的工作原理及配置 (虽然不是很快,但很稳定,能够隔离每个请求,每个请求出现故障,不会影响其他请求)控制进程在最初建立“StartServers”个子进程后,为了满足MinSp

44、areServers设置的需要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个如此按指数级增加创建的进程数,最多达到每秒32个,直到满足MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销以增加性能。 MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考虑同时加大MinSpareServers和MaxSpareServers。MaxClients数值要足够大,这样可以在访问高峰时发挥很好的性能,但不能太大,否则Apache所需内存将会超过物理内存。worker的工作原理特点 速度比prefork快很多,使用多线程进行访问处理。相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论