哪一天 哪一天 我有吃有穿有住有钱 不再流浪 流浪
标签类目:linux

转: Linux下Apache基本配置指南

1、如果找不到文件的具体位置,在Linux下可以用类似locate httpd.conf的指令来搜索文件的位置。如果搜索不到,可以先用updatedb指令更新索引数据库再用locate搜索。

2、apachectl configtest或apachectl –t检查配置文件是否合法。apachectl一般位于安装目录的bin目录下(如:/usr/local/apache2/bin)。不要直接调用httpd。

3、配置文件是httpd.conf;在Linux系统中,它可能存在于系统配置目录(如:/etc/httpd/conf/),也可能存在于Apache的安装目录(如:/usr/local/apache2/conf)。

4、配置文件中,一行包含一个指令,但行尾可以用表示续行。与下一行之间不能有其它任何字符,包括空白字符。
#表示这一行是注释。

5、指令对大小写不敏感,但是参数对大小定敏感,在Linux系统下,路径也要注意大小写。路径后不必加/。

6、类似于<Directory></Directory>表示一个配置段。大多数配置段中的指令仅针对配置段所匹配的请求有 效。但诸如        <IfDefine>、<IfModule>、<IfVersion>之类,是在Apache启动时,如果条 件成立才有效,并且对所有请求都有效。

httpd.conf的基本配置选项

7、ServerName服务器名称。它用来创建URL的重导向。ServerName是apache服务器自身识别访问请求的标记之一,他不必与实际IP或DNS名称一致。也可以不设置,如果这样,那apache会试图用IP来作为请求的标记。端口也可以不设置。
例:ServerName www.example.com:80

8、ServerAdmin管理员的电子邮件地址。服务器的错误提示页会用到。如果ServerSignature定义为Email的话,将在错误页的页尾增加ServerAdmin的链接。

9、Listen服务器监听的地址和端口。端口一定要指定。默认情况下,服务器会监听本机的所有地址。可以同时使用多个Listen指令。
例一:同时所有接受来自端口80和8000的请求
Listen 80
Listen 8080
例二:指定地址+端口,配置虚拟主机时,会需要这样设置。详细看虚拟主机的设置。这并不是必要的。
Listen 192.168.0.2:80
Listen 192.168.0.1:8080
注意地址是本机的地址,是指客户端对本机某个地址的请求。地址可以是域名,但最好是IP地址。
例三:IPv6地址必须用方括号括起来。
Listen [2001:db8::a00:20ff:fea7:ccea]:80
例四:要使Apache只处理IPv4的请求,只需如此:
Listen 0.0.0.0:80

10、ServerRoot 服务器基础目录,一般就是Apache的安装目录,不必更改。

11、DocumentRoot 指定主目录。不指定的话,默认目录一般是ServerRoot目录下的htdocs目录(如/usr/local/apache2/htdocs),视版 本而定;但是可能会有例外,所以最好指定。如果指定相对路径,则认为是相对于ServerRoot目录的。目录后不要加/。

12、DirectoryIndex 默认首页名称。多个默认页名称用空格隔开。

13、ErrorDocument 处理请求出错时的处理方式。未配置时只返回错误代码。
例:
ErrorDocument 500 "The server made a boo boo."
# 指定本地URL时,该URL是相对于DocumentRoot目录的。
ErrorDocument 404 /missing.html
ErrorDocument 404 "/cgi-bin/missing_handler.pl"
# 使用绝对URL时,客户机将无法收到错误码。
ErrorDocument 402 http://www.example.com/subscription_info.html

14、ErrorLog,CustomLog 指定错误日志和访问日志。如果指定路径是相对路径,则认为是相对ServerRoot目录的。日志文件可能会很大,以至影响到其它文件的储存空间,所以有必要把日志文件放到一个单独的分区。
例一:
ErrorLog /var/log/error_log
# commom是日记文件的格式,由LogFormat定义。不可用于ErrorLog。
Customlog /var/log/access_log common
例二:管道日志,可以用Apache提供的rotatelogs来实现。rotatelogs程序一般位于安装目录的bin目录。
# 这将每24小时建立日志文件/var/log/logfile.nnnn,nnnn是日记建立时的系统时间。
CustomLog “|bin/rotatelogs /var/log/logfile 86400”common
# 日志文件达到5M时建立新日记,文件名类似于logfile.2006-12-30-24_33_12。
ErrorLog “|bin/rotatelogs /var/log/logfile.%Y-%m-%d-%H_%M_%S 5M”

