Elton's Blog

PHP

Snow Leopard安装PHP+MySQL+Apache

by Elton on 2009年09月30日, under Mac, PHP

其实Snow Leopard已经内置了apache 2.2.11和php 5.3.0,只是默认都没有打开。

感谢网友Jerry的提醒,如果没有安装过XCode的朋友,在执行下面这些步骤之前,请先安装XCode,XCode在Snow Leopard的光盘里面自带了,你也可以去Apple官方网站,注册一个帐号去下载最新版本的。

1. 启用Apache
进入系统偏好->共享,勾选web共享后即可开启Apache

2. 配置PHP

  1.  
  2. sudo vim /etc/apache2/httpd.conf
  3.  

  1.  
  2. #LoadModule php5_module        libexec/apache2/libphp5.so
  3.  

把前面的#去掉。

  1.  
  2. sudo cp /etc/php.ini.default /etc/php.ini
  3.  

3. 安装MySQL
MySQL网站下载最新的MySQL的dmg,按照普通程序安装即可。

  1.  
  2. sudo vim /etc/php.ini
  3.  

将mysql.default_socket的值改为:“/tmp/mysql.sock”
将mysql.default_port的值改为:3306

重启Apache

  1.  
  2. sudo apachectl restart
  3.  

4.测试
在/Library/WebServer/Documents下面建立一个test.php,里面写入:

  1.  
  2. <?php
  3. ?>
  4.  

然后访问http://localhost/test.php,可以看到mysql都已经配置好了。

5.增加mcrypt扩展支持
先下载,libmcrypt,然后在终端定位到目录里面

再在终端输入:

  1.  
  2. ./configure –disable-posix-threads –enable-static
  3. make
  4. sudo make install
  5.  

下载php的源码包,然后解包,在终端用cd定位到里面的ext/mcrypt目录

终端输入:

  1.  
  2. phpize
  3. ./configure
  4. make
  5. cd modules
  6. sudo cp mcrypt.so /usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so
  7.  

然后在终端输入:

  1.  
  2. sudo pico /etc/php.ini
  3.  

在extensions那里加上:

extension=mcrypt.so

当然,别忘记把extension_dir=后面的改为:

“/usr/lib/php/extensions/no-debug-non-zts-20090626/”

然后,重启apache

  1.  
  2. sudo apachectl restart
  3.  

至此,就在Snow Leopard下成功安装了PHP,MySQL和Apache了。

13 Comments :, , , more...

升级内核后spawn-fcgi无法启动

by Elton on 2009年09月17日, under Linux, PHP

今天升级Linux内核到2.6.30-r5。但是升级重启后,发现blog打不开了。Nginx报502的Gateway错误。

第一反应就是起到php解析作用的spawn-fcgi没有启动起来。于是尝试再次手动启动。 但是发现怎么也启动不起来。

开始以为是因为升级内核引起的,于是退回以前的内核版本,结果一样。始终无法启动spawn-fcgi的fastcgi服务。说明不是内核的问题。 检查Nginx日志和系统日志都没有什么有价值的信息。

在一筹莫展的时候,突然注意到启动spawn-fcgi的命令中包含了php-cgi这个命令。 会不会是php的问题呢。 后来手工直接执行,发现确实php有问题。 重新使用emerge -av php编译安装php后,终于可以启动spawn-fcgi了。 网站也一切正常了。

出现问题的原因应该是以前使用emerge升级过系统,部分升级影响了php所需要调用的文件,当时没有重新启动,升级后的影响没有马上生效。 这次升级内核重启后,那些升级都生效了,结果问题就暴露出来了。

以后出现问题还是不要着急,任何环节都有可能出现问题。 而且有时候可能被问题的表象所蒙蔽,比如这次升级就误认为是因为内核的问题,但是其实后来证明根本跟内核没有关系。 出现问题还是要不放过任何可能出现问题的地方,即使你觉得不可能出现问题的地方也要去测试一下,逐一排查,最终肯定可以找到问题的原因的。

