Fork me on GitHub

Apache基本配置

简介

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,是目前比较广泛使用的一种web server,具有跨平台、高效和稳定等特性。

Apache2目录结构:

apache2
|--apache2.conf
|--conf-available
|--conf-enabled
|--conf.d
|--envvars
|--http.conf
|--magic
|--mods-available
|--mods-enabled
|--ports.conf
|--sites-avaliable
|--sites-enabled
  • apache2.conf: 主要配置文件
  • envvars: Apache2环境变量设置,例如apache的用户和组
  • httpd.conf:遗留的Apache2主要配置文件。这个文件是空的,其中的配置选项都转移到其他配置文件里了
  • ports.conf: 监听 80 端口(默认HTTP通信端口)和 443 端口(SSL支持)如果你想 apache 监听其他端口,只需添加更多的 Listen 指令
  • conf.d/: 用来控制Apache的一些特殊配置,比如SSL配置。
  • sites-available/: web站点的虚拟主机文件,不同的请求对应不同的内容,但并不表示都正在使用
  • sites-enabled/: 包含正在使用的虚拟主机的定义,通常只包含到sites-available目录下文件的符号链接。
  • mods-available: 目录包含模块和模块配置文件,不是所有的模块都有配置文件
  • mods-enabled: 持有/etc/apache2/mods-available目录下文件的链接,当该目录下有一个模块文件和其配置文件,那么Apache重启后该模块将生效

Apache2.conf

主要分成三部分,全局配置、默认服务器配置和虚拟主机配置,在Ubuntu系统下,这个文件主要负责全局配置,默认服务器和虚拟主机可以通过Include语句来处理。

全局配置

Timeout: 默认设置为300,意思是服务器用300s来处理每个请求,即接收和发送的超时时间
KeepAlive: 设置为On,将允许同个客户端每个连接一直保持来处理多个请求(HTTP长连接)
MaxKeepAliveRequests: 设置每个连接最多能处理多少个单独的请求
KeepAliveTimeout: 参数设置下一个请求来之前来等待多久,超过这个时间自动关闭这个connection

MPM配置

MPM(Multi-Processing Module,多进程处理模块)模式,主要包括三种稳定的模式,分别是prefork,worker和event,它们同时也代表这Apache的演变和发展

  • event:这个MPM适用于需要有大量持续连接的(KeepAlive)的情况,KeepAlive的好处相信大家都知道,可以在同一个TCP连接中响应多次请求,这种请求方式可以使一个包含很多元素(图片,CSS,etc)的HTML网页加速一半以上。在配置文件中设置KeepAlive为On,就可以开启KeepAlive。
  • perfork:实现了一个非线程的MPM,兼容1.3,虽然速度不是很快,但是非常稳定,能够隔离每一个请求。perfork顾名思义,就是主进程先派生出一堆子进程,这样新的请求来了以后不需要等待服务器产生子进程所花的时间。使用perfork最重要的是设置MacClients的值,要足够大以发挥很好的性能,但是不能太高防止内存爆掉。
  • worker:Apache2的新MPM。多进程+多线程,资源占用小的同时也比perfork要高效的多,是未来Apache2的发展趋势吧。该MPM重要的是两个选项:MaxClients和ThreadsPerChild。ThreadsPerChild用来每个子进程允许建立的线程数,MaxClients用来控制允许建立的总线程数。

Apache的模式,可以使用httpd -V命令来查看:

Server version: Apache/2.4.18 (Unix)
Server built:   Feb 20 2016 20:03:19
Server's Module Magic Number: 20120211:52
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)

综上所述,如果需要更好的伸缩性,选择worker或event,如果需要更好的稳定性和兼容性,选择perfork。如果无法评估自己的需要,不妨直接选择worker

如果需要切换模式,可以通过如下方法来切换:

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo service apache2 restart #重启apache2

Apache中启用和禁用网站和模块

启用和禁用网站

sudo a2ensite 虚拟主机文件名(example.com.conf)
sudo a2dissite 虚拟主机文件名(example.com.conf)

启用和禁用模块

sudo a2enmod 模块配置文件名
sudo a2dismod  模块配置文件名

启用用户文件夹实现文件服务器的功能

1.使用如下命令启用userdir模块

sudo a2enmod userdir

2.userdir.conf配置userdir模块

sudo vim /etc/apache2/mods-enabled/userdir.conf

