3.2  Apache的安装、配置

 

3.2.1  Apache的安装

$ sudo apt-get install apache2

3.2.2  Apache的配置

2Apache模块

查看可用的模块(默认存储在/etc/apache2/mods-available)

$ sudo a2enmod

Which module would you like to enable?

Your choices are: actions alias asis auth_basic auth_digest authn_alias authn_anon authn_dbd authn_dbm authn_default authn_file authnz_ldap authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cern_meta cgid c gi charset_lite dav_fs dav dav_lock dbd deflate dir disk_cache dump_io env expires ext_filter file_cache filter headers ident p_w_picpathmap include info ldap log_forensic mem_cache mime mime_magic negotiation php5 proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http proxy rewrite setenvif speling ssl status substitute suexec u nique_id userdir usertrack version vhost_alias

Module name?

查看已经安装的模块(默认存储在/etc/apache2/mods-enabled)

$ sudo a2dismod

Which module would you like to disable?

Your choices are: alias auth_basic authn_file authz_default authz_groupfile authz_ host authz_user autoindex cgi dir env mime negotiation php5 rewrite setenvif status

Module name?

 

网站的默认存放位置在/var/www下,在建立网站目录的时候建议使用网站的域名或者IP命名目录,这样好维护

3.2.3           Apache虚拟主机

每一个虚拟主机都有一个单独的配置文件,虚拟主机的配置文件存储在/etc/apache2/sites-available/下。default虚拟主机位于/etc/apache2/sites-available/default,这个虚拟主机已经被链接到/etc/apache2/sites-enabled目录下,所有是已经在运行的虚拟主机。

 

1.  创建一个新的虚拟主机

将默认的虚拟主机复制一份,容易修改

$ sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ blog. mytest.com

对复制的进行编辑,编辑的内容是(NameVirtualHost指令删除(所有的虚拟主机只保留一个NameVirtualHost即可,修改DocumentRoot路径为/var/www/blog.mytest.com,将<Directory /var/www/>修改为<Directory /var/www/blog.mytest.com >,对于日志文件的一些内容不用修改,但是建议修改))

$ sudo nano /etc/apache2/sites-available/blog.mytest.com

创建目录并新建主页文件

$ sudo mkdir /var/www/blog.mytest.com

$ echo "<h1>Oh yeah~</h1>" | sudo tee /var/www/blog.mytest.com/index.html

  禁用虚拟主机并启用新建的虚拟主机重启apache服务器(a2dissiteapache2 disable site的缩写,a2ensiteapache2 enable site的缩写)

$ sudo a2dissite default && sudo a2ensite blog.mytest.com

$ sudo /etc/init.d/apache2 restart

2虚拟主机配置详解

NameVirtualHost *

<VirtualHost *>

        ServerAdmin webmaster@localhost

 

        DocumentRoot /var/www/

        <Directory />

                Options FollowSymLinks

                AllowOverride None

        </Directory>

        <Directory /var/www/>

                Options Indexes FollowSymLinks MultiViews

                AllowOverride None

                Order allow,deny

                allow from all

        </Directory>

 

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

        <Directory "/usr/lib/cgi-bin">

                AllowOverride None

                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

                Order allow,deny

                Allow from all

        </Directory>

 

        ErrorLog /var/log/apache2/error.log

 

        # Possible values include: debug, info, notice, warn, error, crit,

        # alert, emerg.

        LogLevel warn

 

        CustomLog /var/log/apache2/access.log combined

        ServerSignature On

 

    Alias /doc/ "/usr/share/doc/"

    <Directory "/usr/share/doc/">

        Options Indexes MultiViews FollowSymLinks

        AllowOverride None

        Order deny,allow

        Deny from all

        Allow from 127.0.0.0/255.0.0.0 ::1/128

    </Directory>

 

</VirtualHost>

(1)    NameVirtualHost指令

指定服务器的IP地址,只适用于基于名字的虚拟主机。当服务器有多块网卡、多条线路时,通过设置某个网站只能从某块网卡走,对大型网站的部署很有用

NameVirtualHost 192.168.1.10:8080

默认的设置是该虚拟主机可以从所有网卡走

NameVirtualHost *

2<VirtualHost></VirtualHost>指令(里面的指令只针对当前的虚拟主机使用)

<VirtualHost IP地址[:端口号] [IP地址[:端口号]] ...>

...

</VirtualHost>

例如(下面的ip地址也可以用*号表示本机所有的ip地址)

<VirtualHost 192.168.1.10>

  ServerAdmin webmaster@mytest.com

  DocumentRoot /www/docs/www.mytest.com

  ServerName www.mytest.com

  ErrorLog logs/www.mytest.com-error_log

  TransferLog logs/www.mytest.com-access_log

</VirtualHost>

3ServerAdmin指令(管理员邮箱)

ServerAdmin E-mail地址

 

ServerAdmin webmaster@hiweed.com

4DocumentRoot指令(网站的根目录)

DocumentRoot /var/www/blog.mytest.com

5<Directory></Directory>指令中间包含的指令仅对指定的目录有效

特指某个目录

<Directory /var/www/blog.mytest.com>

  ...

</Directory>

支持通配符和正则表达式

