安裝過程中所需要的軟件:
apr-1.5.1.tar.gz
apr-util-1.5.4.tar.bz2
httpd-2.4.10.tar.bz2
mysql-5.5.33-linux2.6-x86_64.tar.gz
php-5.3.29.tar.bz2
一. 編譯安裝apache
1. 編譯安裝apr
# tar zxvf apr-1.5.1.tar.gz # cd apr-1.5.1 # ./configure --prefix=/usr/local/apr # make && make install
2. 編譯安裝apr-util
# tar jxvf apr-util-1.5.4.tar.bz2 # cd apr-util-1.5.4 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr # make && make install
3. 安裝依賴性軟件
# yum -y install gcc pcre-devel openssl-devel
4. 編譯安裝httpd
# tar jxvf httpd-2.4.10.tar.bz2 # cd httpd-2.4.10 # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event # make && make install
5. 編寫httpd啓動腳本/etc/init.d/httpd
# chmod +x httpd # chkconfig --add httpd # chkconfig httpd on # service httpd start
# cat /etc/init.d/httpd #!/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=${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=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 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 } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL
二. 編譯安裝mysql
1. 新建mysql用戶
# groupadd -r mysql # useradd -g mysql -r -s /sbin/nologin -M /mysqldata/data mysql # chown -R mysql:mysql /mysqldata/data
2. 安裝初始化mysql
# tar zxvf mysql-5.5.33-linux2.6-x86_64.tar.gz # mv mysql-5.5.33-linux2.6-x86_64 /usr/local # ln -sv mysql-5.5.33-linux2.6-x86_64 mysql # cd mysql # scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data # chown -R root .
3. 配置mysql配置文件my.cnf
# cd /usr/local/mysql # cp support-files/my-large.cnf /etc/my.cnf # vim /etc/my.cnf 增加一行: datadir = /mysqldata/data
4. 配置mysqld啓動腳本
# cd /usr/local/mysql # cp support-files/mysql.server /etc/init.d/mysqld # chmod +x mysqld # chkconfig --add mysqld # chkconfig mysqld on # service mysqld start
5. 配置mysql庫文件查找路徑
# vim /etc/ld.so.conf.d/mysql.conf 增加 /usr/local/mysql/lib # ldconfig
6. 配置PATH變量
# vim /etc/profile.d/mysql.sh 增加 PATH=/usr/local/mysql/bin:$PATH # . /etc/profile.d/mysql.sh
三. 編譯安裝php
1. 安裝依賴軟件
# yum -y groupinstall "Desktop Platform Development" # yum -y install bzip2-devel libmcrypt-devel # yum -y install libtool libtool-devel libtool-ltdl-devel
2. 編譯安裝php
編譯安裝php作爲apache的module,php安裝成功後會在httpd.conf中增加如下行:
LoadModule php5_module modules/libphp5.so
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config表示使用本機Mysql作爲後臺數據庫
# tar jxvf php-5.3.29.tar.bz2 # cd php-5.3.29 # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts # make && make install
3. 配置apache支持php
# vim httpd.conf 增加 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps 修改 DirectoryIndex index.html 修改前 DirectoryIndex index.php index.html 修改後
4. 編寫測試文件index.php,測試Php是否能正常使用
<?php $link = mysql_connect('127.0.0.1','root','mageedu'); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); ?>
5. 性能測試
ab -c 1000 -n 10000 http://www.tech.com/index.php This is ApacheBench, Version 2.3 <$Revision: 1604373 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.tech.com (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: Apache/2.4.10 Server Hostname: www.tech.com Server Port: 80 Document Path: /index.php Document Length: 11 bytes Concurrency Level: 1000 Time taken for tests: 13.315 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 1980000 bytes HTML transferred: 110000 bytes Requests per second: 751.04 [#/sec] (mean) Time per request: 1331.480 [ms] (mean) Time per request: 1.331 [ms] (mean, across all concurrent requests) Transfer rate: 145.22 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 2 97 224.8 44 1085 Processing: 11 282 995.7 55 13269 Waiting: 10 273 996.8 47 13269 Total: 31 379 1014.0 106 13305 Percentage of the requests served within a certain time (ms) 50% 106 66% 123 75% 130 80% 139 90% 1114 95% 1619 98% 3096 99% 5748 100% 13305 (longest request)
6. 安裝php加速器xcache
# tar zxvf xcache-3.0.4.tar.gz # cd xcache-3.0.4 # /usr/local/php/bin/phpize # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config # make && make install 安裝完成後最後一行顯示: Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20090626/ # mkdir /etc/php.d # cp xcache.ini /etc/php.d # vim /etc/php.d/xcache.ini 修改extension爲 extension = /usr/local/php/lib/php/extensions/no-debug-zts-20090626/xcache.so
7. 安裝xcache之後的性能測試:
ab -c 1000 -n 10000 http://www.tech.com/index.php This is ApacheBench, Version 2.3 <$Revision: 1604373 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.tech.com (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: Apache/2.4.10 Server Hostname: www.tech.com Server Port: 80 Document Path: /index.php Document Length: 11 bytes Concurrency Level: 1000 Time taken for tests: 6.630 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 1980000 bytes HTML transferred: 110000 bytes Requests per second: 1508.24 [#/sec] (mean) Time per request: 663.026 [ms] (mean) Time per request: 0.663 [ms] (mean, across all concurrent requests) Transfer rate: 291.63 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 122 283.1 35 1068 Processing: 5 188 544.2 50 5652 Waiting: 4 181 544.7 44 5652 Total: 17 310 660.4 89 5657 Percentage of the requests served within a certain time (ms) 50% 89 66% 115 75% 125 80% 153 90% 1093 95% 1337 98% 2374 99% 3587 100% 5657 (longest request)
二. 編譯安裝php爲獨立的服務進程php-fpm
1. 安裝依賴軟件
# yum -y install bzip2-devel libmcrypt-devel libxml2-config-devel # yum -y install libtool libtool-devel libtool-ltdl-devel
2. 編譯安裝php
# tar jxvf php-5.3.29.tar.bz2 # cd php-5.3.29 # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 # make && make install //--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd用於配置php通過socket連接非本機mysql數據庫
3. 配置php
# cd php-5.3.29 # cp php.ini-production /etc/php.ini # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod +x /etc/init.d/php-fpm # chkconfig --add php-fpm # chkconfig php-fpm on # cd /usr/local/php/etc # cp php-fpm.conf.default php-fpm.conf # vim php-fpm.conf 修改 pm.max_children = 50 //設置php-fpm進程最大數爲50 pm.start_servers = 5 //設置php-fpm初始進程數爲5 pm.min_spare_servers = 2 //設置最小空閒進程數爲2 pm.max_spare_servers = 8 //設置最大空閒進程數爲8 pid = /usr/local/php/var/run/php-fpm.pid
4. 啓動php-fpm進程
# service php-fpm start # ss -tuln | grep 9000 //驗證php-fpm tcp LISTEN 0 128 127.0.0.1:9000 *:*
5. 啓動httpd的相關模塊
//在httpd.conf中配置對於fastcgi的支持 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
6. 配置虛擬主機支持fcgi
<VirtualHost *:80> ServerAdmin root@localhost DocumentRoot "/usr/local/apache/htdocs/www.dev.com" ServerName www.dev.com ServerAlias dev.com ProxyRequests off //關閉正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/ //把以.php結尾的文件請求發送到php-fpm進程 <Directory /usr/local/apache/htdocs/www.dev.com> Options none AllowOverride none Require all granted </Directory> ErrorLog "logs/www.dev.com_error" CustomLog "logs/www.dev.com_access" combined </VirtualHost>
7. 配置httpd.conf,讓apache能識別.php文件
# vim httpd.conf 增加兩行 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps 修改 DirectoryIndex index.php index.html