Xampp安装PHPUnit

最近准备着手开发Magento的插件,为了保证插件的代码质量,决定采用TDD的方法进行开发。在PHP实战中看到的TDD的开发方法,突然觉得豁然开朗,开发起来一点都不费力气,现在终于有机会亲自实现了。

当然,在卷起袖子之前,还需要配置好PHPUnit的开发环境。在PHPUnit的最新版本已经迁移到自己的pear.phpunit.de网站上了。本文假设你已经配置好Xampp的开发环境,我之前的文章PHP开发环境的搭建

首先升级你的pear版本,最新PHPUnit 3.6 要求PEAR的版本较高。在开始菜单打开CMD命令行。

cd /d D:\xampp\php
pear config-show
CONFIGURATION (CHANNEL PEAR.PHP.NET):
=====================================
Auto-discover new Channels     auto_discover    1
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          D:\xampp\php
PEAR documentation directory   doc_dir          D:\xampp\php\docs
PHP extension directory        ext_dir          D:\xampp\php\ext
PEAR directory                 php_dir          D:\xampp\php\pear
PEAR Installer cache directory cache_dir        D:\xampp\php\tmp
PEAR configuration file        cfg_dir          D:\xampp\php\cfg
directory
PEAR data directory            data_dir         D:\xampp\php\data
PEAR Installer download        download_dir     D:\xampp\php\tmp
directory
PHP CLI/CGI binary             php_bin          D:\xampp\php\.\php.exe
php.ini location               php_ini          <not set>
--program-prefix passed to     php_prefix       <not set>
PHP's ./configure
--program-suffix passed to     php_suffix       <not set>
PHP's ./configure
PEAR Installer temp directory  temp_dir         D:\xampp\php\tmp
PEAR test directory            test_dir         D:\xampp\php\tests
PEAR www files directory       www_dir          D:\xampp\php\www
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            0
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          c:\gnupg\gpg.exe
Signature Key Directory        sig_keydir       C:\Windows\pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         C:\Windows\pear.ini
System Configuration File      Filename         C:\Windows\pearsys.ini

以上是我的pear配置文件,仅供参考,输入以下命令升级pear的版本

pear upgrade pear

查看升级后的版本

pear –V
PEAR Version: 1.9.4
PHP Version: 5.3.5
Zend Engine Version: 2.3.0
Running on: Windows NT ARTHUR-PC 6.1 build 7600 (Unknow Windows version 
Ultimate Edition) i586

升级PHPUnit的版本到3.6,pear自带的版本太低了。

pear upgrade pear/PHPUnit

出现更新失败的信息

pear/PHPUnit is already installed and is the same as the released version 1.3.2
  upgrade failed

先卸载当前的PHPUnit版本

pear uninstall pear/PHPUnit

注意设置自动添加频道

pear config-set auto_discover 1
添加PHPUnit的必要频道
pear channel-discover components.ez.no 
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com

安装PHPUnit

pear install --alldeps phpunit/PHPUnit

查看PHPUnit的版本

phpunit –V
 
PHPUnit 3.6.3 by Sebastian Bergmann.

添加pear的路径到你的环境变量,例如D:\xampp\php。这样就能直接打pear命令了。

编写你的第一个测试

cd /d D:\xampp\htdocs\dev142\tests

新建index.php文件

<?php
//This is my first test
class MyFirstTest extends PHPUnit_Framework_TestCase{ 
    public function testFirst(){
        $stack = array();
        $this-&gt;assertEquals(0,count($stack));
    }
}
?>

测试你编写的代码

phpunit index.php
PHPUnit 3.6.3 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.50Mb
 
OK (1 test, 1 assertion)

参考

http://pear.phpunit.de/

http://amiworks.co.in/talk/installing-pear-and-phpunit-on-wamp-and-windows-7/

https://github.com/sebastianbergmann/phpunit/

http://blog.lixiphp.com/windows-install-pear-phpunit/

快速配置LNMP环境Nginx0.8.54+MYSQL5.5.16+PHP5.3.8+CentOS 6

最近购买了Linode的VPS服务,就开始着手以Nginx 为基础搭建LNMP平台,体验Nginx的魅力。Linux OS选择的是Linode的CentOS 6,话说CentOS 6出来太慢了。。。

仅此篇文章记录配置生产环境的过程。

安装Nginx 和 PHP

1. 修改你的hostname

echo "HOSTNAME=baby" >> /etc/sysconfig/network
hostname "baby"