<Directory /var/www/*.mytest.com>

  ... # 将匹配/var/www/目录下所有以.mytest.com结尾的目录

</Directory>

 

<Directory ~ "^/var/www/.*/[0-9]{3}">

  ... # 将匹配/var/www/目录下所有由3位数字构成的目录

</Directory>

6Options指令(配置指定目录的特性,比如该目录下是否有符号链接、是否使用cgi)

ALL 除了MultiViews之外的所有特性(默认设置)

ExecCGI 允许该目录通过mod_cgi运行cgi脚本

FollowSymLinks 允许在此目录使用符号链接

Includes 在此目录中允许使用mod_include进行服务器端包含

IncludesNOEXEC 允许服务器端包含,但禁用”#exec cmd””exec cgi”

Indexes  允许列目录(在没有发现首页文件的话)

MultiViews 允许“内容协商”的“多重视图”。内容协商由mod_negotiation模块生成。

SymLinksIfOwnerMatch 只允许使用这样的符号链接:这些符号链接与目标目录(文件)的拥有者具有相同的UerId

<Directory /var/www>

Options Indexes FollowSymLinks

</Directory>

 

<Directory /var/www/spec>

Options Includes

</Directory>

Options指令中,如果多次使用,对于同于个目录,则最特殊的一个会被安全接受,而其他则会被忽略。例如上面的,只有Includes这个选项作用到/var/www/spec。所有作用于Options指令的选项前都加有“+”(覆盖)“-”(移除),那么会进行合并。下面的配置使/var/www/spec具有FollowSymLinksIncludes

<Directory /var/www>

Options Indexes FollowSymLinks

</Directory>

 

<Directory /var/www/spec>

Options +Includes -Indexes

</Directory>

7AllowOverride指令(仅在不包含正则表达式的< Directory >配置端中有效)

AllowOverride All | None | directive-type [directive-type] ...

AllowOverride是针对.htacess文件,AllowOverride属性的值有ALLNoneAuthConfigFileInfoIndexesLimit

(8)Order指令

Order DenyAllow默认允许访问

Order AllowDeny默认拒绝访问

9Allow指令

Allow from all | host | env=env-variable [host | env=env-variable] ...

域名

Allow from apache.org

Allow from .net example.edu

完整的ip地址

Allow from 10.1.2.3

Allow from 192.168.1.104 192.168.1.205

部分ip地址(ip地址的开始1~3个字符,用于子网限制)

Allow from 10.1

Allow from 10 172.20 192.168.2

  网络/掩码(用网络地址和掩码,用于更精确的限制)

Allow from 10.1.0.0/255.255.0.0

网络/nnnCIDR形式)

Allow from 10.1.0.0/16

11ErrorLog指令

日志文件

ErrorLog /var/log/apache/error_log

通过管道符“|”将错误信息交给某个去处理,而不是写入日志文件,httpd_errors为一个程序

ErrorLog "|/usr/local/bin/httpd_errors"

14ServerSignature指令

ServerSignature指令用来定义服务器所产生的页面的页脚,这些页面包括错误信息、mod_proxyftp目录列表、mod_info的输出等

设为Off,则不会添加页脚,设为On的话,会有以下页脚

Apache/2.2.8 (Ubuntu) Server at blog.mytest.com Port 80

15Alias指令

链接目录

Alias URL-path file-path|directory-path

/usr/share/doc/映射成/doc/

Alias /doc/ "/usr/share/doc/"

2.  HTTPS的实现

启用mod_ssl模块(apache2-common软件包提供)

$ sudo a2enmod ssl

要创建CA签名的证书,需要要到openssl

$ sudo apt-get install openssl

      创建key

$ openssl genrsa -des3 -out server.key 1024

Generating RSA private key, 1024 bit long modulus

.........................++++++

......++++++

e is 65537 (0x10001)

Enter pass phrase for server.key:(在这里输入密码,越复杂就越安全)

Verifying - Enter pass phrase for server.key:(再输入一次密码)

可以不设置密码,将-des3去掉即可(不建议)

$ openssl genrsa -out server.key 1024

Generating RSA private key, 1024 bit long modulus

.......................++++++

..................................................................++++++

e is 65537 (0x10001)

      创建CSA(Certificate Singing Request,证书签发请求)

$ openssl req -new -key server.key -out server.csr

      如果不是有CA签名的证书,要使用自己签名的证书,则可以这样做

$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

      安装证书

$ sudo cp server.crt /etc/ssl/certs

$ sudo cp server.key /etc/ssl/private

修改虚拟主机配置文件,启用SSL即可,例如修改default的配置文件(/etc/apache2/sites-available/default)<VirtualHost>段中DocumentRoot一行的下方,添加如下内容

SSLEngine on

 

SSLOptions +StrictRequire

 

SSLCertificateFile /etc/ssl/certs/server.crt

SSLCertificateKeyFile /etc/ssl/private/server.key

 

 

重启服务器即可

$ sudo /etc/init.d/apache2 restart

 

文章整理来自于《Ubuntu Server最佳方案》书籍

由于字数限制,笔记可能无法全部上传,关于性能优化、缓存、压缩、安全、验证等请下载笔记