很早就想把apache,由于一直使用的是nginx,一直都拖着,现在就基于httpd 4.x的新特性小结下并给出一个安装ecmall的过程。
apace 处理请求的过程
1 dns查询过程
2 client和服务器建立tcp连接,并请求监听在服务器的80端口(httpd进程在启动时候向tcp/ip协议栈申请服务器地址+80端口的套接字,tcp/ip协议栈就把80端口标示已用并和httpd进程关联起来!)
3 web server 在用户空间处理client请求,访问或者下载文件需要通过内核访问硬盘数据(动态内容经过处理后格式成html文件)再经过tcp/ip封装返给client!
http 请求报文的语法:
<method> <request-URL> <version> <headers> <entity-body>
http 响应报文的语法:
<version> <status> <reason-phrase> <headers> <entity-body>
如下面:
请求报文: GET / HTTP/1.1 Host: www.magedu.com Connection: keep-alive 响应报文: HTTP/1.1 200 OK X-Powered-By: PHP/5.2.17 Vary: Accept-Encoding,Cookie,User-Agent Cache-Control: max-age=3, must-revalidate Content-Encoding: gzip Content-Length:
apache 配置文件
httpd.conf全局配置:
ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full
这个指令控制了服务器回应给客户端的"Server:"应答头是否包含关于服务器操作系统类型和编译进的模块描述信息,默认是full。
不同选项显示内容如下:
ServerTokens Full (or not specified) Server sends (e.g.): Server: Apache/2.4.2 (Unix) PHP/4.2.2 MyMod/1.2 ServerTokens Prod[uctOnly] Server sends (e.g.): Server: Apache ServerTokens Major Server sends (e.g.): Server: Apache/2 ServerTokens Minor Server sends (e.g.): Server: Apache/2.4 ServerTokens Min[imal] Server sends (e.g.): Server: Apache/2.4.2 ServerTokens OS Server sends (e.g.): Server: Apache/2.4.2 (Unix)
Timeout seconds
默认是 60s 这个官方给的解释是
The TimeOut directive defines the length of time Apache httpd will wait for I/O in various circumstances:
When reading data from the client, the length of time to wait for a TCP packet to arrive if the read buffer is empty.
When writing data to the client, the length of time to wait for an acknowledgement of a packet if the send buffer is full.
KeepAlive On|Off KeepAliveTimeout num[ms] MaxKeepAliveRequests number
上面定义了是否启用长连接,默认是启用的,超时时间默认是5s(httpd-2.4.x支持ms级)
TheMaxKeepAliveRequests
directive limits the number of requests allowed per connection whenKeepAlive
is on. If it is set to0
, unlimited requests will be allowed. We recommend that this setting be kept to a high value for maximum server performance。默认是100!
EnableSendfile off|on
是否启用小文件直接从内核传给用户默认不启用!
apache mpm
Web服务器处理并发连接请求的架构方式 1、单线程web服务器(Single-threaded web servers) 此种架构方式中,web服务器一次处理一个请求,结束后读取并处理下一个请求。在某请求处理过程中,其它所有的请求将被忽略,因此,在并发请求较多的场景中将会出现严重的必能问题。 2、多进程/多线程web服务器 此种架构方式中,web服务器生成多个进程或线程并行处理多个用户请求,进程或线程可以按需或事先生成。有的web服务器应用程序为每个用户请求生成一个单独的进程或线程来进行响应,不过,一旦并发请求数量达到成千上万时,多个同时运行的进程或线程将会消耗大量的系统资源。 3、I/O多路复用web服务器 为了能够支持更多的并发用户请求,越来越多的web服务器正在采用多种复用的架构――同步监控所有的连接请求的活动状态,当一个连接的状态发生改变时(如数据准备完毕或发生某错误),将为其执行一系列特定操作;在操作完成后,此连接将重新变回暂时的稳定态并返回至打开的连接列表中,直到下一次的状态改变。由于其多路复用的特性,进程或线程不会被空闲的连接所占用,因而可以提供高效的工作模式。 4、多路复用多线程web服务器 将多进程和多路复用的功能结合起来形成的web服务器架构,其避免了让一个进程服务于过多的用户请求,并能充分利用多CPU主机所提供的计算能力。
rpm 切换 : rpm安装的需要改/etc/sysconfig/httpd
编译安装的apache在添加--enable-mpms-shared=all这项后可以动态加载对应mpm 配置文件在httpd-mpm.conf
httpd.conf main配置:(具体参数可以看http://httpd.apache.org/docs/2.4/mod/directives.html#K)
先看httpd-2.2.x配置
<Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory>
Options
None: 不支持任何选项
Indexes: 允许索引目录
FollowSynLinks: 允许访问符号链接指向的原文件
Includes: 允许执行服务端包含(SSI)
ExecCGI: 允许运行CGI脚本
MultiViews:支持多视图显示(如国际化网站)
All: 支持所有选项
Order:用于定义基于主机的访问功能的,IP,网络地址或主机定义访问控制机制
Order allow,deny
allow from
deny from
httpd-2.4.x已经对于目录限制改变
<Directory "/www/vhost/"> Options Indexes FollowSymLinks AllowOverride all Require all granted </Directory>
<Files ".ht*"> Require all denied </Files>
TypesConfig /etc/mime.types DefaultType text/plain
HostnameLookups Off ##日志中是否对ip地址反解析
Alias /icons/ "/var/www/icons/"
路径别名,注意后面的斜线,要么都有要么都没有!
apache虚拟主机和基于ssl加密通信的http
<VirtualHost *:80> DocumentRoot "/www/www.angus.com/" ServerName www.angus.com ErrorLog "logs/www.angus.com-error.log" CustomLog "logs/www.angus.com-access.log" combined <Directory "/www/www.angus.com/"> Options Indexes FollowSymLinks AllowOverride all Require all granted </Directory> </VirtualHos
<VirtualHost *:443> DocumentRoot "/www/www.angus.com/" ServerName www.angus.com:443 ErrorLog "/usr/local/apache/logs/www.angus.com_log" TransferLog "/usr/local/apache/logs/www.angus.com_log" SSLEngine on SSLCertificateFile "/etc/httpd/www.angus.com.cer" SSLCertificateKeyFile "/etc/httpd/no_passwd_www.angus.com.key" SSLCACertificateFile "/etc/httpd/ov_bundle.cer" <Directory "/www/www.angus.com/"> Options Indexes FollowSymLinks AllowOverride all Require all granted </Directory> <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory "/usr/local/apache/cgi-bin"> SSLOptions +StdEnvVars </Directory> BrowserMatch "MSIE [2-5]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 CustomLog "/usr/local/apache/logs/ssl_request_log" \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>
安装apache+ecmall
yum install wget crontabs vim patch unzip net-snmp net-snmp-devel net-snmp-libs man net-snmp-utils strace sysstat system-config-network-tui rsync telnet parted -y yum install gcc gcc-c++ make automake autoconf -y yum install curl-devel libmcrypt-devel gd-devel libjpeg-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel pcre-devel libxslt libxslt-devel -y yum install bzip2 bzip2-devel pango pango-devel libxml2 libxml2-devel libevent-devel recode recode-devel libpng-devel libXpm-devel libxml2-devel libxslt-devel mhash-devel openssl-devel perl-CPAN ncurses-devel -y echo y | cp /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime cd /usr/local/src/ yum localinstall MySQL-*.rpm -y tar xf apr-1.4.6.tar.gz cd apr-1.4.6 ./configure --prefix=/usr/local/apr make && make install cd ../ tar xf apr-util-1.5.1.tar.gz cd apr-util-1.5.1 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ make && make install cd ../ tar xf httpd-2.4.4.tar.gz cd httpd-2.4.4 ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-mpms-shared=all --with-mpm=event make && make install grep -o "/var/run/httpd.pid" /etc/httpd/httpd.conf || sed -i '/Listen 80/ a\PidFile "/var/run/httpd.pid"' /etc/httpd/httpd.conf sed -i '/^User /s#daemon#www#g' /etc/httpd/httpd.conf sed -i '/^Group /s#daemon#www#g' /etc/httpd/httpd.conf grep "^AddType application/x-httpd-php" /etc/httpd/httpd.conf || sed -i '/AddType application\/x-gzip .gz .tgz/ a\ AddType application/x-httpd-php .php\n AddType application/x-httpd-php-source .phps' /etc/httpd/httpd.conf grep "DirectoryIndex" /etc/httpd/httpd.conf | grep "index.php" [ $? -eq 0 ] || sed -i '/DirectoryIndex/s#index.html#index.html index.php#' /etc/httpd/httpd.conf cat >> /etc/init.d/httpd <<-'EOF' #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd.pid # 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=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/local/apache/bin/apachectl httpd=/usr/local/apache/bin/httpd prog=httpd pidfile=/var/run/httpd.pid lockfile=/var/lock/subsys/httpd RETVAL=0 TOP_TIMEOUT=${STOP_TIME-10} start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL EOF chmod +x /etc/init.d/httpd chkconfig --add httpd chkconfig --level 35 httpd on service httpd start cd ../ echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local.conf tar zxvf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr make && make install cd ../ tar xf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8 ./configure make && make install cd ../ tar xf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure make && make install cd ../ ldconfig tar xf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 ./configure make && make install cd ../ tar xf php-5.3.26.tar.gz cd php-5.3.26 ./configure --prefix=/usr/local/php-5.3.26 \ --with-config-file-path=/usr/local/php-5.3.26/etc \ --with-config-file-scan-dir=/usr/local/php-5.3.26/etc/conf.d \ --with-apxs2=/usr/local/apache/bin/apxs \ --with-libdir=lib64 \ --with-pear \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-zlib-dir \ --with-iconv \ --with-mcrypt \ --with-mhash \ --with-mysql \ --with-pdo-mysql \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-openssl \ --with-xsl \ --with-recode \ --enable-sockets \ --enable-soap \ --enable-mbstring \ --enable-gd-native-ttf \ --enable-zip \ --enable-xml \ --enable-bcmath \ --enable-calendar \ --enable-shmop \ --enable-dba \ --enable-wddx \ --enable-sysvsem \ --enable-sysvshm \ --enable-sysvmsg \ --with-tsrm-pthreads \ --disable-debug make && make install ln -sv /usr/local/php-5.3.26/ /usr/local/php cp php.ini-production /usr/local/php/etc/php.ini sed -i '/^;date.timezone/ a\date.timezone = Asia/Hong_Kong' /usr/local/php/etc/php.ini echo "PATH=\$PATH:/usr/local/php/bin" > /etc/profile.d/php.sh source /etc/profile echo "/usr/local/php/lib" > /etc/ld.so.conf.d/php.conf ldconfig ln -sv /usr/local/php/include/ /usr/include/php mkdir /usr/local/php/etc/conf.d cd ../ tar xf xcache-3.0.1.tar.gz cd xcache-3.0.1 /usr/local/php/bin/phpize ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config make && make install cp xcache.ini /usr/local/php/etc/conf.d/ sed -i '/^extension/ s#xcache.so#/usr/local/php-5.3.26/lib/php/extensions/no-debug-zts-20090626/xcache.so#' /usr/local/php/etc/conf.d/xcache.ini cd ../
这样安装ecmall的apache环境已经安装好了,在安装过程中我第一次安装的是php5.3.25,装xcache会提示下面告警:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php-5.3.25/lib/php/extensions/no-debug-non-zts-20090626/xcache.so' - /usr/local/php-5.3.25/lib/php/extensions/no-debug-non-zts-20090626/xcache.so: undefined symbol: sapi_globals in Unknown on line 0 PHP 5.3.25 (cli) (built: Jul 3 2013 23:17:32)
ecmal安装注意下面几点
chmod o+w data/ chmod o+w temp/ chmod o+w external/widgets/ chmod 777 temp/caches/ CREATE DATABASE `www.ecmall.com` /*!40100 CHARACTER SET utf8 COLLATE 'utf8_general_ci' */ GRANT ALL PRIVILEGES ON *.* TO 'ecmall'@'x.x.x.x' IDENTIFIED BY 'ecmall' WITH GRANT OPTION;