2. 编辑你的/etc/hosts。添加以下

127.0.0.1   localhost.localdomain   localhost
12.34.56.78 baby.example.com   baby

3. 添加EPEL软件仓库,为Fedora 开发的仓库,同样适用CentOS 6.

rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm

利用yum 安装Nginx ,PHP,Spawn-fcgi,安装时会询问导入EPEL的key文件,直接y就ok。

yum install nginx php-cli php make automake gcc gcc-c++ spawn-fcgi wget

4. 添加nginx服务,设置开机启动,开启nginx

chkconfig --add nginx
chkconfig --level 35 nginx on
service nginx start

现在访问你的IP地址,可以看到nginx的默认页面。

5. 可以开始配置,创建网站文件夹。

mkdir -p /srv/www/www.xbc.me/public_html
mkdir /srv/www/www.xbc.me/logs
chown -R nginx:nginx /srv/www/www.xbc.me
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

在你的/etc/nginx/nginx.conf添加不同网站的配置文件

# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;

添加网站配置文件/etc/nginx/sites-available/www.xbc.me

server {
    server_name www.xbc.me xbc.me;
    access_log /srv/www/www.xbc.me/logs/access.log;
    error_log /srv/www/www.xbc.me/logs/error.log;
    root /srv/www/www.xbc.me/public_html;
 
    location / {
        index index.html index.htm index.php;
    }
 
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.xbc.me/public_html$fastcgi_script_name;
    }
}

在nginx启用网站配置文件

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.xbc.me
service nginx restart

6. 配置spawn-fcgi

cd /opt
wget -O php-fastcgi-rpm.sh http://library.linode.com/assets/696-php-fastcgi-rpm.sh
mv php-fastcgi-rpm.sh /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
wget -O php-fastcgi-init-rpm.sh http://library.linode.com/assets/697-php-fastcgi-init-rpm.sh
mv php-fastcgi-init-rpm.sh /etc/rc.d/init.d/php-fastcgi
chmod +x /etc/rc.d/init.d/php-fastcgi
chkconfig --add php-fastcgi
chkconfig php-fastcgi on
/etc/init.d/php-fastcgi start

7. 启动spawn-fcgi服务,可以测试PHP

新建文件/srv/www/www.xbc.me/public_html/test.php

<?php echo phpinfo(); ?>

安装mysql数据库

yum install mysql-server

添加mysqld开机服务

/sbin/chkconfig --levels 235 mysqld on

启动mysql守护进程

/etc/init.d/mysqld start

配置MYSQL,进行安全安装。

mysql_secure_installation

这条命令会

1. 提示你创建root密码

2. 删除匿名用户

3. 禁用root从localhost以外登录

4. 删除测试数据库

5. 提示刷新权限表

强烈推荐全部选择YES!!!

安装PHP常用扩展

安装扩展pear、mysql、apc加速器。

yum install php-pear
 
yum install php-mysql
 
yum install php-pecl-apc

重启php-fastcgi

killall php-cgi 
php-fastcgi start

访问网站子目录出现403 Forbidden错误,假设子目录为ecshop,修改网站配置文件/etc/nginx/sites-available/www.xbc.me

location /ecshop/ { 
	index index.php; 
}

出现错误FastCGI sent in stderr: "PHP Warning: Unknown: open(/var/lib/php/session/

/var/lib/php 权限预设为root apache,因为我们安装的是nginx,运行用户为nginx。

修改为root nginx 即可

Spawn-fastcgi 和 Php-fpm

从spawn-fastcgi转到php-fpm,最大的好处是内存占用小、稳定、可以无需重启载入配置。

默认CentOS 6的php版本是5.3.2,而php-fpm自从5.3.3以后的版本就集成了php-fpm,成为官方的人了。orz….

为了能贴近文章的标题,特找到一偷懒的方法利用yum 升级php–利用第三方的仓库remi。

添加remi仓库

cd /opt
wget http://rpms.famillecollet.com/RPM-GPG-KEY-remi

导入验证key文件

rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi

验证rpm包

rpm -K remi-release-6.rpm

添加remi 仓库

rpm -i remi-release-6.rpm

更新,如果已经安装ldop扩展,需要先删除,可能会出现依赖的问题。

yum remove php-ldap #to squash later dependency issues

升级php 和mysql

yum --enablerepo=remi update mysql php

升级完成后,PHP的版本为5.3.8,MYSQL为5.5.16

安装php-fpm

yum --enablerepo=remi install php-fpm