Gentoo下安装PHP和Nginx请参考之前发表的帖子

Leave a Comment :, , more...

Gentoo下安装Nginx+php

by Elton on 2009年07月19日, under Linux, PHP

使用nginx(engin x)和spawn-fcgi来共同支持php

安装nginx

  1. emerge -av nginx

安装spawn-fcgi

  1. emerge -av spawn-fcgi

启动spawn-fcgi

  1. spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -C 10

a 表示绑定的ip地址
p 表示端口号
f 表示fcgi的应用程序,在这里是制定php的cgi版本的程序
C 表示spawn的child的个数

执行netstat检查spwan-fcgi是否正常启动,可以看到9000端口是否已经开始监听

  1. netstat -tnpl

配置nginx

编辑nginx.conf文件

  1. user nginx nginx;
  2. worker_processes 8;
  3. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
  4.  
  5. error_log /var/log/nginx/error_log info;
  6.  
  7. #Specifies the value for maximum file descriptors that can be opened by this process.
  8. worker_rlimit_nofile 51200;
  9.  
  10. events {
  11.         worker_connections  8192;
  12.         use epoll;
  13. }
  14.  
  15. http {
  16.         include         /etc/nginx/mime.types;
  17.         default_type    application/octet-stream;
  18.  
  19.         log_format main
  20.                 ‘$remote_addr – $remote_user [$time_local] ‘
  21.                 ‘"$request" $status $bytes_sent ‘
  22.                 ‘"$http_referer" "$http_user_agent" ‘
  23.                 ‘"$gzip_ratio"’;
  24.  
  25.         client_header_timeout   10m;
  26.         client_body_timeout     10m;
  27.         client_max_body_size 50m; #最大允许上传50M的附件
  28.         send_timeout            10m;
  29.  
  30.         connection_pool_size            512;
  31.         client_header_buffer_size       8k;
  32.         large_client_header_buffers     4 4k;
  33.         request_pool_size               1024k;
  34.  
  35.         gzip on;
  36.         gzip_min_length 1k;
  37.         gzip_buffers    4 8k;
  38.         gzip_http_version  1.1;
  39.         gzip_types   text/plain application/x-javascript text/css application/xml;
  40.  
  41.         output_buffers  1 1024k;
  42.         postpone_output 1460;
  43.  
  44.         sendfile        on;
  45.         tcp_nopush      on;
  46.         tcp_nodelay     on;
  47.  
  48.         keepalive_timeout       75 20;
  49.         ignore_invalid_headers  on;
  50.         index index.html;
  51.  
  52.         include /data/www/vhosts/*.conf;

其中,worker_processes表示worker进程的个数,一般跟CPU个数相同
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000
表示每个CPU处理一个worker

虚拟主机配置

  1. server {
  2.         index           index.htm index.html index.php
  3.         server_name     blog.domain.com;
  4.         access_log      /var/log/nginx/domain.access_log main;
  5.         error_log       /var/log/nginx/domain.error_log info;
  6.         root            /data/www/domain/htdocs/blog;
  7.  
  8.         location / {
  9.                 if (-f $request_filename/index.html){
  10.                         rewrite (.*) $1/index.html break;
  11.                 }
  12.  
  13.                 if (-f $request_filename/index.php){
  14.                         rewrite (.*) $1/index.php;
  15.                 }
  16.  
  17.                 if (!-f $request_filename){
  18.                         rewrite (.*) /index.php;
  19.                 }
  20.         }
  21.  
  22.         location ~ .*.php$ {
  23.                 include /etc/nginx/fastcgi_params;
  24.                 fastcgi_index index.php;
  25.                 fastcgi_pass  127.0.0.1:9000;
  26.                 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        }
  27.  
  28.          location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js)$ {
  29.                 expires 30d;
  30.         }
  31. }

上面的配置以wordpress的静态url为例
其中,location ~ .*.php$ 部分是配置php的fcgi
expires,是设置静态资源的缓存时间
rewrite部分是设置wordpress静态url时候需要用到的rewrite

Nginx日常维护

Nginx 支持下表中的信号:
信号名 作用描述
TERM, INT 快速关闭程序,中止当前正在处理的请求
QUIT 处理完当前请求后,关闭程序
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

如要重新加载配置文件就使用如下命令

  1. #kill -HUP <pid>

pid是nginx的进称号,通过netstat -tnpl可以查到

Nginx 监控

通过在配置文件中加入:

  1. location ~ ^/status/ {
  2.             stub_status on; #Nginx 状态监控配置
  3.             access_log off;
  4.          }

就可以使用http://yourdomain.com/stauts监控nginx的状态。
可以看到类似这样的信息

  1. Active connections: 70
  2. server accepts handled requests
  3.  14553819 14553819 19239266
  4. Reading: 0 Writing: 3 Waiting: 67
  • active connections – 当前 Nginx 正处理的活动连接数.
  • server accepts handled requests — 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )
  • reading — nginx 读取到客户端的 Header 信息数
  • writing — nginx 返回给客户端的 Header 信息数。
  • waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

参考:
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/index.html
http://wiki.nginx.org/NginxChs
http://blog.chinaunix.net/u/12909/showart_1831422.html

Leave a Comment :, , , more...

CentOS环境中lighttpd+php+fastcgi+eAccelerator安装配置

by Elton on 2009年06月21日, under Linux, PHP

Lighttpd 作为新一代的web server,以小巧(不到1M的大小)、快速而著称,因为服务器上安装了rails、java,并以lighttpd为前端代理服务器,不想再部署apache了,所以直接使用lighttpd来部署,顺便看一下性能如何。

本文主要介绍在CentOS下,配置一套用lighttp作为web server的php环境

· 安装Lighttpd
从http://www.lighttpd.net/download/下载源码
安装前先检查pcre是否安装,需要pcre和pcre-devel两个包。 用yum search pcre\*检查,如果都是installed就是都安装了。否则安装缺少的包。

  1. yum install pcre-devel
  2.  
  3. tar xzvf lighttpd-1.4.23.tar.gz
  4. cd lighttpd-1.4.23
  5. ./configure –prefix=/usr/local/lighttpd
  6.  

configure完毕以后,会给出一个激活的模块和没有激活模块的清单,可以检查一下,是否自己需要的模块都已经激活,在enable的模块中一定要有“mod_rewrite”这一项,否则重新检查pcre是否安装。然后编译安装:

  1. make && make install

编译后配置:

  1.  
  2. cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd
  3. mkdir /etc/lighttpd
  4. cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf
  5.  

如果你的Linux是RedHat/CentOS,那么:

  1.  
  2. cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd
  3.  

如果你的Linux是SuSE,那么:

  1.  
  2. cp doc/rc.lighttpd /etc/init.d/lighttpd
  3.  

其他Linux发行版本可以自行参考该文件内容进行修改。然后修改/etc/init.d/lighttpd,把

  1.  
  2. lighttpd="/usr/sbin/lighttpd"
  3.  

改为

  1.  
  2. lighttpd="/usr/local/lighttpd/sbin/lighttpd"
  3.  

此脚本用来控制lighttpd的启动关闭和重起:

  1.  
  2. /etc/init.d/lighttpd start
  3. /etc/init.d/lighttpd stop
  4. /etc/init.d/lighttpd restart
  5.  

如果你希望服务器启动的时候就启动lighttpd,那么:

  1.  
  2. chkconfig lighttpd on
  3.  

这样lighttpd就安装好了,接下来需要配置lighttpd。

配置Lighttpd

修改/etc/lighttpd/lighttpd.conf
1)server.modules
取消需要用到模块的注释,mod_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,mod_compress,mod_accesslog是一般需要用到的。

2)server.document-root, server.error-log,accesslog.filename需要指定相应的目录

3)用什么权限来运行lighttpd
server.username = “nobody”
server.groupname = “nobody”
从安全角度来说,不建议用root权限运行web server,可以自行指定普通用户权限。

4)静态文件压缩
compress.cache-dir = “/tmp/lighttpd/cache/compress”
compress.filetype = (“text/plain”, “text/html”,”text/javascript”,”text/css”)
可以指定某些静态资源类型使用压缩方式传输,节省带宽,对于大量AJAX应用来说,可以极大提高页面加载速度。

5)配置ruby on rails

最简单的配置如下:

  1.  
  2. $HTTP["host"] == "www.xxx.com" {
  3.  server.document-root = "/yourrails/public"
  4.  server.error-handler-404 = "/dispatch.fcgi"
  5.  fastcgi.server = (".fcgi" =>
  6.     ("localhost" =>
  7.       ("min-procs" => 10,
  8.        "max-procs" => 10,
  9.        "socket" => "/tmp/lighttpd/socket/rails.socket",
  10.        "bin-path" => "/yourrails/public/dispatch.fcgi",
  11.        "bin-environment" => ("RAILS_ENV" => "production")
  12.       )
  13.     )
  14.  )
  15. }
  16.  

即由lighttpd启动10个FCGI进程,lighttpd和FCGI之间使用本机Unix Socket通信。

如果想指定www.abc.com以及所有二级域名,则需要把第一行改为
$HTTP[”host”] =~ “(^|\.)abc\.com” {

}

如果要设置代理,比如lighttpd和tomcat整合,tomcat放在lighttpd后面,则需要通过代理访问tomcat

$HTTP["host"] =~ “www.domain.cn” {
proxy.server = ( “” => ( “localhost” => ( “host”=> “127.0.0.1″, “port”=> 8080 ) ) )
}

则www.domain.cn为主机的网址都交给tomcat处理,tomcat的端口号为8080. 在tomcat的虚拟主机中,需要捕获www.domain.cn这个主机名,设置这个虚拟主机。这里的host都是跟tomcat里面的虚拟主机对应的。

· 安装支持fastcgi的PHP
安装PHP所需的相关类库
curl

  1.  
  2. wget http://curl.cs.pu.edu.tw/download/curl-7.19.5.tar.bz2
  3.  
  4. tar xvjf curl-7.19.5.tar.bz2
  5. cd curl-7.19.5
  6. ./configure –prefix=/usr/local/curl
  7. make
  8. make install
  9.  

gettext

  1.  
  2. wget ftp://ftp.ntu.edu.tw/pub/gnu/gnu/gettext/gettext-0.17.tar.gz
  3. tar xvzf gettext-0.17.tar.gz
  4. cd gettext-0.17
  5. ./configure –prefix=/usr/local/gettext
  6. make
  7. make install
  8.  

zlib

  1.  
  2. wget http://kent.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz
  3. tar xvzf zlib-1.2.3.tar.gz
  4. cd zlib-1.2.3
  5. ./configure –prefix=/usr/local/zlib
  6. make && make install
  7.  

libpng

  1.  
  2. wget http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/l/li/libpng/libpng-1.2.9.tar.gz
  3. tar xvzf libpng-1.2.9.tar.gz
  4. cd libpng-1.2.9
  5. ./configure –prefix=/usr/local/libpng
  6. make && make install
  7.  

jpeg

  1.  
  2. wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz
  3. tar xvzf jpegsrc.v6b.tar.gz
  4. cd jpeg-6b/
  5. ./configure –prefix=/usr/local/jpeg6
  6. make
  7.  
  8. mkdir /usr/local/jpeg6/bin
  9. mkdir -p /usr/local/jpeg6/bin
  10. mkdir -p /usr/local/jpeg6/man/man1
  11. mkdir -p /usr/local/jpeg6/lib
  12. mkdir -p /usr/local/jpeg6/include
  13. make install-lib
  14. make install
  15.  

freetype

  1.  
  2. wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.9.tar.gz
  3. tar xvzf freetype-2.3.9.tar.gz
  4. cd freetype-2.3.9
  5. ./configure –prefix=/usr/local/freetype2
  6. make
  7. make install
  8.  

gd

  1.  
  2. wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
  3. tar xvzf gd-2.0.35.tar.gz
  4. cd gd-2.0.35
  5. ./configure –prefix=/usr/local/gd2 –with-zlib=/usr/local/zlib/ –with-png=/usr/local/libpng/ –with-jpeg=/usr/local/jpeg6/ –with-freetype=/usr/local/freetype2/
  6. make
  7. 如果第一次make出错,试着再make一次,我就是这样,第二次就对了。
  8. make install
  9.  

PHP

  1.  
  2. tar xvzf php-5.2.10.tar.gz
  3. cd php-5.2.10
  4. ./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-pdo-mysql=/usr/local/mysql –with-jpeg-dir=/usr/local/jpeg6/ –with-png-dir=/usr/local/libpng/ –with-gd=/usr/local/gd2/ –with-freetype-dir=/usr/local/freetype2/  –with-zlib-dir=/usr/local/zlib –with-curl=/usr/local/curl –with-gettext=/usr/local/gettext –enable-fastcgi –enable-zend-multibyte –with-config-file-path=/etc –enable-discard-path –enable-force-cgi-redirect
  5. make
  6. make install
  7. cp php.ini-dist /etc/php.ini
  8.  
  9. 可以使用php -m查看你安装的模块
  10.  

eAccelerator
eAccelerator是一个开源的PHP加速器

  1.  
  2. wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
  3. tar xjvf eaccelerator-0.9.5.3.tar.bz2
  4. cd eaccelerator-0.9.5.3
  5. export PHP_PREFIX="/usr/local/php"
  6. $PHP_PREFIX/bin/phpize
  7. ./configure –enable-eaccelerator=shared –with-php-config=$PHP_PREFIX/bin/php-config
  8. make
  9. make install
  10.  

执行好后,会提示安装到的路径,下面会用到,如我的被安装到这里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
编辑php.ini中的内容
vim /etc/php.ini

  1.  
  2. cgi.fix_pathinfo = 1
  3.  
  4. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
  5. eaccelerator.shm_size="16"
  6. eaccelerator.cache_dir="/tmp/eaccelerator"
  7. eaccelerator.enable="1"
  8. eaccelerator.optimizer="1"
  9. eaccelerator.check_mtime="1"
  10. eaccelerator.debug="0"
  11. eaccelerator.filter=""
  12. eaccelerator.shm_max="0"
  13. eaccelerator.shm_ttl="0"
  14. eaccelerator.shm_prune_period="0"
  15. eaccelerator.shm_only="0"
  16. eaccelerator.compress="1"
  17. eaccelerator.compress_level="9"
  18.  

如果一切顺利,你可以通过下面命令来验证是否安装成功

  1.  
  2. $ php -v
  3. PHP 5.2.10 (cli) (built: Jun 20 2009 23:32:09)
  4. Copyright (c) 1997-2009 The PHP Group
  5. Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
  6.     with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
  7.  

修改/etc/lighttpd/lighttpd.conf文件,添加下面的配置
vim /etc/lighttpd/lighttpd.conf

  1.  
  2. fastcgi.server             = ( ".php" =>
  3.                                ( "localhost" =>
  4.                                  (
  5.                                    "socket" => "/tmp/php-fastcgi.socket",
  6.                                    "bin-path" => "/usr/local/php/bin/php-cgi"
  7.                                  )
  8.                                )
  9.                             )
  10.  

重启lighttpd

  1.  
  2. /etc/init.d/lighttpd restart
  3.  

写一个php测试文件在lighttpd的网站目录里,测试php是否安装成功

Leave a Comment :, , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...