一 : Apache配置多个站点
Apache配置多个站点;2010-03-27 12:40
例如,现在您有一站点:
http://localhost/
想添加一个端口运行另外一个站点:
http://localhost:8080/
第一步: 新建一个配置文件
复制 httpd.conf 另存为 hooyes.conf (当然您可以起一个比这个更好听一点的名字)
第二步: 修改配置文件
打开 hooyes.conf 文件
将其中的端口设置: Listen:8080
将网站目录设置:
DocumentRoot "D:/Tweb/hooyes/webroot" (D:/Tweb/hooyes/webroot 为您的第二站点的目录)
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "D:/Tweb/hooyes/webroot"> (这个地方也要修改)
第三步:新建一个服务
========================================
配置Apache及实现多站点安装好Apache以后,可以在浏览器中输入http://localhost%20测试,如果配置正常会显示it's/ Working. 更改第一个站点的根目录:在文件Apache2.2\conf\httpd.conf中查找 DocumentRoot 属性,将后面的路径改为你的主站点的路径,如:D:\www\web1为第二个Apache服务建立配置文件:复制并重命名httpd.conf为web2.conf(举个例子而已,也可以叫my.conf等等),修改web2.conf中的Listen 8080(原来为80)、ServerName localhost:8080(原来为80)、DocumentRoot "D:/www/web2" (原来为web1)添加第二个Apache服务:Apache安装目录的bin子目录下,使用如下命令将Apache安装为Windows NT服务:httpd.exe -k install -n "服务名" -f "d:\apache2.2\conf\web2.conf"其他的命令:将Apache安装为Windows NT服务:apache -k install指定服务的名称,当你在同一机器上安装多个Apache服务时,你必须为它们指定不同的名字。apache -k install -n "服务名" 为不同名称的服务使用不同的配置文件,则安装时需要指定配置文件:apache -k install -n "服务名" -f "c:\files\my.conf" 如果你使用的是第一个命令,也就是除 -k install 外没有其它命令行参数,那么被安装的服务名称将是:Apache2 ,配置文件将使用conf\httpd.conf 。移除一个Apache服务:apache -k uninstall 使用下述命令移除特定名称的Apache服务:apache -k uninstall -n "服务名" 通常,启动、重启、关闭Apache服务的方法是使用Apache Service Monitor工具,另外也可以使用控制台命令:NET START Apache2 和 NET STOP Apache2 或者通过Windows服务控制面板。在启动Apache服务之前,你应当使用下面的命令检查一下配置文件的正确性:apache -n "服务名" -t 你可以通过命令行开关来控制Apache服务。要启动一个已经安装的Apache服务,可以使用:apache -k start 要停止一个已经安装的Apache服务,可以使用:apache -k stop 或apache -k shutdown 要重启一个运行中的Apache服务,强制它重新读取配置文件,可以使用:apache -k restart注意:如果使用Apache2.0以下的版本,服务程序
二 : Apache配置文件中的deny和allow的使用
Apache配置文件中的deny和allow的使用
由于产品的需要,最近在配置apache的负载均衡功能,但是在配置虚拟主机的访问权限的时候我们遇到了一些问题。[www.61k.com)主要问题是deny和allow的执行顺序,抽时间研究了下这两个参数的使用,现把deny和allow的使用情况总结如下。
一、 使用情况总结
我们来看下下面的apache的一个配置,具体代码如下:
<Directory "D:/TRS/Apache2.2.17/cgi-bin"> Order allow,deny #1 Allow from all #2 deny from 192.9.200.69 #3 </Directory> |
以前使用这两个参数的时候比较混乱,具体不太清楚到底是哪个参数在起作用。通过实验,我们可以总结下规律,具体规律如下:
1. 规律
当我们看到一个apache的配置时,可以从下面的角度来理解。一默认,二顺序,三重叠。
2. 上面配置说明
[1] 一默认
Order allow,deny ,这句话的作用是配置allow和deny的顺序,默认只有最后一个关键字起作用,这里起作用的关键字就是“deny”,默认拒绝所有请求。为了便于理解,我们可以画一个圆,圆的背景色涂上黑色,我们给这个圆起个编号,叫圆1。
[2] 二顺序
由于上边的Order指出判断的顺序是先判断allow的规则,然后才是deny的规则。所以我们要先判断allow的请求,由于该请求中配置的是allow from all,
所以表示该请求允许所有请求。这时我们再画一个圆,背景色涂上白色,我们给圆起个编号,叫圆2。
我们再来看deny的判断规则,由于 deny from 192.9.200.69 ,表示拒绝来自ip地址为“192.9.200.69”,所以我们可以画出一块红色区域,表示“192.9.200.69”,我们把这块区域叫区域3。
注意:即使把“Allow from all”写在“deny from 192.9.200.69”下面,依然是需要先判断allow规则,也就是说只有Order才能决定allow和order的优先顺序。
[3] 三重叠
我们把上边产生的圆1、圆2和区域3依次从下往上堆叠在一起。每个层都是不透明的,这时我们可以看到最终效果是除了“192.9.200.69”这块红色区域外,其他的所有都是白色区域。也就是只有“192.9.200.69”这个ip地址没有权限访问该目录,其他的请求都有权限访问该目录。
二、 看看下面的例子
也许上边没有说明白,我们再来看下面的例子,每个配置后面都有简单的说明,配置文件中的“#”号后边的数字表示配置项起作用的先后顺序。
1. 只允许192.9.200.69请求访问目录
<Directory "D:/TRS/Apache2.2.17/cgi-bin"> Order deny,allow #1.默认允许全部请求 Allow from 192.9.200.69 #3.重叠,允许IP192.9.200.69的请求 deny from all #2.按照顺序,先判断deny规则,拒绝所有请求 </Directory> |
2. 允许所有请求访问目录
<Directory "D:/TRS/Apache2.2.17/cgi-bin"> Order deny,allow #1.默认允许全部请求 Allow from all #3.重叠,允许所有请求 deny from 192.9.200.69 #2.按照顺序,先判断deny规则,拒绝192.9.200.69的请求 </Directory> |
3. 拒绝所有请求访问目录
<Directory "D:/TRS/Apache2.2.17/cgi-bin"> Order allow,deny #1.默认拒绝全部请求 Allow from 192.9.200.69 #2.顺序,允许 192.9.200.69请求 deny from all#3.重叠,拒绝所有请求 </Directory> |
4. 除了192.9.200.69的请求外,其他请求都可以访问目录
<Directory "D:/TRS/Apache2.2.17/cgi-bin"> Order allow,deny #1.默认拒绝全部请求 deny from 192.9.200.69#3.重叠,拒绝192.9.200.69请求 Allow from all #2.顺序,允许所有请求 </Directory> |
转自:http://blog.csdn.net/wgw335363240/article/details/6362418
扩展:apache deny allow / nginx deny allow / order deny allow
三 : Apache最佳配置
#
# This is the main Apache HTTP server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2/> for detailed information.
# In particular, see
# <URL:>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
# with ServerRoot set to "D:/website/APMServ5.2.6/Apache" will be interpreted by the
# server as "D:/website/APMServ5.2.6/Apache/logs/foo.log".
#
# NOTE: Where filenames are specified, you must use forward slashes
# instead of backslashes (e.g., "D:/website/APMServ5.2.6/Apache" instead of "c:\apache").
# If a drive letter is omitted, the drive on which Apache.exe is located
# will be used by default. It is recommended that you always supply
# an explicit drive letter in absolute paths, however, to avoid
# confusion.
#
# ThreadsPerChild: constant number of worker threads in the server process
# MaxRequestsPerChild: maximum number of requests a server process serves
ThreadsPerChild 500
MaxRequestsPerChild 10000
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to point the LockFile directive
# at a local disk. If you wish to share the same ServerRoot for multiple
# httpd daemons, you will need to change at least LockFile and PidFile.
#
ServerRoot "D:/website/APMServ5.2.6/Apache"
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 80
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modul
es/mod_auth_basic.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authzwww.61k.commodule modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule cgi_module modules/mod_cgi.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule deflate_module modules/mod_deflate.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule headers_module modules/mod_headers.so
LoadModule imagemap_module modules/mod_imagemap.so
LoadModule include_module modules/mod_include.so
#LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule speling_module modules/mod_speling.so
#LoadModule status_module modules/mod_status.so
#LoadModule unique_id_module modules/mod_unique_id.so
LoadModule userdir_module modules/mod_userdir.so
#LoadModule usertrack_module modules/mod_usertrack.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule ssl_module modules/mod_ssl.so
LoadFile "D:/website/APMServ5.2.6/PHP/libmysql.dll"
LoadModule php5_module "D:/website/APMServ5.2.6/PHP/php5apache2_2.dll"
PHPIniDir "D:/website/APMServ5.2.6/PHP/php.ini"
# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition. These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being de
fined.
#
#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin webmaster@localhost
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName 127.0.0.1:80
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "D:/website/APMServ5.2.6/www/htdocs"
#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "D:/website/APMServ5.2.6/www/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# #options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex default.php index.html index.htm default.htm index.php index.cgi default.cgi index.pl default.pl index.shtml default.shtml
</IfModule>
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
</FilesMatch>
#
# ErrorLog: The location of the error lo
g file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog logs/error.log
#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog logs/access.log common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
#CustomLog logs/access.log combined
</IfModule>
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.61k.combar
#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "D:/website/APMServ5.2.6/www/cgi-bin/"
</IfModule>
#
# "D:/website/APMServ5.2.6/Apache/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configure
d.
#
<Directory "D:/website/APMServ5.2.6/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
#
# Apache parses all CGI scripts for the shebang line by default.
# This comment line, the first line of the script, consists of the symbols
# pound (#) and exclamation (!) followed by the path of the program that
# can execute this specific script. For a perl script, with perl.exe in
# the C:\Program Files\Perl directory, the shebang line should be:
#!c:/program files/perl/perl
# Note you _must_not_ indent the actual shebang line, and it must be the
# first line of the file. Of course, CGI processing must be enabled by
# the appropriate ScriptAlias or Options ExecCGI directives for the files
# or directory in question.
#
# However, Apache on Windows allows either the Unix behavior above, or can
# use the Registry to match files by extention. The command to execute
# a file of this type is retrieved from the registry by the same method as
# the Windows Explorer would use to handle double-clicking on a file.
# These script actions can be configured from the Windows Explorer View menu,
# 'Folder Options', and reviewing the 'File Types' tab. Clicking the Edit
# button allows you to modify the Actions, of which Apache 1.3 attempts to
# perform the 'Open' Action, and failing that it will try the shebang line.
# This behavior is subject to change in Apache release 2.0.
#
# Each mechanism has it's own specific security weaknesses, from the means
# to run a program you didn't intend the website owner to invoke, and the
# best method is a matter of great debate.
#
# To enable the this Windows specific behavior (and therefore -disable- the
# equivilant Unix behavior), uncomment the following directive:
#
#ScriptInterpreterSource registry
#
# The directive above can be placed in individual <Directory> blocks or the
# .htaccess file, with either the 'registry' (Windows behavior) or 'script'
# (Unix behavior) option, and will override this server default option.
#
#
# DefaultType: the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value. If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig conf/mime.types
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have cer
tain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi
AddHandler cgi-script .pl
# For type maps (negotiated resources):
#AddHandler type-map var
#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
</IfModule>
#
# The mod_mime_magic module allows the server to use various hints from the
# contents of the file itself to determine its type. The MIMEMagicFile
# directive tells the module where the hint definitions are located.
#
#MIMEMagicFile conf/magic
#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402
#
#
# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall is used to deliver
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
#
#EnableMMAP off
#EnableSendfile off
# Supplemental configuration
#
# The configuration files in the conf/extra/ directory can be
# included to add extra features or to modify the default configuration of
# the server, or you may simply copy their contents here and change as
# necessary.
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf
# Multi-language error messages
#Include conf/extra/httpd-multilang-errordoc.conf
# Fancy directory listings
Include conf/extra/httpd-autoindex.conf
# Language settings
#Include conf/extra/httpd-languages.conf
# User home directories
#Include conf/extra/httpd-userdir.conf
# Real-time info on requests and configuration
#Include conf/extra/httpd-info.conf
# Virtual hosts
Include conf/extra/httpd-
vhosts.conf
# Local access to the Apache HTTP Server Manual
#Include conf/extra/httpd-manual.conf
# Distributed authoring and versioning (WebDAV)
#Include conf/extra/httpd-dav.conf
# Various default settings
Include conf/extra/httpd-default.conf
# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf
#Include conf/extra/httpd-vhosts-ssl.conf
#
# Note: The following must must be present to support
# starting without SSL on platforms with no /dev/random equivalent
# but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
#APMServ默认虚拟主机
NameVirtualHost *:80
<VirtualHost *:80>
ServerName *
DocumentRoot "D:/website/APMServ5.2.6/www/htdocs"
<Directory "D:/website/APMServ5.2.6/www/htdocs">
Options FollowSymLinks IncludesNOEXEC Indexes
DirectoryIndex default.php index.html index.htm default.htm index.php index.cgi default.cgi index.pl default.pl index.shtml
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
#phpMyAdmin配置信息
Alias /phpmyadmin "D:/website/APMServ5.2.6/www/phpMyAdmin"
<Directory "D:/website/APMServ5.2.6/www/phpMyAdmin">
AllowOverride None
Options Indexes FollowSymLinks Includes
Order allow,deny
Allow from all
</Directory>
#APMServ配置ASP支持
ProxyPass /asp http://127.0.0.1:10322
ProxyPassReverse /asp/ http://127.0.0.1:10322/
#APMServ配置由可视化界面自动创建的虚拟主机,如需手工创建虚拟主机,请不要将代码放在vhost.conf文件内。
Include conf/apmserv/vhost.conf
Include conf/apmserv/vhost_ssl.conf
#APMServ配置由可视化界面自动创建的虚拟目录,如需手工创建虚拟目录,请不要将代码放在vdir.conf文件内。
Include conf/apmserv/vdir.conf
四 : Apache常用配置
Apache的常用配置
Apache采用IBM HTTPServer,内核为Apache/2.0.47
Server version: IBM_HTTP_Server/6.1.0.13 Apache/2.0.47
查看IBM HTTPServer的版本的命令
apache.exe -V
./apachectl -V 4
注:IBM HTTPServer是包装Apache后增强了部分功能
普通Apache下的版本查看命令
./apachectl –v
若新配置Apache,建议安装最新版本的Apache
目前最新版本:2.2.26,可用Apache网站提供源码在linux下进行编译 最新window的安装包:2.2.25
注:Apache已经推出2.4的版本,对静态内容响应比2.2的版本快。
主要针对以下变更
1、 trace方法的漏洞
2、 定义了404和403错误显示页面,代替了原来的默认页面
3、 取消了apache的返回显示签名,取消错误页面时的版本显示。
4、 取消的目录内容列出的配置。
5、 Apache的内容输出压缩配置。
6、 Apache的status监控时默认地址的修改。
7、 Apache虚拟主机配置
8、 Apache默认的线程数的调整 含window和linux下
9、 Apache循环日志的设置
10、 Linux下Apache日志定时清除设置
11、 Apache的默认参数修改
12、 Apache的proxy功能启用
13、 Apache的缓存功能设置
在安装apache后,需要对默认配置进行修改,以增强一下默认的安全性配置。 每个详细的配置过程如下:
1、 针对Apache 2.0.55以前的版本,需要进行如下修改
放开rewrite模块
LoadModule rewrite_module modules/mod_rewrite.so
增加如下配置
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
若是Apache 2.0.55版本以上的apache,则需在http.conf中增加如下配置
TraceEnable Off
2、 定义404和403错误显示页面
在虚拟主机的配置处,增加如下配置,并将对应的错误页面拷贝到根目录下 ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
以覆盖默认的配置
3、 取消apache的版本返回显示签名
在http.conf中搜索如下配置,将
ServerSignature On 改为 ServerSignature Off
目前版本apache均是将配置分散到多个conf文件中,可修改
httpd-default.conf文件中的配置,注意在http.conf文件中将
#Include conf/extra/httpd-default.conf
修改为
Include conf/extra/httpd-default.conf,将#号取消
4、 取消目录内容列出配置
方法一,修改 httpd.conf配置文件,查找 Options Indexes FollowSymLinks,修改为 Options -Indexes;
方法二,在www 目录下的修改.htaccess 配置文件,加入 Options -Indexes。 (推荐)
apache的配置是在虚拟主机的配置中如下配置
<Directory "/xxx/www">
Options – AllowOverride None
Order allow,deny
Allow from all
</Directory>
注意将FollowSymLinks清除掉,并将Indexs改为 -Indexs
5、 Apache的内容输出压缩配置
按如下配置进行页面压缩配置
LoadModule deflate_module modules/mod_deflate.so
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase
Request_URI .(?:gif|jpe?g|png|bmp|swf|cab)$ no-gzip dont-vary
SetEnvIfNoCase
Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase
Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
# SetEnvIfNoCase Request_URI
/.(gif|jpg|cab|jpe?g|exe|bmp|mp3|rar|zip|swf|png)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain
text/xml text/css text/javascript
AddOutputFilterByType DEFLATE application/ms*
application/vnd* application/postscript application/javascript
application/x-javascript
SetEnvIf User-Agent ^Mozilla/4 gzip-only-text/html
SetEnvIf User-Agent ^Mozilla/4/.0[678] no-gzip
SetEnvIf User-Agent \bMSIE !no-gzip
SetEnvIf User-Agent \bMSIE !gzip-only-text/html
# SetEnvIfNoCase Request_URI
/.(gif|jpg|cab|jpe?g|exe|bmp|mp3|rar|zip|swf|png)$ no-gzip dont-vary
DeflateCompressionLevel 9
</IfModule>
6、 Apache的status监控时默认地址的修改
Apache中的默认设置为
<Location /server-status>
SetHandler server-status
Order deny,allow
# Deny from all
Allow from .localhost
</Location>
此种设置不安全,如果启用status监控需要修改默认地址,若启动虚拟主机,则在每个虚拟主机节点中进行配置,方有效,即在
<VirtualHost *:80>
<Location /test-server-status>
SetHandler server-status
Order deny,allow
# Deny from all
Allow from .localhost
</Location>
</VirtualHost >
之间配置
若无虚拟主机则正常配置
<Location /test-server-status>
SetHandler server-status
Order deny,allow
# Deny from all
Allow from .localhost
</Location>
将server-status的默认值需要修改一下,避免此处的安全漏洞 注:若要启动扩展状态监控
即 配置
ExtendedStatus On
注意:
ExtendedStatus On不能配置在单独的虚拟主机节点内,需要在httpd.conf中配置
可参考: httpd-info.conf文件中的配置
7、 Apache的虚拟主机配置
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.61k.com
ServerAlias www.test.edu.cn
DocumentRoot /test/www
AddDefaultCharset Off
<Directory "/test/www">
Options -Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
</VirtualHost>
虚拟主机按以上进行配置,对于新版本的apache注意将
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
处进行修改
Include conf/extra/httpd-vhosts.conf
在httpd-vhosts.conf文件中进行虚拟主机的配置
8、 Apache默认线程数调整
对2.2版本的apache,在httpd.conf文件中将
# Include conf/extra/ httpd-mpm.conf修改为
Include conf/extra/ httpd-mpm.conf
并在此文件中寻找到此处
# WinNT MPM
# ThreadsPerChild: constant number of worker threads in the server process # MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_winnt_module>
ThreadLimit 2000
ThreadsPerChild 2000
MaxRequestsPerChild 100
</IfModule>
此处是将线程的限制修改到2000,注意window修改2000的限制在window下会报一些错误,因为window下默认达不到2000,一般只能到1912左右,要想突破2000的限制,需要使用ThreadLimit指令
Linux下需要对以下模块设置,linux下Apache默认工作模式是prefork
可通过 ./apachectl –l来查看apache的工作模式
Window下通过 httpd.exe –l来查看
注:需要在apache的安装目录下的bin目录中
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves <IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
Linux下若调整线程数则调整以上数字,重点调整MaxClients 配置样例,需要根据机器的配置进行调整:
<IfModule mpm_prefork_module>
StartServers 10
MinSpareServers 10
MaxSpareServers 20
MaxClients 1500
MaxRequestsPerChild 10000
</IfModule>
9、 Apache循环日志配置
Linux下的配置样例:
/usr/local/apache 为apache的安装目录,根据实际情况修改
ErrorLog "| /usr/local/apache/bin/rotatelogs
/usr/local/apache/logs/%Y_%m_%d_test-error.log 86400 480"
CustomLog "logs/test-access.log" common
Window下配置
ErrorLog "| bin/rotatelogs.exe logs/%Y_%m_%d_test-error.log 86400 480"
10、 Linux下Apche日志定时清除
按以下配置即可
1、root用户登录,检查crond服务的运行情况
[root@CentOS-APP2]# service crond status
crond (pid 3698) 正在运行...
若没运行,则
service crond start
2、在/usr/local/testweb下编写执行脚本
vi clean-log
拷贝如下脚本:
#! /bin/sh
#cd $(dirname $0) || exit 1
logdir=/usr/local/apache/logs/test-com/
cd ${logdir}
declare -i filesum=`ls test_access* |wc -l`
declare -i delnum=$filesum-30
#echo ${delnum}
if [ "${delnum}" -ge 1 ]; then
rm -rf `ls -tr test_access* | head -${delnum}`
fi
declare -i filesumerr=`ls test_error* |wc -l`
declare -i delnumerr=$filesumerr-30
if [ "${delnumerr}" -ge 1 ]; then
rm -rf `ls -tr test_error* | head -${delnumerr}`
fi
拷贝脚本时,请注意日志目录的实际位置和要清理的日志文件的命名格式,需要保持一致
这里的日志目录:
/usr/local/apache/logs/test-com/
要清理的日志格式:
test_access_20131223.log
test_error_20131223.log
设定要清理的日志数量,设定为30,超过30个日志文件则清理
3、设定运行权限
cd /usr/local/testweb
chmod 755 clean-log
4、设定定时运行
设定为每月30日晚1点30分定时执行
30 1 30 * * /usr/local/testweb/clean-log
/usr/local/testweb/clean-log 为编写好的日志清除脚本
设定方法:
root用户登录后执行:
crontab -e
打开一个文件,用vi的方式进行编辑
输入
30 1 30 * * /usr/local/testweb/clean-log
然后:wq退出保存
可在/var/spool/cron/目录下看到此文件
root
与所设定的用户名一致
5、执行
service crond reload 重新加载配置运行
6、测试
测试时可以执行
crontab -e 编辑设定的文件,增加如下配置,每分钟定时,可先修改脚本,将日志文件数减少,如将30变为3
*/1 * * * * /usr/local/testweb/clean-log
可以在/usr/local/apache/logs/test-com/目录下用vi命令按
test_access_20131202.log的文件命名格式来建若干个空文件,以测试是否可定时删除文件
可以tail -n 100 /usr/spool/mail/root 查看错误
或
tail -f /var/log/cron 查看运行日志
11、 Apache默认参数修改
修改此文件
Include conf/extra/httpd-default.conf
作如下修改:
? ServerTokens Full 修改为 ServerTokens Prod
? Timeout 300 可修改为 Timeout 100
? MaxKeepAliveRequests 100 可根据机器配置适当加大
12、 Apache的proxy功能启用
启用代理 需要在httpd.conf文件放开以下两个模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
然后启用虚拟主机,在此句之后
NameVirtualHost *:80
增加如下:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://172.16.0.2:9090/portal/
ProxyPassReverse / http://172.16.0.2:9090/portal/
ServerName test.xxx.cn
ServerAlias test.xxx.edu.cn
</VirtualHost>
13、 Apache的缓存功能设置
Apache的缓存功能需要开启以下模块
#LoadModule cache_module modules/mod_cache.so
#LoadModule mem_cache_module modules/mod_mem_cache.so 缓存有两种:1、内存中缓存;2、磁盘缓存
此处讲解内存中缓存
配置样例:
<IfModule mod_cache.c>
<IfModule mod_mem_cache.c>
CacheEnable mem /image #要缓存目录
CacheEnable mem /images
CacheEnable mem /js
CacheEnable mem /style
CacheEnable mem /css
CacheEnable mem /script
CacheEnable mem /swf
CacheEnable mem /file
MCacheMaxObjectCount 20000 #缓存对象数量
MCacheMaxObjectSize 1048576 #缓存对象最大大小 字节 MCacheMaxStreamingBuffer 65536
MCacheMinObjectSize 10 #缓存对象最小大小 字节 MCacheRemovalAlgorithm LRU #缓存的算法
MCacheSize 2097152 #缓存的大小 kb
CacheMaxExpire 14400 #缓存最大过期时间 CacheDefaultExpire 14400
</IfModule>
</IfModule>
配置说明:
1,CacheEnable mem /images 缓存images下面的内容,这里的 mem只是一个缓存类型,指示mod_cache使用内存的存储管理器通过实施
mod_mem_cache 。缓存类型disk指示mod_cache使用基于磁盘的存储管理
的实施mod_disk_cache 。缓存类型,fd指示mod_cache使用文件描述符缓存实施mod_mem_cache
2,MCacheSize最大内存使用量,插入在缓存和对象大小的对象是大于剩余内存,将被删除,直到新的对象可以被缓存。 被删除的对象选择使用指定的算法MCacheRemovalAlgorithm
3,MCacheRemovalAlgorithm缓存算法:
LRU (最近最少使用)
LRU删除文件,没有时间最长的被访问的。
GDSF(GreadyDual尺寸)
GDSF分配一个优先的文件缓存文件的费用的基础上,缓存大小。以最低的优先权文件被删除第一次。
4,MCacheMaxObjectCount最大缓存对像个数
5,MCacheMaxObjectCount最大缓存对像字节数
6,MCacheMinObjectSize最小缓存对像字节数
7,CacheMaxExpire 最大缓存结止时间
8,CacheDefaultExpire 默认缓存结止时间
9,CacheDisable /php 不缓存php下面的内容
本文标题:apache配置-Apache配置多个站点61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1