→环境配置

1. 安装C、C++编译器:

[root@centos local]# yum install -y gcc gcc-c++


2. 安装APR 1.5.2解决apr not found问题

[root@centos local]# wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz

[root@centos local]# tar -zxf apr-1.5.2.tar.gz 

[root@centos apr-1.5.2]# cd apr-1.5.2

[root@centos apr-1.5.2]# ./configure --prefix=/usr/local/apr

[root@centos apr-1.5.2]# make

[root@centos apr-1.5.2]# make install

如编译configure过程中,遇到 rm: cannot remove `libtoolT' 错误,编辑 configure文件,查找 $RM "$cfgfile" 这个地方,用#注释掉,然后就可以了。


3.安装APR-UTIL 1.5.4解决APR-util not found问题:

[root@centos local]# wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz

[root@centos local]# tar -zxf apr-util-1.5.4.tar.gz

[root@centos local]# cd apr-util-1.5.4

[root@centos apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config

[root@centos apr-util-1.5.4]# make

[root@centos apr-util-1.5.4]# make install


4. 安装PCRE 8.39 解决pcre-config for libpcre not found问题):

[root@centos local]# wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz

[root@centos local]# tar -zxf pcre-8.39.tar.gz

[root@centos local]# cd pcre-8.39

[root@centos pcre-8.39]# ./configure --prefix=/usr/local/pcre

[root@centos pcre-8.39]# make

[root@centos pcre-8.39]# make install


5. 安装zlib-devel (解决mod_deflate has been requested but can not be built due to prerequisite failures):

[root@centos httpd-2.4.23]# yum install -y zlib-devel


安装apache(httpd-2.4.23)

[root@centos local]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.23.tar.gz

[root@centos httpd-2.4.23]# tar -zxf httpd-2.4.23.tar.gz 

[root@centos local]# cd httpd-2.4.23

[root@centos httpd-2.4.23]#

./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite 

[root@centos httpd-2.4.23]# make

[root@centos httpd-2.4.23]# make install


./configure 后面参数说明:

--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre  #路径为源码目录

--prefix=/usr/local/apache2         #apache的默认安装路径。

--enable-deflate                         #提供对内容的压缩传输编码支持,提高传输速度。调优参数

--enable-expires                        #对网站图片,js,css等内容,提供在客户端浏览器缓存设置。调优参数

--enable-headers                       #允许对HTTP请求头控制。

--enable-so                                #激活apache的DSO支持,以后可以以DSO的方式编译安装共享模块。

--enable-rewrite                         #提供基于URL规则的重写功能。

--with-mpm=worker                    #使用线程方式来处理请求,系统资源开销小于 MPM perfork。调优参数


安装完成后检查编译安装情况:

1. 静态加载的模块(-l 参数)

[root@centos bin]# /usr/local/apache2/bin/apachectl -l

Compiled in modules:

  core.c

  mod_so.c

  http_core.c

  worker.c


2. 基于当前配置加载的所有模块(-M 参数)

[root@centos bin]# /usr/local/apache2/bin/apachectl -M

Loaded Modules:

 core_module (static)

 so_module (static)

 http_module (static)

 mpm_worker_module (static)

 authn_file_module (shared)

 authn_core_module (shared)

 authz_host_module (shared)

 authz_groupfile_module (shared)

 authz_user_module (shared)

 authz_core_module (shared)

 access_compat_module (shared)

 auth_basic_module (shared)

 reqtimeout_module (shared)

 filter_module (shared)

 mime_module (shared)

 log_config_module (shared)

 env_module (shared)

 headers_module (shared)

 setenvif_module (shared)

 version_module (shared)

 unixd_module (shared)

 status_module (shared)

 autoindex_module (shared)

 dir_module (shared)

 alias_module (shared)


3. 检查编译参数:

[root@centos bin]# /usr/local/apache2/bin/apachectl -V

Server version: Apache/2.4.23 (Unix)

Server built:   Jul  7 2016 10:28:32

Server's Module Magic Number: 20120211:61

Server loaded:  APR 1.5.2, APR-UTIL 1.5.4

Compiled using: APR 1.5.2, APR-UTIL 1.5.4

Architecture:   32-bit

Server MPM:     worker

  threaded:     yes (fixed thread count)

    forked:     yes (variable process count)

Server compiled with....

 -D APR_HAS_SENDFILE

 -D APR_HAS_MMAP

 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)

 -D APR_USE_SYSVSEM_SERIALIZE

 -D APR_USE_PTHREAD_SERIALIZE

 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT

 -D APR_HAS_OTHER_CHILD

 -D AP_HAVE_RELIABLE_PIPED_LOGS

 -D DYNAMIC_MODULE_LIMIT=256

 -D HTTPD_ROOT="/usr/local/apache2"

 -D SUEXEC_BIN="/usr/local/apache2/bin/suexec"

 -D DEFAULT_PIDLOG="logs/httpd.pid"

 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"

 -D DEFAULT_ERRORLOG="logs/error_log"

 -D AP_TYPES_CONFIG_FILE="conf/mime.types"

 -D SERVER_CONFIG_FILE="conf/httpd.conf"


启动apapche服务

[root@centos bin]# /usr/local/apache2/bin/apachectl start


bin目录主要文件作用:

/usr/local/apache2/bin/

├── ab                                # Apache服务器性能测试工具

├── apachectl                     #Apache启动脚本

├── apxs                            #Apache服务器编译和安装扩展模块的工具

├── htpasswd                    #建立和更新基本认证文件

├── httpd                           #Apache控制命令程序

└── rotatelogs                   #Apache自带日志轮询工具


conf目录主要文件作用:

/usr/local/apache2/conf/

├── extra                                #Apache辅助配置文件目录

│   ├── httpd-autoindex.conf

│   ├── httpd-dav.conf

│   ├── httpd-default.conf

│   ├── httpd-info.conf

│   ├── httpd-languages.conf

│   ├── httpd-manual.conf

│   ├── httpd-mpm.conf

│   ├── httpd-multilang-errordoc.conf

│   ├── httpd-ssl.conf

│   ├── httpd-userdir.conf

│   ├── httpd-vhosts.conf

│   └── proxy-html.conf

├── httpd.conf                    #Apache主配置文件


htdocs目录

/usr/local/apache2/htdocs/

└── index.html                        #默认首页文件


apache日志目录

/usr/local/apache2/logs/

├── access_log

└── error_log


apache模块目录

/usr/local/apache2/modules/


→配置基于域名的虚拟web服务

1. 编辑httpd.conf文件

[root@centos conf]# vim httpd.conf 

取消Include conf/extra/httpd-vhosts.conf的注释

# Virtual hosts

Include conf/extra/httpd-vhosts.conf


2. 编辑httpd-vhosts.conf文件

[root@centos conf]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

增加虚拟主机配置

    ServerAdmin [email protected]

    DocumentRoot "/usr/local/apache2/htdocs/vhost1"

    ServerName vhost1.centos.com


    ServerAdmin [email protected]

    DocumentRoot "/usr/local/apache2/htdocs/vhost2"

    ServerName vhost2.centos.com


3. 编辑hosts文件或增加DNS A记录

[root@centos conf]# vim /etc/hosts


172.16.1.11     vhost1.centos.com

172.16.1.11     vhost2.centos.com


→配置web服务身份验证

1. 编辑httpd.conf文件

[root@centos conf]# vim httpd.conf 

添加配置

"/usr/local/apache2/htdocs/vhost1">

    AuthType basic

    AuthName "centos web site Authority"

    AuthUserFile /usr/local/apache2/bin/.htpasswd

    Require valid-user

AuthUserFile 必须使用绝对路径


2. 创建用户和密码

user1:

[root@centos bin]# ./htpasswd -cm .htpasswd user1

New password: 

Re-type new password: 

Adding password for user user1

user2:

[root@centos bin]# ./htpasswd -m .htpasswd user2

New password: 

Re-type new password: 

Adding password for user user2


查看用户:

[root@centos bin]# cat .htpasswd 

user1:$apr1$gMEduFiy$7a0WvrNQYDjvVXHieNidM0

user2:$apr1$6SKnEMv5$5YciH4JB2WhZjdsaMso4R1