15、User, Group 指定运行服务子进程的用户和组。Rpm包安装apache时会自动设置一个用户和组,但有时会设成nobody或者不设置。为了安全和方便管理,设置为用户和组为apache是很重要的。
例:User apache
Group apache
注意在Linux系统中手动添加apache用户和组时,必须把他们的shell指定为nologin

虚拟主机

16、虚拟主机通过<VirtualHost>配置段来配置,配置段里的指令对虚拟主机有效,配置段没有配置的,将采用全局的配置。检查虚拟主机的配置可用apachectl –S(可能某些版本这个参数无效)。

17、基于域名的虚拟主机在DNS把多个域名都映射到同一IP的情况下有用。典型的配置如下:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@test.com
DocumentRoot /www/docs/test.com
ServerName test.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/docs/test2.com
ServerName test2.com
</VirtualHost>

NameVirtualHost *:81
<VirtualHost *:81>
DocumentRoot /www/docs/test3.com
ServerName test3.com
</VirtualHost>
注意一:NameVirtualHost 指定虚拟主机所使用的IP地址或域名,但是最好是IP地址。使用基于域名的虚拟主机时,NameVirtualHost是必要的指令。NameVirtualHost可以定义多个。
注意二:所有符合NameVirtualHost或<VirtualHost>标签定义的请求,都会被作为虚拟主机处理,而主服务器将不理 会。NameVirtualHost定义了而<VirtualHost>标签没有定义的的请求,服务器会找不到相应的虚拟主机而将无法处理。 所以每个NameVirtualHost定义的参数至少要有一个<VirtualHost>相匹配。
注意三:如果设置NameVirtualHost 或<VirtualHost>为*:80的话,所有针对80端口的请求,都会被虚拟主机处理,请求会根据域名指向某个虚拟主机。如果有来自 80端口的请求,而所请求的域名没有被配置为虚拟主机,那将指向第一个虚拟主机。这样主服务器将无法收到来自80端口的任何请求。为此也要为主服务器配置 一个虚拟主机。

18、ServerAlias 虚拟主机的别名
例:
NameVirtualHost *:80
<VirtualHost *:80>
       ServerName www.domain.tld
       ServerAlias domain.tld *.domain.tld
       DocumentRoot /www/domain
</VirtualHost>
这表示对 domain.tld和*.domain.tld的请求也由虚拟主机www.domain.tld处理。

19、ServerPath指令是用于让某些老式浏览器也访问基于域名的虚拟主机的,一般不必设置。

20、基于IP地址的虚拟主机。例:
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example.com
</VirtualHost>
<VirtualHost 172.20.30.50 192.168.0.10:80>
DocumentRoot /www/example2
ServerName www.example.org
</VirtualHost>
每个虚拟主机可定义多个IP,之间用空格隔开。

21、各种虚拟主机的混用。例:
Listen 80
Listen 81

NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example.com
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example.org
</VirtualHost>

NameVirtualHost 172.20.30.40:81
<VirtualHost 172.20.30.40:81>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>
<VirtualHost 172.20.30.60:81 172.20.30.40:81>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost>

22、虚拟主机混用时的问题:
一、虚拟主机混用可以这样理解:一行NameVirtualHost指令定义的所有虚拟主机为一组;该组与一个基于IP的虚拟主机平级。即把一行NameVirtualHost定义的整个组看作是一个基于IP的虚拟主机。
二、虚拟主机指定的端口必须是Listen定义的。如果虚拟主机没有指定端口,则认为是80端口。如果NameVirtualHost * 这样定义,是指所有地址的所有已定义端口。
三、更具体的地址定义优先。比如NameVirtualHost指令定义了*:80,而某个基于IP的虚拟主机定义为192.168.0.1:80,那么 此时如有对192.168.0.1:80的请求,那请求会被优先指向192.168.0.1:80定义的虚拟主机。所以为了避免混乱,不要定义相互有交叉 或包含的地址区间。
四、一个虚拟主机,可以同时为基于域名和基于IP的。如上一例中最后一个虚拟主机。这样符合两种定义的请求都会被指同一个虚拟主机。有时要区别内外网对虚拟主机的访问时可以这样,因为来自内网的请求可能和来自外网的请求可能不一样,但是它们需要指向同一个虚拟主机。

23、使用"_default_"虚拟主机,这个虚拟主机可以理解成基于IP的虚拟主机。例:
<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>
这个虚拟主机将接管与其它虚拟主机IP和端口不匹配的请求。不过如此一来,主服务器将不会处理任何请求。因此把主服务器配置成一个虚拟主机是必要的。

