测试环境:centos 6.5 x86_64的系统
软件:apache2.4.10
apache2.4新特性
Loadable MPMs
可以在编译的时候将多个 MPM 做成可装卸式的。对 MPM 的选择也可以在运行的时候进行配置。
Event MPM
全面支持 Event MPM。这在 Apache 2.2 中还只是试验版本。
Asynchronous support
在支持的 MPM 和平台上提供更好的异步读/写支持。
Per-module and per-directory LogLevel configuration
可以针对不同的模块和目录设置不同的 LogLevel。在 debug 记录级别上增加了 trace1 到 trace8 的跟踪方法。
Per-request configuration sections
<If>, <ElseIf>, 和 <Else> 块可以用来在 可以在每次请求的级别上设定配置。
General-purpose expression parser
新的表达式解析器允许在指令(如 SetEnvIfExpr, RewriteCond, Header, <If> 等)中使用普通的句法指定复杂的条件。
KeepAliveTimeout in milliseconds
现在可以以毫秒(milliseconds)设置连接保持时间( KeepAliveTimeout)。
NameVirtualHost directive
不再需要,已弃用。
Override Configuration
新的 AllowOverrideList 允许对目录( .htaccess 中允许的目录)进行更加精细的控制。
Config file variables
现在可以在配置文件中定义和使用变量了。
Reduced memory usage
虽然增加了很多新功能,2.4.x 版 Apache 还是成功将内存占用降低了(相比 2.2.x 版)。
准备工作
在编译前还是需要做一些准备的。
1、如果服务器上之前有apache2.2.x那么我们需要停用apache2.2.x并取消其开机自动运行
2、apache2.4.x依赖的apr也需要1.4版本以上的,所以在编译前还需要编译高版本的qpr
3、制作apache2.4.x可用的启动脚本和配置开机启动等
一、停用apache2.2.x(Centos 自带的httpd服务)
[[email protected] apr-1.5.1]# service httpd stop Stopping httpd: [FAILED] [[email protected] apr-1.5.1]# chkconfig httpd off [[email protected] apr-1.5.1]# chkconfig --list | grep httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
二、apr >= 1.4版本的编译
高版本apr可以在官方网站下载 http://apr.apache.org/download.cgi
这里我采用apr1.5.1和apr-util-1.5.3的版本
[[email protected] ~]# yum groupinstall "Development tools" "Server Platform Development" -y [[email protected] ~]# wget http://www.dwhd.org/tar_gz_bz2/apr-1.5.1.tar.gz [[email protected] ~]# wget http://www.dwhd.org/tar_gz_bz2/apr-util-1.5.3.tar.bz2 [[email protected] ~]# tar xf apr-1.5.1.tar.gz [[email protected] ~]# tar xf apr-util-1.5.3.tar.bz2 [[email protected] ~]# cd apr-1.5.1 [[email protected] apr-1.5.1]# ./configure --prefix=/usr/local/apr151 [[email protected] apr-1.5.1]# make -j4 &amp;&amp; make install [[email protected] apr-1.5.1]# cd ../apr-util-1.5.3 [[email protected] apr-util-1.5.3]# ./configure --prefix=/usr/local/aprutil153 --with-apr=/usr/local/apr151/ [[email protected] apr-util-1.5.3]# make -j4 &amp;&amp; make install &amp;&amp; cd ~
三、编译安装apache2.4.x
apache可以在官方网站下载 http://httpd.apache.org/download.cgi
[Downlink rel="external nofollow" target="_blank" href="https://www.dwhd.org/wp-content/themes/begin/go.php?url=aHR0cDovL3d3dy4wNWhkLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNC8wOC9odHRwZC0yLjQuMTAudGFyLmJ6Mg=="]httpd-2.4.10.tar.bz2[/Downlink]
这里我采用的是apache2.4.10
[[email protected] ~]# wget http://www.05hd.com/wp-content/uploads/2014/08/httpd-2.4.10.tar.bz2 [[email protected] ~]# tar xf httpd-2.4.10.tar.bz2 [[email protected] ~]# cd httpd-2.4.10 [[email protected] httpd-2.4.10]# ./configure --prefix=/usr/local/apache2410 --sysconfdir=/etc/httpd2410 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr151/ --with-apr-util=/usr/local/aprutil153/ --enable-mpms-shared=all --with-mpm=event --enable-modules=most [[email protected] httpd-2.4.10]# make -j4 &amp;&amp; make install ###如果在编译的时候提示错误为: configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/ 就安装pcre-devel然后再编译安装 [[email protected] httpd-2.4.10]# yum install pcre-devel -y #
接下来更新环境变量导入apache新版的bin和man+到处头文件
[[email protected] ~]# echo "export PATH=/usr/local/apache2410/bin/:\$PATH" &gt; /etc/profile.d/apache2410.sh [[email protected] ~]# sed -i "$(awk '$1=="MANPATH"{i=NR}END{print i}' /etc/man.config)a \MANPATH\tMANPATH /usr/local/apache2410/man" /etc/man.config [[email protected] ~]# ln -s /usr/local/apache2410/include/ /usr/include/apache ###这里的3部做完需要系统的环境变量生效,可以新开个shell进程 或者直接重启下系统 ##重启后检查下 [[email protected] ~]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/bind995/bin:/usr/local/bind995/sbin:/usr/local/apache2410/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [[email protected] ~]# man httpd [[email protected] ~]# httpd -v Server version: Apache/2.4.10 (Unix) Server built: Aug 10 2014 12:25:37
现在我们可以来启动新版的httpd服务了
[[email protected] ~]# apachectl start AH00557: httpd: apr_sockaddr_info_get() failed for LookBack163.05hd.org AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message ###出现上面的信息可以忽略
来看看80端口是否被监听了
[[email protected] ~]# ss -utnl Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 *:111 *:* udp UNCONN 0 0 *:631 *:* udp UNCONN 0 0 *:760 *:* udp UNCONN 0 0 *:37776 *:* udp UNCONN 0 0 *:820 *:* udp UNCONN 0 0 172.16.41.163:53 *:* udp UNCONN 0 0 172.16.41.100:53 *:* udp UNCONN 0 0 127.0.0.1:53 *:* udp UNCONN 0 0 :::111 :::* udp UNCONN 0 0 :::760 :::* udp UNCONN 0 0 :::34273 :::* tcp LISTEN 0 128 :::111 :::* tcp LISTEN 0 128 *:111 *:* tcp LISTEN 0 128 :::80 :::* tcp LISTEN 0 10 172.16.41.163:53 *:* tcp LISTEN 0 10 172.16.41.100:53 *:* tcp LISTEN 0 10 127.0.0.1:53 *:* tcp LISTEN 0 128 :::22 :::* tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 128 127.0.0.1:631 *:* tcp LISTEN 0 128 ::1:631 :::* tcp LISTEN 0 100 ::1:25 :::* tcp LISTEN 0 100 127.0.0.1:25 *:* tcp LISTEN 0 128 :::54235 :::* tcp LISTEN 0 128 *:33671 *:*
来看看浏览器访问的结果
四、配置apache2.4.x的启动脚本
第一种方法:(推荐)
[[email protected] ~]# for I in $(ps -aux | grep [h]ttp |awk '{print$2}'); do kill -9 $I;done [[email protected] ~]# for I in $(ps -aux | grep [a]pache |awk '{print$2}'); do kill -9 $I;done ###上2行的命令是强制结束所有httpd的进程 [[email protected] ~]# [ -f /etc/init.d/httpd ] &amp;&amp; mv /etc/init.d/httpd /etc/init.d/httpd.bak [[email protected] ~]# cp /usr/local/apache2410/bin/apachectl /etc/init.d/httpd [[email protected] ~]# sed -i '/#!\/bin\/sh/a \# chkconfig: 35 85 15\n# description: Activates\/Deactivates Apache 2.4.10' /etc/init.d/httpd [[email protected] ~]# chkconfig --add httpd [[email protected] ~]# chkconfig --list httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [[email protected] ~]# chkconfig httpd on [[email protected] ~]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
第二种方法:
[[email protected] ~]# [ -f /etc/init.d/httpd ] &amp;&amp; mv /etc/init.d/httpd /etc/init.d/httpd.bak [[email protected] ~]# wget -c4 -O /etc/rc.d/init.d/httpd "http://www.05hd.com/httpd.sh" [[email protected] ~]# chkconfig --add httpd [[email protected] ~]# chkconfig --list httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [[email protected] ~]# chkconfig httpd on [[email protected] ~]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ###下面是启动脚本的源码 #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache # Short-Description: start and stop Apache HTTP Server # Description: The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions #if [ -f /etc/sysconfig/httpd ]; then # . /etc/sysconfig/httpd #fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. #apachectl=/usr/sbin/apachectl apachectl=/usr/local/apache2410/bin/apachectl httpd=${HTTPD-/usr/local/apache2410/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache2410/logs/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] &amp;&amp; touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd RETVAL=$? echo [ $RETVAL = 0 ] &amp;&amp; rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t &gt;&amp;/dev/null; then RETVAL=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 killproc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart|try-restart) if status -p ${pidfile} $httpd &gt;&amp;/dev/null; then stop start fi ;; force-reload|reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL
到此 新版的apache启动脚本就好了
五、apache上配置多虚拟机(基于端口 、基于IP和基于主机名的3种情况)
下面来看看/etc/httpd2410/httpd.conf的配置内容
[[email protected] ~]# cat /etc/httpd2410/httpd.conf | grep '^[^#]' | grep -vE '^[[:space:]]+#' Listen 80 Listen 8080 LoadModule authn_file_module modules/mod_authn_file.so LoadModule authn_core_module modules/mod_authn_core.so LoadModule authz_host_module modules/mod_authz_host.so LoadModule authz_groupfile_module modules/mod_authz_groupfile.so LoadModule authz_user_module modules/mod_authz_user.so LoadModule authz_core_module modules/mod_authz_core.so LoadModule access_compat_module modules/mod_access_compat.so LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule reqtimeout_module modules/mod_reqtimeout.so LoadModule filter_module modules/mod_filter.so LoadModule mime_module modules/mod_mime.so LoadModule log_config_module modules/mod_log_config.so LoadModule env_module modules/mod_env.so LoadModule headers_module modules/mod_headers.so LoadModule setenvif_module modules/mod_setenvif.so LoadModule version_module modules/mod_version.so LoadModule mpm_event_module modules/mod_mpm_event.so LoadModule unixd_module modules/mod_unixd.so LoadModule status_module modules/mod_status.so LoadModule autoindex_module modules/mod_autoindex.so LoadModule dir_module modules/mod_dir.so LoadModule alias_module modules/mod_alias.so &lt;IfModule unixd_module&gt; User daemon Group daemon &lt;/IfModule&gt; ServerAdmin ad[email protected] ServerName LookBack163.05hd.com:80 &lt;Directory /&gt; AllowOverride none Require all denied &lt;/Directory&gt; DocumentRoot "/usr/local/apache2410/htdocs" &lt;Directory "/usr/local/apache2410/htdocs"&gt; Options Indexes FollowSymLinks AllowOverride None Require all granted &lt;/Directory&gt; &lt;IfModule dir_module&gt; DirectoryIndex index.html &lt;/IfModule&gt; &lt;Files ".ht*"&gt; Require all denied &lt;/Files&gt; ErrorLog "logs/error_log" LogLevel warn &lt;IfModule log_config_module&gt; LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common &lt;IfModule logio_module&gt; LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio &lt;/IfModule&gt; CustomLog "logs/access_log" common &lt;/IfModule&gt; &lt;IfModule alias_module&gt; ScriptAlias /cgi-bin/ "/usr/local/apache2410/cgi-bin/" &lt;/IfModule&gt; &lt;IfModule cgid_module&gt; &lt;/IfModule&gt; &lt;Directory "/usr/local/apache2410/cgi-bin"&gt; AllowOverride None Options None Require all granted &lt;/Directory&gt; &lt;IfModule mime_module&gt; TypesConfig /etc/httpd2410/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz &lt;/IfModule&gt; Include /etc/httpd2410/extra/httpd-vhosts.conf &lt;IfModule proxy_html_module&gt; Include /etc/httpd2410/extra/proxy-html.conf &lt;/IfModule&gt; &lt;IfModule ssl_module&gt; SSLRandomSeed startup builtin SSLRandomSeed connect builtin &lt;/IfModule&gt; &lt;Directory /&gt; AllowOverride none Require all denied &lt;/Directory&gt; DocumentRoot "/usr/local/apache2410/htdocs" &lt;Directory "/usr/local/apache2410/htdocs"&gt; Options Indexes FollowSymLinks AllowOverride None Require all granted &lt;/Directory&gt; &lt;IfModule dir_module&gt; DirectoryIndex index.html &lt;/IfModule&gt; &lt;Files ".ht*"&gt; Require all denied &lt;/Files&gt; ErrorLog "logs/error_log" LogLevel warn &lt;IfModule log_config_module&gt; LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common &lt;IfModule logio_module&gt; LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio &lt;/IfModule&gt; CustomLog "logs/access_log" common &lt;/IfModule&gt; &lt;IfModule alias_module&gt; &lt;/IfModule&gt; &lt;IfModule cgid_module&gt; &lt;/IfModule&gt; &lt;Directory "/usr/local/apache2410/cgi-bin"&gt; AllowOverride None Options None Require all granted &lt;/Directory&gt; &lt;IfModule mime_module&gt; TypesConfig /etc/httpd2410/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz &lt;/IfModule&gt; Include /etc/httpd2410/extra/httpd-vhosts.conf &lt;IfModule proxy_html_module&gt; Include /etc/httpd2410/extra/proxy-html.conf &lt;/IfModule&gt; &lt;IfModule ssl_module&gt; SSLRandomSeed startup builtin SSLRandomSeed connect builtin &lt;/IfModule&gt;
下面来看看/etc/httpd2410/extra/httpd-vhosts.conf的配置内容
[[email protected] ~]# cat /etc/httpd2410/extra/httpd-vhosts.conf | grep '^[^#]' &lt;VirtualHost 172.16.41.163:80&gt; ServerAdmin [email protected] ServerName www1.05hd.com DocumentRoot "/web/hosta" ErrorLog "logs/www1.05hd.com-erro_log" CustomLog "logs/www1.05hd.com-access_log" common &lt;Directory "/web/hosta" &gt; Require all granted &lt;/Directory&gt; &lt;/VirtualHost&gt; &lt;VirtualHost 172.16.41.100:80&gt; ServerAdmin [email protected] ServerName www2.05hd.com DocumentRoot "/web/hostb" ErrorLog "logs/www2.05hd.com-error_log" CustomLog "logs/www2.05hd.com-access_log" common &lt;Directory "/web/hostb" &gt; Require all granted &lt;/Directory&gt; &lt;/VirtualHost&gt; &lt;VirtualHost 172.16.41.163:8080 172.16.41.163:80&gt; ServerAdmin [email protected] ServerName www3.05hd.com DocumentRoot "/web/hostc" ErrorLog "logs/www3.05hd.com-error_log" CustomLog "logs/www3.05hd.com-access_log" common &lt;Directory "/web/hostc" &gt; Require all granted &lt;/Directory&gt; &lt;/VirtualHost&gt;
接下来制作3个虚拟机的index.html测试页面
[[email protected] extra]# echo " &lt;h1&gt;This is A&lt;/h1&gt; " &gt; /web/hosta/index.html [[email protected] extra]# echo " &lt;h1&gt;This is B&lt;/h1&gt; " &gt; /web/hostb/index.html [[email protected] extra]# echo " &lt;h1&gt;This is C&lt;/h1&gt; " &gt; /web/hostc/index.html
重新启动下httpd并看看端口监听状况
[[email protected] ~]# apachectl restart [[email protected] ~]# ss -utnl Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 *:111 *:* udp UNCONN 0 0 *:631 *:* udp UNCONN 0 0 *:764 *:* udp UNCONN 0 0 *:48421 *:* udp UNCONN 0 0 172.16.41.163:53 *:* udp UNCONN 0 0 172.16.41.100:53 *:* udp UNCONN 0 0 127.0.0.1:53 *:* udp UNCONN 0 0 *:824 *:* udp UNCONN 0 0 :::111 :::* udp UNCONN 0 0 :::764 :::* udp UNCONN 0 0 :::50874 :::* tcp LISTEN 0 128 :::111 :::* tcp LISTEN 0 128 *:111 *:* tcp LISTEN 0 128 :::8080 :::* tcp LISTEN 0 128 :::80 :::* tcp LISTEN 0 10 172.16.41.163:53 *:* tcp LISTEN 0 10 172.16.41.100:53 *:* tcp LISTEN 0 10 127.0.0.1:53 *:* tcp LISTEN 0 128 :::22 :::* tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 128 127.0.0.1:631 *:* tcp LISTEN 0 128 ::1:631 :::* tcp LISTEN 0 100 ::1:25 :::* tcp LISTEN 0 100 127.0.0.1:25 *:* tcp LISTEN 0 128 *:38142 *:* tcp LISTEN 0 128 :::53573 :::*
六、配置SSL访问
未完待续
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