内容如下,把public_html改为你的个人文件夹名称,如果不存在则创建:

<IfModule mod_userdir.c>
    UserDir public_html
    UserDir disabled root

    <Directory /home/*/public_html>
        AllowOverride All
        Options MultiViews Indexes SymLinksIfOwnerMatch
        <Limit GET POST OPTIONS>
            # Apache <= 2.2:
            Order allow,deny
            Allow from all

            # Apache >= 2.4:
            #Require all granted
        </Limit>
        <LimitExcept GET POST OPTIONS>
            # Apache <= 2.2:
            Order deny,allow
            Deny from all

            # Apache >= 2.4:
            #Require all denied
        </LimitExcept>
    </Directory>
</IfModule>

3.创建个人文件夹并重启Apache

sudo service apache2 restart #重启
mkdir /home/$USER/public_html

4.给你的文件添加访问权限

上面的AllowOverride All改为AllowOverride AuthConfig,然后给你的服务器添加认证用户,认证用户保存在/var/www/passwd/中,需要使用htpasswd命令来添加用户,如下所示:

htpasswd -c /var/www/passwd/public_html lippi 
New password: mypassword
Re-type new password: mypassword
Adding password for user lippi

userdir.conf调整为:

<IfModule mod_userdir.c>
    UserDir public_html 
    UserDir disabled root

    <Directory /home/*/public_html>
        AllowOverride FileInfo AuthConfig Limit Indexes
        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        <Limit GET POST OPTIONS>
            #Require all granted
            AuthType Basic
            AuthName "lippi"
            AuthUserFile /var/www/passwd/public_html
            Require valid-user
        </Limit>
        <LimitExcept GET POST OPTIONS>
            Require all denied
        </LimitExcept>
    </Directory>
</IfModule>

virtualHost部分参数配置详解

1.AllowOverride All就是允许根目录下的.htaccess起rewrite作用,AllowOverride None禁止.htaccess起作用
2.order是设置deny/allow的组合排序, 不区分大小写,中间用”,”分开,中间不能有空格

Order Deny,Allow #表示设定"先检查禁止设定,没有设定禁止的全部允许"
Order Allow,Deny #表示设定"先检查允许设定,没有设定允许的全部禁止"

最后的访问结果由第二参数决定!

安全配置

隐藏apache信息

查看并编辑conf-available/security.conf:

ServerTokens OS  修改为:ServerTokens Prod (在出现错误页的时候不显示服务器操作系统的名称)
ServerSignature On 修改为:ServerSignature Off(不回显apache版本信息)

如果conf-enabled/security.conf不存在,则:

cd /etc/apache2/conf-enabled
ln -s ../conf-available/security.conf security.conf

下面是ServerTokens的一些可能的赋值:

ServerTokens Prod   显示 "Server: Apache"
ServerTokens Major  显示 "Server: Apache/2"
ServerTokens Minor  显示 "Server: Apache/2.4"
ServerTokens Min    显示 "Server: Apache/2.4.18"
ServerTokens OS     显示 "Server: Apache/2.4.18 (Ubuntu)"
ServerTokens Full   显示 "Apache/2.4.18 (Ubuntu) PHP/7.0.33-0ubuntu0.16.04.1"

PHP版本号隐藏

http响应头中的X-Powered-By泄露了php版本信息,修改php.ini:

expose_php = Off

LAMP环境安装

ubuntu lamp

以Ubuntu为例,不同版本的ubuntu,所提供源不同,自然所包含的php、mysql、apache等的版本也是不同的,安装的时候需要选择相应的版本进行安装,如果提供的不能满足需求,需要手动添加源(即/etc/apt/sources.list下添源链接或者add-apt-repository命令添加PPA源)

php安装

sudo apt-get install php5.6 php5.6-common php5.6-curl php5.6-gd php5.6-mcrypt php5.6-xml 
                    php5.6-bcmath php5.6-mbstring php5.6-mysql

mysql安装

sudo apt-get install mysql mysql-server

apache安装

sudo apt-get install apache2

安装对脚本语言的支持

sudo apt-get install libapache2-mod-php5

相关问题

问题一:

root@ubuntu:~# a2ensite example.com 
ERROR:  Site example.com does not exists!

解决方案:

# cd /etc/apache2/sites-available/
# mv example.com example.com.conf

附:相关资料

轻轻的我走了,正如我轻轻的来