24、本地机器代理在其它机器上运行的虚拟主机。例:
<VirtualHost 158.29.33.248>
ProxyPreserveHost On
ProxyPass /foo/no !
ProxyPass /foo http://192.168.111.2
ProxyPassReverse /foo http://192.168.111.2
ServerName hostname.example.com
</VirtualHost>
一、首先这是一个基于IP的虚拟主机,它接收并处理对IP地址158.29.33.248的请求。
二、ProxyPass /foo http://192.168.111.2 将把对http://158.29.33.248/foo的请求转换为一个代理请求,该请求指向http://192.168.111.2
三、ProxyPass /foo/no ! 不代理针对/foo/no的请求。这个必须放在正常代理指令之前。
四、ProxyPreserveHost On 意思是传送原始请求的Host信息给被代理的机器。
五、ProxyPassReverse /foo http://192.168.111.2 可以保证请求URL在其它机器上被重定向后,本机处理时也可以保持一致。具体看手册关于反向代理的部分。
六、基于域名的虚拟主机也是同样的道理。不管是什么类型的虚拟主机,它只是处理归它处理的请求而已。

配置段

25、<IfDefine> 只有在用httpd命令行启动服务器时(最好不要直接使用httpd,用apachectl代替),使用了-D参数定义了相应参数时才生效。如服务器用 apachectl –D test启动时,<IfDefine test>配置段生效。

26、<IfVersion> 例如:<IfVersion >= 2.0.55> 当Apache版本不低于2.0.55时生效。

27、<IfModule> 服务器启用了指定的模块后才生效。这是最常用的。例如<IfModule mod_mine_magic.c>。

28、<Directory> 用于封装一组指令,使之对某个目录和下属的子目录有效。
例:
<Directory />
       Options FollowSymLinks Indexes
       AllowOverride None
       Order Deny,Allow
       Deny from All
       Allow from example.com
</Directory>
该配置段对整个根目录树有效。
一、Options 常用选项:FollowSymlinks允许在此目录中使用符号链接;Indexes允许目录列表,即在该目录没有默认页时服务器返回该目录的列表给客户 机;SymLinksIfOwnerMatch只有符号链接与其目的目录或文件属于同一用户时才有效。
注意:对同一目录,只有一行Options有效,如果定义某个目录的Options同时要继承上级目录的定义,可以这样:Options +Indexes。如果这样:Options +Indexes –FollowSymLinks,这将为本级目录增加Indexes,取消FollowSymLinks。
二、AllowOverride 常用选项:None 不允许使用.htaccess;All允许在.htaccess中使用所有的指令。一般不必使用.htaccess,而且为了安全和效率起见,设置为None比较好。
三、Order 访问控制,控制条件由Deny行和Allow行定义。Order指令常用选项:Deny,Allow 除了符合条件的外,其它的也允许访问;Allow,Deny 除了符合条件的外,其它的不允许访问。
Deny from All是拒绝所有的访问,Allow from example.com是允许example.com域访问该目录(意思是如果该服务器上有多个虚拟主机的话,只有example.com可以访问该目 录)。三行合起来的意思就是只允许example.com域访问根目录。当然这只是个例子,应该禁止所有域对根目录的访问。注意:Deny,Allow指 令生效的顺序取决于Order中Deny和Allow的顺序。
注意<Directory>不能嵌套。
这样为了安全起见常常需要设置:
#拒绝对所有目录的访问,注意这里的/是指操作系统的根目录,而非DocumentRoot目录。
<Directory />
       Options –Indexes -FollowSymLinks
       AllowOverride None
       Order Allow,Deny
</Directory>
#允许所有对/var/htdocs的访问,允许对/var/htdocs的文件列表。
<Directory /var/htdocs>
       Options +Indexes
       Order Deny,Allow
</Directory>

29、<Files>和<Directory>类似,不过它定义的是对文件的访问控制。它们都可以接受正则表达式为参数,格式 如<Files ~ ".(gif|jpe?g|png)$">或者<FilesMatch ".(gif|jpe?g|png)$">。

30、<Location>与<Files>和<Directory>同,不过它定义的是对URL的访问控制。

PHP配置

31、加载php模块:LoadModule php5_module modules/libphp5.so

32、AddModule mod_php5.c (不是必须的)

