Tag: PHP
Snow Leopard安装PHP+MySQL+Apache
by Elton on 九.30, 2009, 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 | sudo vim /etc/apache2/httpd.conf |
在
1 | #LoadModule php5_module libexec/apache2/libphp5.so |
把前面的#去掉。
1 | sudo cp /etc/php.ini.default /etc/php.ini |
3. 安装MySQL
从MySQL网站下载最新的MySQL的dmg,按照普通程序安装即可。
1 | sudo vim /etc/php.ini |
将mysql.default_socket的值改为:“/tmp/mysql.sock”
将mysql.default_port的值改为:3306
重启Apache
1 | sudo apachectl restart |
4.测试
在/Library/WebServer/Documents下面建立一个test.php,里面写入:
1 2 3 | <?php phpinfo(); ?> |
然后访问http://localhost/test.php,可以看到mysql都已经配置好了。
5.增加mcrypt扩展支持
先下载,libmcrypt,然后在终端定位到目录里面
再在终端输入:
1 2 3 | ./configure --disable-posix-threads --enable-static make sudo make install |
下载php的源码包,然后解包,在终端用cd定位到里面的ext/mcrypt目录
终端输入:
1 2 3 4 5 | phpize ./configure make cd modules sudo cp mcrypt.so /usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so |
然后在终端输入:
1 | sudo pico /etc/php.ini |
在extensions那里加上:
extension=mcrypt.so
当然,别忘记把extension_dir=后面的改为:
“/usr/lib/php/extensions/no-debug-non-zts-20090626/”
然后,重启apache
1 | sudo apachectl restart |
至此,就在Snow Leopard下成功安装了PHP,MySQL和Apache了。
升级内核后spawn-fcgi无法启动
by Elton on 九.17, 2009, 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请参考之前发表的帖子
Gentoo下安装Nginx+php
by Elton on 七.19, 2009, 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | user nginx nginx; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; error_log /var/log/nginx/error_log info; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { worker_connections 8192; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"'; client_header_timeout 10m; client_body_timeout 10m; client_max_body_size 50m; #最大允许上传50M的附件 send_timeout 10m; connection_pool_size 512; client_header_buffer_size 8k; large_client_header_buffers 4 4k; request_pool_size 1024k; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css application/xml; output_buffers 1 1024k; postpone_output 1460; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 75 20; ignore_invalid_headers on; index index.html; include /data/www/vhosts/*.conf; |
其中,worker_processes表示worker进程的个数,一般跟CPU个数相同
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000
表示每个CPU处理一个worker
虚拟主机配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | server { index index.htm index.html index.php server_name blog.domain.com; access_log /var/log/nginx/domain.access_log main; error_log /var/log/nginx/domain.error_log info; root /data/www/domain/htdocs/blog; location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } location ~ .*.php$ { include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js)$ { expires 30d; } } |
上面的配置以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 2 3 4 | location ~ ^/status/ { stub_status on; #Nginx 状态监控配置 access_log off; } |
就可以使用http://yourdomain.com/stauts监控nginx的状态。
可以看到类似这样的信息
1 2 3 4 | Active connections: 70 server accepts handled requests 14553819 14553819 19239266 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
lighttpd配置之压缩文件(mod_compress)
by Elton on 七.11, 2009, under Linux
在页面中使用gzip可以有效的减低页面的大小,加快网页的下载速度。在lighttpd中对php页面进行压缩,需要两个步骤:
1. 编辑 lighttpd.conf
将 “mod_compress” 设为启用
接著找到
## compress module
在下面加入
1 2 | compress.cache-dir = “/var/tmp/lighttpd/cache/” compress.filetype = (”text/plain”, “text/html”, “text/css”, “text/javascript”) |
做完上面的动作后,
基本上 .txt .html .css .js 的文件都会被Gzip压缩了。但php此时还没有压缩
对于动态的php文件,还需要在php.ini中做相关设置,否则.php页面还是不使用压缩模式
2. 编辑 php.ini
修改
zlib.output_compression = On
zlib.output_handler = On
重新启动Lighttpd。
这样php也压缩了
CentOS环境中lighttpd+php+fastcgi+eAccelerator安装配置
by Elton on 六.21, 2009, 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 2 3 4 5 | yum install pcre-devel tar xzvf lighttpd-1.4.23.tar.gz cd lighttpd-1.4.23 ./configure --prefix=/usr/local/lighttpd |
configure完毕以后,会给出一个激活的模块和没有激活模块的清单,可以检查一下,是否自己需要的模块都已经激活,在enable的模块中一定要有“mod_rewrite”这一项,否则重新检查pcre是否安装。然后编译安装:
1 | make && make install |
编译后配置:
1 2 3 | cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd mkdir /etc/lighttpd cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf |
如果你的Linux是RedHat/CentOS,那么:
1 | cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd |
如果你的Linux是SuSE,那么:
1 | cp doc/rc.lighttpd /etc/init.d/lighttpd |
其他Linux发行版本可以自行参考该文件内容进行修改。然后修改/etc/init.d/lighttpd,把
1 | lighttpd="/usr/sbin/lighttpd" |
改为
1 | lighttpd="/usr/local/lighttpd/sbin/lighttpd" |
此脚本用来控制lighttpd的启动关闭和重起:
1 2 3 | /etc/init.d/lighttpd start /etc/init.d/lighttpd stop /etc/init.d/lighttpd restart |
如果你希望服务器启动的时候就启动lighttpd,那么:
1 | chkconfig lighttpd on |
这样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 3 4 5 6 7 8 9 10 11 12 13 14 | $HTTP["host"] == "www.xxx.com" { server.document-root = "/yourrails/public" server.error-handler-404 = "/dispatch.fcgi" fastcgi.server = (".fcgi" => ("localhost" => ("min-procs" => 10, "max-procs" => 10, "socket" => "/tmp/lighttpd/socket/rails.socket", "bin-path" => "/yourrails/public/dispatch.fcgi", "bin-environment" => ("RAILS_ENV" => "production") ) ) ) } |
即由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 3 4 5 6 7 | wget http://curl.cs.pu.edu.tw/download/curl-7.19.5.tar.bz2 tar xvjf curl-7.19.5.tar.bz2 cd curl-7.19.5 ./configure --prefix=/usr/local/curl make make install |
gettext
1 2 3 4 5 6 | wget ftp://ftp.ntu.edu.tw/pub/gnu/gnu/gettext/gettext-0.17.tar.gz tar xvzf gettext-0.17.tar.gz cd gettext-0.17 ./configure --prefix=/usr/local/gettext make make install |
zlib
1 2 3 4 5 | wget http://kent.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz tar xvzf zlib-1.2.3.tar.gz cd zlib-1.2.3 ./configure --prefix=/usr/local/zlib make && make install |
libpng
1 2 3 4 5 | wget http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/l/li/libpng/libpng-1.2.9.tar.gz tar xvzf libpng-1.2.9.tar.gz cd libpng-1.2.9 ./configure --prefix=/usr/local/libpng make && make install |
jpeg
1 2 3 4 5 6 7 8 9 10 11 12 13 | wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz tar xvzf jpegsrc.v6b.tar.gz cd jpeg-6b/ ./configure --prefix=/usr/local/jpeg6 make mkdir /usr/local/jpeg6/bin mkdir -p /usr/local/jpeg6/bin mkdir -p /usr/local/jpeg6/man/man1 mkdir -p /usr/local/jpeg6/lib mkdir -p /usr/local/jpeg6/include make install-lib make install |
freetype
1 2 3 4 5 6 | wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.9.tar.gz tar xvzf freetype-2.3.9.tar.gz cd freetype-2.3.9 ./configure --prefix=/usr/local/freetype2 make make install |
gd
1 2 3 4 5 6 7 | wget http://www.libgd.org/releases/gd-2.0.35.tar.gz tar xvzf gd-2.0.35.tar.gz cd gd-2.0.35 ./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/ make 如果第一次make出错,试着再make一次,我就是这样,第二次就对了。 make install |
PHP
1 2 3 4 5 6 7 8 | tar xvzf php-5.2.10.tar.gz cd php-5.2.10 ./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 make make install cp php.ini-dist /etc/php.ini 可以使用php -m查看你安装的模块 |
eAccelerator
eAccelerator是一个开源的PHP加速器
1 2 3 4 5 6 7 8 | wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2 tar xjvf eaccelerator-0.9.5.3.tar.bz2 cd eaccelerator-0.9.5.3 export PHP_PREFIX="/usr/local/php" $PHP_PREFIX/bin/phpize ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config make make install |
执行好后,会提示安装到的路径,下面会用到,如我的被安装到这里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
编辑php.ini中的内容
vim /etc/php.ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | cgi.fix_pathinfo = 1 zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" eaccelerator.shm_size="16" eaccelerator.cache_dir="/tmp/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" |
如果一切顺利,你可以通过下面命令来验证是否安装成功
1 2 3 4 5 | $ php -v PHP 5.2.10 (cli) (built: Jun 20 2009 23:32:09) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator |
修改/etc/lighttpd/lighttpd.conf文件,添加下面的配置
vim /etc/lighttpd/lighttpd.conf
1 2 3 4 5 6 7 8 | fastcgi.server = ( ".php" => ( "localhost" => ( "socket" => "/tmp/php-fastcgi.socket", "bin-path" => "/usr/local/php/bin/php-cgi" ) ) ) |
重启lighttpd
1 | /etc/init.d/lighttpd restart |
写一个php测试文件在lighttpd的网站目录里,测试php是否安装成功


