Apache+fastcgi模块+APC经典配置

本文详细介绍了如何从头到尾安装并优化Apache、MySQL、PHP-FPM,包括FastCGI模块的安装、配置,以及如何在Apache中启用PHP-FPM,最终实现对PHP文件的高效处理。同时,提供了APC缓存的配置步骤,以提升网站性能。

一、前言

FastCGI是一个程序接口,它能加速公共网关接口(CGI),CGI是一种用最常见的方式使Web服务器调用应用程序的Web应用程序。按一个FastCGI工具来看,用户要求进入一个网站并使用一个专门的应用软件的话,使用FastCGI能够快3到30倍。FastCGI是Web服务器的一种插件。为了获得良好的性能,它要求对现有服务器应用程序(比如 Perl 、Tcl 脚本和C、C++程序)做细小的改动。

让我们开始来安装:

二、安装apache fastcgi模块:

下载地址:http://down.51cto.com/data/139939 (51cto下载中心)

tar -zxvf mod_fastcgi-current.tar.gz

cd mod_fastcgi
cp Makefile.AP2 Makefile
vi Makefile 修改top_dir=/usr/local/apache2  #你的apache安装路径或者直接编(apache2.0已经安装)
make
make install(这里采用apache而不采用nginx的原因是:虽然nginx有很多优点,但是对于大部分中小型网站来说,apache经过优化可以满足访问要求。)

三、安装MYSQL
tar -zxvf mysql-5.0.75.tar.gz  //解压
cd mysql-5.0.75
 ./configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charsets=all
make
make install
groupadd mysql  //创建组mysql
useradd -g mysql mysql  //创建mysql用户,把mysql加入到mysql组

cd /usr/local/mysql/
cp share/mysql/my-medium.cnf  /etc/my.cnf

cp share/mysql/mysql.server /etc/init.d/mysqld

(以上是把mysql加入系统服务)

 chown -R mysql.mysql /usr/local/mysql
 /usr/local/mysql/bin/mysql_install_db --user=mysql  //初始化数据库
 chown -R mysql.mysql /usr/local/mysql/var
 /usr/local/mysql/bin/mysqld_safe --user=mysql &  //启动数据库(或者 service mysqld restart)

四、安装php-fpm
在安装PHP-FPM编译的时候会报找不到libevent,可以执行
yum -y install libevent*
wget 
http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar zxvf libevent-1.4.12-stable.tar.gz
cd libevent-1.4.12
./configure --prefix=/usr/local/libenent
mak && make install

tar -jxvf php-5.3.3.tar.bz2
cd php-5.3.3
./configure \
--prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc \
--with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-zlib --with-curl --with-jpeg-dir --with-freetype-dir --with-png-dir \
--with-libxml-dir=/usr/ --with-gd  --with-openssl  \
--with-openssl --with-ldap --with-ldap-sasl --with-xmlrpc --without-pear \
--enable-xml --enable-gd-native-ttf \
--enable-ctype --enable-calendar --enable-inline-optimization \
--enable-magic-quotes --with-bz2 --enable-mbstring \
--enable-fpm  --enable-safe-mode  --enable-bcmath  --enable-shmop \
--enable-sysvsem --enable-mbregex  --enable-mbstring --enable-pcntl  \
--enable-zip --enable-ftp --enable-sockets --enable-soap --with-libevent-dir=/usr/local/libevent/
make && make install

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

cp -r sapi/fpm/php-fpm.conf /usr/local/php5/etc/
启动php-fpm
/etc/init.d/php-fpm start
此时启动应该会报错,说php-fpm.conf配置文件中很多选项没有打开,只要按照它的提示打开就OK。

五、修改apache,让apache支持php-fpm
vi httpd.conf
增加: LoadModule fastcgi_module modules/mod_fastcgi.so 
有的话不用添加。

在vhosts.conf文件的项目中加入以下(如果不引用vhosts.conf的话可以把下面的语句加入http.conf的最后)
    ScriptAlias /cgi-bin/ "/usr/local/php5/bin/"
    FastCgiExternalServer /usr/local/php5/bin/php-fpm -host 127.0.0.1:9000
    AddType application/x-httpd-php .php
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /cgi-bin/php-fpm

    <Directory "/usr/local/php5/bin/">
    Options -Indexes FollowSymLinks +ExecCGI
    Order allow,deny
    Allow from all
    </Directory>