33、哪种后缀的文件作为php脚本来解析:AddType application/x-httpd-php .php (这是必须的,但是可以用下面的配置代替):
<Files *.php>
SetOutputFilter PHP
SetInputFilter PHP
</Files>
这种方法还可以为.php文件专门设置更多的配置。

34、哪种后缀的文件是php源文件:Addtype application/x-httpd-php-source .phps (不是必须的)

35、添加index.php为目录首页:DirectoryIndex index.php(视情况而定)

36、ScriptAlias /php/ /usr/local/php/ 对类似http://example/php/abc.php的请求将引导执行/usr/local/php/abc.php脚本。 (一些所谓配置指南里有,事实上完全没有这个必要。并且ScriptAlias这个指令是针对CGI脚本的。他会把php脚本也当作已定义的cgi脚本处理)。

37、Action application/x-httpd-php "C:/PHP/php.exe" 所有application/x-httpd-php类型的文件都由C:/PHP/php.exe来处理,注意application/x-httpd- php必须是已经定义的文件类型。 (只有在windows中以CGI模式安装PHP时才有用)。

38、事实上,必要的配置只有这么两条:
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
可以把相关php的配置语句都放在一起便于管理。简单至此,不要被一些配置指南吓住了。

Virtual Hosting using Apache 2 on a linux machine

Virtual Hosting allow web servers to host more than one website on a sing machine. This is how sharing hosting works. I become pretty handy as well while develloping different web project on the same machine and allows you to access to your local repository using addresses such as http://dev.mysite.com instead of http://localhost/~myuser/myproject/ :) .
This tutorial is based on a machine runnning ubuntu/linux but should be the same on any debian based distribution and almost the same on other distributions.

First of all, you need an apache server ready to run on your machine, if it is not yet install, open a terminal and type

$sudo apt-get install apache2-utils apache2-common

Once the server is installed, it is time to get into apache 2 configuration.
Let’s open apache’s main configuration file, name /etc/apache2/apache2.conf. A search for the word virtual bring us to the following line:

Include /etc/apache2/sites-enabled/[^.#]*

This mean that when starting apache, it will look for files in /etc/apache2/sites-enabled/.
Lets go there and see what is in.

$cd /etc/apache2/sites-enabled/

$ls -l

total 1

lrwxrwxrwx 1 root root 36 2005-12-27 01:42 000-default -> /etc/apache2/sites-available/default

Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:

  1. Have a simple main configuration file
  2. Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/
  3. In case your web server doesn’t restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/

Now let say you want to be able to map the domain name dev.example.com to you local machine, using the code file in /home/myuser/public_html/example.com/.
While in /etc/apache2/sites-available, create a new file (let say example.com.conf)

$sudo vi example.com.conf

Now add the following lines:

<VirtualHost dev.example.com>
ServerAdmin webmaster@localhost
#We want to be able to access the web site using www.dev.example.com or dev.example.com
ServerAlias www.dev.example.com
DocumentRoot /home/myuser/public_html/example.com
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>

Note:People who don’t want to bother knowing how the site enabling system works might just jump to the end of the article to find debian built-in command syntax. If you want to know how it works, or do not use a debian based distro, carry on.

Now, we specified a new host to apache but it is not yet linked to the repertory where apache actually look for virtual hosts. Let go to:

$cd /etc/apache2/sites-enabled/

and create a link to the file we just created:

$sudo ln -s /etc/apache2/sites-available/example.com.conf example.com.conf

Now apache is almost ready to restart, but before doing so, we must inform our linux system that dev.example.com and www.dev.example.com are not to be looked for on the net, but on the local machine instead.
To do so, simply edit /etc/hosts and add the new host names at the end of the line beginning by 127.0.0.1, which is localhost.
In the end, your file should look like:

127.0.0.1 localhost.localdomain localhost dev.example.com www.dev.example.com

And now we are done, simply reload apache:

sudo /etc/init.d/apache2 reload

Open your web browser and enter the following address dev.example.com. Magic, it runs the same as when you were using http://localhost/~myuser/example.com but it is far more usefull when devellopping a web service and want to be able to develop applications on your machine just like it is where the real web site.

Edit: As you can see from the comments, many people pointed out that you can use a debian specific command (so if you are not using a debian based system, don’t expect to find that command :) ).
to enable a new virtual host simply type:

sudo a2ensite mysiteavailable-site

to disable a virtual host:

sudo a2dissite mysiteavailable-site

where mysiteavailable-site is the name of the virtual hos you want to enable/disable, so in out example: example.com.conf

返回顶部