MYSQL升级完成后,出现

Missing system table mysql.proxies_priv; please run mysql_upgrade to create it

MYSQL需要运行mysql_upgrade升级脚本,如果运行命令出错,请用root来执行。

mysql_upgrade -u root -p

服务器优化

安装好LNMP环境后,可优化下配置,发挥服务器最大性能。这里主要讲后端的优化,前端优化请参考Yahoo的前端优化最佳实践

服务器硬件

内存: 768M

CPU: Intel(R) Xeon(R) CPU L5520 @ 2.27GHz 四核

带宽: 百兆共享

机房所在地:美国加利福尼亚州

Nginx配置优化/etc/nginx/nginx.conf参数,仅供参考。

cat /etc/nginx/nginx.conf | grep -v "#"
user              nginx;
worker_processes  2;
 
error_log  /var/log/nginx/error.log;
 
pid        /var/run/nginx.pid;
worker_rlimit_nofile 65535;
 
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                      '$status $body_bytes_sent &quot;$http_referer&quot; '
                      '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
 
    keepalive_timeout  65;
 
    gzip  on;
    gzip_min_length  1000;
    gzip_buffers     4 8k;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    server {
        listen       80;
        server_name  _;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
 
        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
 
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

主机配置文件/etc/nginx/sites-enabled/www.xbc.me

cat /etc/nginx/sites-enabled/www.xbc.me | grep -v "#"
server { 
    server_name www.xbc.me xbc.me; 
    access_log /srv/www/www.xbc.me/logs/access.log; 
    error_log /srv/www/www.xbc.me/logs/error.log; 
    root /srv/www/www.xbc.me/public_html;
 
    location / { 
        index index.html index.htm index.php; 
    } 
    location /phpmyadmin/ { 
        if (!-e $request_filename) { 
            rewrite ^.+/?(/phpmyadmin/.*\.php)$ $1 last; 
            rewrite ^(.+)$ /phpmyadmin/index.php?q=$1 last; 
        } 
        index index.php; 
    } 
    location ~ \.php$ { 
        fastcgi_pass  127.0.0.1:9000; 
        fastcgi_index index.php; 
        include /etc/nginx/fastcgi_params; 
        fastcgi_param SCRIPT_FILENAME /srv/www/www.xbc.me/public_html$fastcgi_script_name; 
    } 
    location ~* \.(js|css|jpg|jpeg|gif|png)$ { 
        if (-f $request_filename) { 
           expires 30d; 
           break; 
        } 
    } 
}

MYSQL参数优化/etc/my.cnf

cat /etc/my.cnf | grep -v "#"
[client]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
 
[mysqld]
datadir=/var/lib/mysql
user=mysql
symbolic-links=0
 
port            = 3306
socket          = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 256K
join_buffer_size = 512K
read_buffer_size = 512K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
 
default-storage-engine = MyISAM
skip-innodb
skip-name-resolve
external-locking = FALSE
slow_query_log
slow_query_log_file=/tmp/slow.log
long_query_time = 1
query_cache_size = 1M
max_heap_table_size = 32M
tmp_table_size = 32M
max_connections =8
 
thread_stack = 192K
thread_cache = 128
thread_cache_size = 10
thread_concurrency = 8
bind-address = 173.255.248.110
log-bin=mysql-bin
 
binlog_format=mixed
expire_logs_days = 30
server-id       = 1
 
[mysqldump]
quick
max_allowed_packet = 16M
 
[mysql]
no-auto-rehash
 
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
 
[mysqlhotcopy]
interactive-timeout
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

PHP配置优化,尽可能的加载可能少的模块,添加php加速器apc、eaccelerator

设置php的内存限制

memory_limit = 16M

php-fpm 配置优化

cat /etc/php-fpm.d/www.conf | grep -v ";"
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
 
user = nginx
group = nginx
 
pm = dynamic
pm.max_children = 32
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 8
 
slowlog = /var/log/php-fpm/www-slow.log
 
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on

参考

http://library.linode.com/web-servers/nginx/php-fastcgi/centos-5

http://library.linode.com/lamp-guides/centos-6#sph_install-mysql

http://www.clearfoundation.com/component/option,com_kunena/Itemid,232/catid,17/func,view/id,18743/

http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/

http://engin.bzzzt.biz/2011/04/06/mysql-workbench-fetching-problem-solved/

512M内存以下的小内存VPS性能优化和安全防护

小规模低性能低流量网站优化实践