Apache的配置

Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改。

  1. 主站点的配置(基本配置)

    (1) 基本配置:
    ServerRoot "/mnt/software/apache2" #你的apache软件安装的位置。其它指定的目录如果没有指定绝对路径,则目录是相对于该目录。

    PidFile logs/httpd.pid #第一个httpd进程(所有其他进程的父进程)的进程号文件位置。

    Listen 80 #服务器监听的端口号。

    ServerName www.clusting.com:80 #主站点名称(网站的主机名)。

    ServerAdmin admin@clusting.com #管理员的邮件地址。

    DocumentRoot "/mnt/web/clusting" #主站点的网页存储位置。

    以下是对主站点的目录进行访问控制:

    <Directory "/mnt/web/clusting">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>

    在上面这段目录属性配置中,主要有下面的选项:

    DirectoryIndex index.html index.htm index.php  #主页文件的设置(本例将主页文件设置为:index.html,index.htm和index.php)

    (2) 服务器的优化 (MPM: Multi-Processing Modules)
    apache2主要的优势就是对多处理器的支持更好,在编译时同过使用--with-mpm选项来决定apache2的工作模式。如果知道当前的apache2使用什么工作机制,可以通过httpd -l命令列出apache的所有模块,就可以知道其工作方式:

    (3) HTTP返头回信息配置:

    ServerTokens Prod #该参数设置http头部返回的apache版本信息,可用的值和含义如下:

    ServerSignature Off #在页面产生错误时是否出现服务器版本信息。推荐设置为Off

    (4) 持久性连接设置

    KeepAlive On #开启持久性连接功能。即当客户端连接到服务器,下载完数据后仍然保持连接状态。

    MaxKeepAliveRequests 100 #一个连接服务的最多请求次数。

    KeepAliveTimeout 30 #持续连接多长时间,该连接没有再请求数据,则断开该连接。缺省为15秒。

  2. 别名设置

    对于不在DocumentRoot指定的目录内的页面,既可以使用符号连接,也可以使用别名。别名的设置如下:

    Alias /download/ "/var/www/download/" #访问时可以输入:http://www.custing.com/download/

    <Directory "/var/www/download"> #对该目录进行访问控制设置
    Options Indexes MultiViews
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
    </Directory>

  3. CGI设置

    ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/" # 访问时可以:http://www.clusting.com/cgi-bin/ 。但是该目录下的CGI脚本文件要加可执行权限!

    <Directory "/usr/local/apache2/cgi-bin"> #设置目录属性
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    </Directory>

  4. 个人主页的设置 (public_html)
  5. 日志的设置

    (1)错误日志的设置
    ErrorLog logs/error_log #日志的保存位置
    LogLevel warn #日志的级别

    显示的格式日下:
    [Mon Oct 10 15:54:29 2005] [error] [client 192.168.10.22] access to /download/ failed, reason: user admin not allowed access

  6. (2)访问日志设置

    日志的缺省格式有如下几种:
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common #common为日志格式名称
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent
    CustomLog logs/access_log common

    格式中的各个参数如下:

    下面是一段访问日志的实例:
    192.168.10.22 - bearzhang [10/Oct/2005:16:53:06 +0800] "GET /download/ HTTP/1.1" 200 1228
    192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/blank.gif HTTP/1.1" 304 -
    192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/back.gif HTTP/1.1" 304 -

    各参数的详细解释,请参阅:http://www.clusting.com/Apache/ApacheManual/logs.html

  7. 用户认证的配置
    (1)in the httpd.conf:
    AccessFileName .htaccess
    .........
    Alias /download/ "/var/www/download/"
    <Directory "/var/www/download">
    Options Indexes
    AllowOverride AuthConfig
    </Directory>
  8. (2) create a password file:
    /usr/local/apache2/bin/htpasswd -c /var/httpuser/passwords bearzhang

    (3)onfigure the server to request a password and tell the server which users are allowed access.
    vi /var/www/download/.htaccess:
    AuthType Basic
    AuthName "Restricted Files"
    AuthUserFile /var/httpuser/passwords
    Require user bearzhang
    #Require valid-user #all valid user

  9. 虚拟主机的配置
    (1)基于IP地址的虚拟主机配置
    Listen 80
    <VirtualHost 172.20.30.40>
    DocumentRoot /www/example1
    ServerName www.example1.com
    </VirtualHost>
  10. <VirtualHost 172.20.30.50>
    DocumentRoot /www/example2
    ServerName www.example2.org
    </VirtualHost>

    (2) 基于IP和多端口的虚拟主机配置
    Listen 172.20.30.40:80
    Listen 172.20.30.40:8080
    Listen 172.20.30.50:80
    Listen 172.20.30.50:8080

    <VirtualHost 172.20.30.40:80>
    DocumentRoot /www/example1-80
    ServerName www.example1.com
    </VirtualHost>

    <VirtualHost 172.20.30.40:8080>
    DocumentRoot /www/example1-8080
    ServerName www.example1.com
    </VirtualHost>

    <VirtualHost 172.20.30.50:80>
    DocumentRoot /www/example2-80
    ServerName www.example1.org
    </VirtualHost>

    <VirtualHost 172.20.30.50:8080>
    DocumentRoot /www/example2-8080
    ServerName www.example2.org
    </VirtualHost>

    (3)单个IP地址的服务器上基于域名的虚拟主机配置:
    # Ensure that Apache listens on port 80
    Listen 80

    # Listen for virtual host requests on all IP addresses
    NameVirtualHost *:80

    <VirtualHost *:80>
    DocumentRoot /www/example1
    ServerName www.example1.com
    ServerAlias example1.com. *.example1.com
    # Other directives here
    </VirtualHost>

    <VirtualHost *:80>
    DocumentRoot /www/example2
    ServerName www.example2.org
    # Other directives here
    </VirtualHost>

    (4)在多个IP地址的服务器上配置基于域名的虚拟主机:
    Listen 80

    # This is the "main" server running on 172.20.30.40
    ServerName server.domain.com
    DocumentRoot /www/mainserver

    # This is the other address
    NameVirtualHost 172.20.30.50

    <VirtualHost 172.20.30.50>
    DocumentRoot /www/example1
    ServerName www.example1.com
    # Other directives here ...
    </VirtualHost>

    <VirtualHost 172.20.30.50>
    DocumentRoot /www/example2
    ServerName www.example2.org
    # Other directives here ...
    </VirtualHost>

    (5)在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机):
    Listen 80
    Listen 8080

    NameVirtualHost 172.20.30.40:80
    NameVirtualHost 172.20.30.40:8080

    <VirtualHost 172.20.30.40:80>
    ServerName www.example1.com
    DocumentRoot /www/domain-80
    </VirtualHost>

    <VirtualHost 172.20.30.40:8080>
    ServerName www.example1.com
    DocumentRoot /www/domain-8080
    </VirtualHost>

    <VirtualHost 172.20.30.40:80>
    ServerName www.example2.org
    DocumentRoot /www/otherdomain-80
    </VirtualHost>

    <VirtualHost 172.20.30.40:8080>
    ServerName www.example2.org
    DocumentRoot /www/otherdomain-8080
    </VirtualHost>

    (6)基于域名和基于IP的混合虚拟主机的配置:
    Listen 80

    NameVirtualHost 172.20.30.40

    <VirtualHost 172.20.30.40>
    DocumentRoot /www/example1
    ServerName www.example1.com
    </VirtualHost>

    <VirtualHost 172.20.30.40>
    DocumentRoot /www/example2
    ServerName www.example2.org
    </VirtualHost>

    <VirtualHost 172.20.30.40>
    DocumentRoot /www/example3
    ServerName www.example3.net
    </VirtualHost>

     

  11. SSL加密的配置

    首先在配置之前先来了解一些基本概念:

    (1) conf/ssl.conf 配置文件中的主要参数配置如下:

    Listen 443
    SSLPassPhraseDialog buildin
    #SSLPassPhraseDialog exec:/path/to/program
    SSLSessionCache dbm:/usr/local/apache2/logs/ssl_scache
    SSLSessionCacheTimeout 300
    SSLMutex file:/usr/local/apache2/logs/ssl_mutex

    <VirtualHost _default_:443>

    # General setup for the virtual host
    DocumentRoot "/usr/local/apache2/htdocs"
    ServerName www.example.com:443
    ServerAdmin you@example.com
    ErrorLog /usr/local/apache2/logs/error_log
    TransferLog /usr/local/apache2/logs/access_log

    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

    SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt
    SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key
    CustomLog /usr/local/apache2/logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

    </VirtualHost>

    (2) 创建和使用自签署的证书:
    a.Create a RSA private key for your Apache server
    /usr/local/openssl/bin/openssl genrsa -des3 -out /usr/local/apache2/conf/ssl.key/server.key 1024

    b. Create a Certificate Signing Request (CSR)
    /usr/local/openssl/bin/openssl req -new -key /usr/local/apache2/conf/ssl.key/server.key -out /usr/local/apache2/conf/ssl.key/server.csr

    c. Create a self-signed CA Certificate (X509 structure) with the RSA key of the CA
    /usr/local/openssl/bin/openssl req -x509 -days 365 -key /usr/local/apache2/conf/ssl.key/server.key -in /usr/local/apache2/conf/ssl.key/server.csr -out /usr/local/apache2/conf/ssl.crt/server.crt

    /usr/local/openssl/bin/openssl genrsa 1024 -out server.key
    /usr/local/openssl/bin/openssl req -new -key server.key -out server.csr
    /usr/local/openssl/bin/openssl req -x509 -days 365 -key server.key -in server.csr -out server.crt

    (3) 创建自己的CA(认证证书),并使用该CA来签署服务器的证书。
    mkdir /CA
    cd /CA
    cp openssl-0.9.7g/apps/CA.sh /CA
    ./CA.sh -newca
    openssl genrsa -des3 -out server.key 1024
    openssl req -new -key server.key -out server.csr
    cp server.csr newreq.pem
    ./CA.sh -sign
    cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.crt
    cp server.key /usr/local/apache2/conf/ssl.key/

 


Copyright© 2005 Clusting.com All Rights Reserved

联系: