PHP5.4.0发布了,添加了很多新特性,于是今天在ubuntu下编译安装了PHP5.4+Nginx1.0.13+Memcache,让PHP

以fastcgi模式运行。MySQL就不装了用以前的5.1版本的。附张图:

ubuntu编译安装PHP5.4.0+Nginx(FPM/FastCGI)_今天ubuntu编译安装PHP5.4.0+Nginx(FPM/FastCGI)_ubuntu_02

安装编译过程遇到许多小问题,折腾了一个晚上,现在整理下:

(1):卸载以前通过源安装的php5.3.2和apache

sudo apt-get –purge remove apache2
sudo apt-get –purge remove apache2-common
sudo apt-get –purge remove apache2-utils
sudo apt-get autoremove apache2
 
(–purge是不保留配置文件的意思,这个绝对不能少)
然后再sudo apt-get install apache2就可以了
PS:如果不行的话,就把关于apache2的所有东西强删掉,因为我执行这些命令之前就已经把所有的都干掉。
 
删掉/etc/apache2 文件夹:sudo rm -r /etc/apache2
删掉/var/www文件夹:sudo rm -r /var/www
删掉/etc/init.d/apache2文件:sudo rm -r /etc/init.d/apache2
 
然后就是卸载PHP5了,方法如下:
sudo apt-get –purge remove libapache2-mod-php5 php5 php5-gd php5-mysql
sudo apt-get autoremove php5
 
下面是卸载MYSQL的方法:
终端下输入命令:
1 sudo apt-get autoremove –purge mysql-server-5.0
2 sudo apt-get remove mysql-server
3 sudo apt-get autoremove mysql-server
4 sudo apt-get remove mysql-common (非常重要)
 
下面这句是清楚残留信息。
dpkg -l |grep ^rc|awk ’{print $2}’ |sudo xargs dpkg -P

(2):安装Nginx1.0.13

然后sudo apt-get install nginx

(3):准备工作

1,从php官网下载 php5.4.tar.gz 解压:tar -zxvf php-5.4.0tar.gz  然后cd php-5.4.0

2, ./buildconf --force 报错(好像是这样):You need autoconf version 2.59 or higher 要安装autoconf2.59更高

版本,方法如下:

     1,先卸载原先的autoconf: sudo apt-get --purge remove autoconf, 然后 sudo apt-get autoremove autoconf

     2,安装autoconf2.62:

 
# cd /home/cyrec
# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz
# cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
# tar -zvxf autoconf-2.62.tar.gz
# cd autoconf-2.62/
# ./configure && make && make install
然后再./buildconf --force   (如果php出现找不到autoconf的错误执行export PHP_AUTOCONF="/(autoconf路径)/autoconf2.62" )       

(4):开始编译安装配置PHP5.4.0

    ./configure --prefix=/opt/php5 --with-config-file-path=/opt/php5/etc --with-curl --with-pear --with-gd --with-jpeg-dir --with-png-dir --with-zlib --with-xpm-dir --with-freetype-dir --with-t1lib --with-mcrypt --with-mhash --with-mysql --with-mysqli --enable-pdo --with-pdo-mysql --with-openssl --with-xmlrpc --with-xsl --with-gettext --with-fpm-user=www-data --with-fpm-group=www-data --enable-fpm --enable-exif --enable-wddx --enable-zip --enable-bcmath -with-bz2 --enable-calendar --enable-ftp --enable-mbstring --enable-soap --enable-sockets --enable-shmop --enable-dba --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-debug --enable-maintainer-zts --enable-embed

最后几个参数解释下:

 

--enable-debug

顾名思义,它的作用是激活调试模式。它将激活PHP源码中几个非常关键的函数,最典型的功能便是在每一个请求结束后给出这一次请求中内存的泄漏情况。

 

--enable-maintainer-zts

激活php的线程安全机制(Thread Safe Resource Manager(TSRM)/Zend Thread Safety(ZTS)),使我们开发出的程序是线程安全的。

 

--enable-embed

它主要用在你做php的嵌入式开发的场景中。平时我们把php作为apache的一个module进行编译,得到libphp5.so,而这个选项便使php编译后得到一个与我们设定的SAPI相对应的结果。

 

当中遇到的错误和解决方法如下:

1) Configure: error: xml2-config not found. Please check your libxml2 installation.

Solutions :
yum install libxml2 libxml2-devel (For Redhat & Fedora)

# aptitude install libxml2-dev (For ubuntu)

2) Checking for pkg-config… /usr/bin/pkg-config
configure: error: Cannot find OpenSSL’s <evp.h> or <hmac.h>

Solutions :

yum install openssl openssl-devel

on some systems, like Ubuntu Feisty, you might need to use this command:

sudo apt-get install openssl-dev

sudo apt-get install libcurl3-openssl-dev

 

3) Configure: error: Please reinstall the BZip2 distribution

Solutions :
yum install bzip2 bzip2-devel

4) Configure: error: Please reinstall the libcurl distribution -
easy.h should be in /include/curl/

Solutions :
yum install curl curl-devel (For Redhat & Fedora)

apt-get install curl
apt-get install libcurl3
apt-get install libcurl4-gnutls-dev(For Ubuntu)


5) Configure: error: libjpeg.(also) not found.

Solutions :
yum -y install gd
yum -y install gd-devel
yum install libjpeg libjpeg-devel

6) Configure: error: libpng.(also) not found.

Solutions :
yum install libpng libpng-devel
apt-get install libpng12-dev

7) Configure: error: freetype.h not found.
Solutions :
yum install freetype-devel

8) Configure: error: Unable to locate gmp.h

Solutions :
yum install gmp-devel

9) Configure: error: Cannot find MySQL header files under /usr.
Note that the MySQL client library is not bundled anymore!

Solutions :
yum install mysql-devel (For Redhat & Fedora)

# apt-get install libmysql++-dev (For Ubuntu)

10) Configure: error: Please reinstall the ncurses distribution

Solutions :
yum install ncurses ncurses-devel

11) Checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!

Solutions :
yum install unixODBC-devel

12) Configure: error: Cannot find pspell

Solutions :
yum install pspell-devel

13) configure: error: mcrypt.h not found. Please reinstall libmcrypt.

Solutions :
yum install libmcrypt libmcrypt-devel (For Redhat & Fedora)

# apt-get install libmcrypt-dev

14) Configure: error: snmp.h not found. Check your SNMP installation.

Solutions :

yum install net-snmp net-snmp-devel

编译完成后make && make install,将php5所在的目录也加到bash的PATH中去:

echo 'if [ -d "/opt/php5/bin" ] && [ -d "/opt/php5/sbin" ]; then
    PATH="$PATH:/opt/php5/bin:/opt/php5/sbin"
fi' >> /etc/bash.bashrc

当前会话也加上:

export PATH="$PATH:/opt/php5/bin:/opt/php5/sbin"

准备PHP5的日志文件目录:

mkdir /var/log/php-fpm chown -R www-data:www-data /var/log/php-fpm

准备PHP的配置文件:

cp -f php.ini-production /opt/php5/etc/php.ini

chmod 644 /opt/php5/etc/php.ini

cp /opt/php5/etc/php-fpm.conf.default /opt/php5/etc/php-fpm.conf

注意: PHP5的配置文件在/opt/php5/etc/php.ini, php-fpm的配置文件在/opt/php5/etc/php-fpm.conf.

设置php-fpm启动管理文件,并让PHP在ubuntu启动时自动运行(下面地sapi是PHP5.4.0安装包中的):

cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm

update-rc.d -f php-fpm defaults

设置PHP的自动日志分割和压缩: vi /etc/logrotate.d/php-fpm

加入下面的内容保存退出:

/var/log/php-fpm/*.log {
 weekly
 missingok
 rotate 52
 compress
 delaycompress
 notifempty
 create 640 www-data www-data
 sharedscripts
 postrotate
  [ ! -f /opt/php5/var/run/php-fpm.pid ] || kill -USR1 `cat /opt/php5/
/var/run/php-fpm.pid`
 endscript
}

(5):安装APC,Memcache拓展:

wget http://pecl.php.net/get/APC-3.1.9.tgz
tar -zxf APC-3.1.9.tgz
cd APC-3.1.9
/opt/php5/bin/phpize
./configure --with-php-config=/opt/php5/bin/php-config
make
make install
然后打开php配置文件:vi /opt/etc/php.ini
在末尾加上:
extension = apc.so
apc.enabled = 1
apc.shm_size = 128M
apc.shm_segments=1
apc.write_lock = 1
apc.rfc1867 = On
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
;# Optional, for "[apc-warning] Potential cache slam averted for key... errors"
;apc.slam_defense = Off
PS:虽然能正常编译安装,但加载APC启动fpm时会报错,看来APC拓展在php5.4下不兼容,虽然能使用,但运行PHP程序
会出core,而且另外两款PHP缓存拓展eacc和xcache在php5.4下也不能编译安装,所以现在php5.4还不能用在生产
环境,只能尝鲜玩玩。
安装Memcache只要在pecl上找到相应的安装包按上面步骤重做一遍。

顺便修改php.ini的时区:
date.timezone = Asia/Shanghai

(6):配置php-fpm和nginx
php-fpm:
vi /opt/php5/etc/php-fpm.conf
主要是下面的参数需要改下:
  1. pid = run/php-fpm.pid  //这个必去将前面的;注释去掉(一开始没去掉结果出502错误)
  2.  
  3. error_log = log/php-fpm.log
  4.  
  5. log_level = notice
  6.  
  7. ;emergency_restart_threshold = 0
  8.  
  9. ;emergency_restart_interval = 0
  10.  
  11. ;process_control_timeout = 0
  12.  
  13. ;daemonize = yes
  14.  
  15. ;[www]
  16.  
  17. ;prefix = /path/to/pools/$pool
  18.  
  19. listen = 127.0.0.1:9000
  20.  
  21. ; listen.backlog = -1
  22.  
  23. listen.allowed_clients = 127.0.0.1
  24. listen.owner = www-data
  25. listen.group = www-data
  26. listen.mode = 0666
  27.  
  28. user = www-data
  29. group = www-data
  30.  
  31. pm = dynamic
  32.  
  33. pm.max_children = 50
  34.  
  35. pm.start_servers = 20
  36.  
  37. pm.min_spare_servers = 5
  38.  
  39. pm.max_spare_servers = 35
  40.  
  41. pm.max_requests = 500
然后配置nginx:
vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events 
{
    use epoll;
    worker_connections  1024;
}
http
{
 include       mime.types;
 default_type  application/octet-stream;

 #charset  gb2312;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 32k;
 large_client_header_buffers 4 32k;
 
 client_max_body_size 8M;

 sendfile on;
 tcp_nopush     on;
 keepalive_timeout 60;
 tcp_nodelay on;

 fastcgi_connect_timeout 300s;
 fastcgi_send_timeout 300s;
 fastcgi_read_timeout 300s;
 fastcgi_buffer_size 64k;
 fastcgi_buffers 4 64k;
 fastcgi_busy_buffers_size 128k;
 fastcgi_temp_file_write_size 128k;

 gzip on;
 gzip_min_length  1k;
 gzip_buffers     4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types text/plain application/x-javascript text/css application/xml;
 gzip_vary on;

 #limit_zone  crawler  $binary_remote_addr  10m;

 server
 {
    listen       80;               #/*监听端口*/
    server_name  localhost;        #/*服务器名称*/
    index index.html index.htm index.php;  #/*缺省主页名称*/
    root  /var/www;  #/*网站根目录,也可以采用下面内容*/
#也可以采用相对路径,下面注释部分*/
    #location / {
       #         root   html;
       #         index  index.html index.htm;
       # }
 
    #limit_conn   crawler  20;   
   
    #通过FastCGI方式支持PHP,php页面由fastcgi代理处理,这也是反向代理的一个应用,这里可以是jsp/asp等脚本。
    location ~ .*\.(php|php5)?$
    {     
        include /etc/nginx/fastcgi_params;#为FastCGI模块设置了基本的环境变量
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/$fastcgi_script_name;
        #include fcgi.conf;       #/*fastcgi配置文件,修改为以下内容*/
       }
   
    #对于某一类型的文件,设置过期时间,静态的页面通常设置长一点。
    #静态文件,nginx自己处理
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
         expires      30d;
       }
 
#日志的格式
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /var/log/nginx/access.log  access;
      }
}


然后保存退出,重启fpm和nginx 
/etc/init.d/php-fpm restart 
/etc/init.d/nginx restart 
安装完成:
root@Cyrec-desktop:~# php-fpm -v
PHP 5.4.0 (fpm-fcgi) (built: Mar 12 2012 18:13:13) (DEBUG)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

一些参考资料:

http://www.cslog.cn/Content/ubuntu-10-04-nginx-0-8-52-php-fpm-5-3-3-apc-mysql-i/

http://www.laruence.com/2009/07/28/1030.html

http://www.frostsky.com/2011/11/php-configure-error/

http://letle.iteye.com/blog/625728

http://kangxiaowei.com/archives/2398.html

http://wenku.baidu.com/view/2ae270e9551810a6f52486ea.html