#Action php5-fastcgi /cgi-bin/php-fpm中的php-fpm一定要和FastCgiExternalServer中的文件名一样.而且/cgi-bin/和前面的ScriptAlias的路径也要一样,两句放一块就是说上面的映射也就是所有的.php文件都由/cgi-bin/php-fpm 处理
 否則會出錯的.
加完以上的,安装完成了。但是php-fpm.conf配置文件需要优化(常见优化见后期给出)

注:在增加了apc.so后总是出现linux Warning: PHP Startup: Unable to load dynamic library
后怎么更改都不行,后重新编译php-5.3.3,并加上
--with-libevent-dir=/usr/local/libevent参数,一切OK!最好不要用系统默认的。
六、安装php-apc的步骤:

PHP APC提供两种缓存功能,即缓存Opcode(目标文件),我们称之为apc_compiler_cache。同时它还提供一些接口用于PHP开发人员将用户数据驻留在内存中,我们称之为apc_user_cache。我们这里主要讨论php-apc的配置。

1、执行/usr/local/php5/bin/phpize更新模块。

export PHP_PREFIX="/usr/local/php5/"

./configure --enable-apc --enable-apc-mmap --with-apxs=/usr/local/httpd-2.2.14/bin/apxs --with-php-config=$PHP_PREFIX/bin/php-config


php.ini添加如下语句

extension_dir="/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626"
extension = apc.so
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64M
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl = 3600
apc.cache_by_default = on


(注意*)php5.3.3默认没有把php.ini文件里面的short_open_tag = Off设置为short_open_tag = On ,所以我们需要把short_open_tag = Off设置为On;否则的话,访问php测试页面和php页面不显示的问题。(安装php-apc后,网站就无法显示,无法读取php变成空白页面)

最后测试,可以访问apc.php测试效果。(欲了解更多APC优化知识,可以参看http://www.perfgeeks.com/?p=298

由于水平有限 O(∩_∩)O~ ,写的不详细地方的请多指教


--------------------


fastcgi+apache+php的配置

from  http://blog.youkuaiyun.com/stopname/article/details/1254467

1.apache安装
这里安装apache1.3.x,因为在我的虚拟机上在apache2.x下怎么也安装不上mod_fastcgi
./configure /配置参数/
.make
.make install
 
2.fcgi安装
版本fcgi-2.4.0 ( http://www.fastcgi.com)可以下载
fcgi是fastcgi的开发包,需要在mod_fastcgi之前安装
这个安装很简单,而且很顺利
 
3.mod_fastcgi安装
版本mod_fastcgi-2.4.2
apache的fastcgi模块.
这里有两种安装方式(共享模块/静态模块),解压文件夹后,查看(INSTALL.txt,apache 2.x看INSTALL.AP2)里面有详细的安装说明.
我的安装如下:(共享模块方式)
cd <mod_fastcgi_dir>
<mod_fastcgi_dir>$ apxs -o mod_fastcgi.so -c *.c
<mod_fastcgi_dir>$ apxs -i -a -n fastcgi mod_fastcgi.so
 
 
4. php的编译方式
版本php5.x
./configure --enable-fastcgi --enable-force-cgi-redirect --disable-cli --with-apxs=/usr/local/apche/apxs
make
make install
安装成功后,执行
php -v 输出
PHP 5.1.4 (cgi-fcgi).
这里输出带了cgi-fcgi
 
如果编译时不加--disable-cli则输出
PHP 5.1.4 (cli).
 
5.apache配置
以上安装完后,需要配置apache来以fastcgi模式运行php程序。
假定php和fastcgi都是以apache的共享模块来安装的。
//加载模块
LoadModule php5_module        libexec/libphp5.so
LoadModule fastcgi_module     libexec/mod_fastcgi.so
 
//ln -s /var/www/php/cgi-bin/php5.fcgi /usr/local/php5-fcgi/bin/php
//以静态方式执行fastcgi 启动了10进程
FastCgiServer /var/www/php/cgi-bin/php5.fcgi -processes 10 -idle-timeout 150 -pass-header HTTP_AUTHORIZATION
<VirtualHost *:80>
 DocumentRoot /var/www/php
 ServerName  www.php.com
 
 ScriptAlias /fcgi-bin/ /var/www/php/cgi-bin/
 Options +ExecCGI
 
 AddHandler fastcgi-script .fcgi
 AddType application/x-httpd-php .php
 Action application/x-httpd-php /fcgi-bin/php5.fcgi
 <Directory /var/www/php>
 Options Indexes ExecCGI
 Order allow,deny
 allow from all
 </Directory>
</VirtualHost>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值