Archive for the ‘PHP’ Category

PHP has two memcache extension, one is memcache, the other is memcached, which is badly named (confused with the daemon name), but arguably better.

To install memcached via PECL should be easier, but unfortunately the libmemcached come with CentOS repository doesn’t work with the memcached 2.2.0, which is the latest version in PECL right now.

My solution is

  • Install the memcached daemon via yum, if not installed yet.
    • Start the service: service memcached start
  • Remove the existing libmemcached and compile and install from the source in libmemcached website. https://launchpad.net/libmemcached/+download. I installed 1.0.18.
    • ./configure
    • make
    • make install
  • Download the memcached 2.2.0 from PECL website or via command “pecl download memcached”
  • tar -xvzf memcached-2.2.0.tar.gz
  • cd memcached-2.2.0
  • phpize
  • ./configure
  • make
  • make install
  • echo “extension=memcached.so” > /etc/php.d/memcached.ini
Advertisements

In PHP 5.3, request_order is introduced. This directive affects the contents of $_REQUEST.

If this directive is not set, variables_order is used for $_REQUEST contents.

Note that the default distribution php.ini files does not contain the ‘C’ for cookies, due to security concerns.

To include Cookie in $_REQUEST, just make sure that request_order is set to GPC

request_order = "GPC" 

Apache will return the server OS and apache version by default, e.g., Server: Apache/2.0.41 (Unix).

To hide it, set ServerTokens to Prod in httpd.conf, the default value is OS. All available values can be found in http://httpd.apache.org/docs/2.2/mod/core.html#servertokens

ServerTokens Prod

To hide the PHP number, set expose_php to off in php.ini

expose_php off

You should also set the ServerSignature to Off to stop apache from display the server information on the default error page, such as 404.

ServerSignature Off

1. Set showScriptName to false in urlManager in protected/config/main.php

2. In nginx config file, add the below line within server context

location /<your directory> {
try_files $uri $uri/ /<your directory>/index.php?$args;
}

You can install xdebug with the below command line if pecl is installed

pecl install xdebug

Or follow the similar way in Installing mcrypt to install it yourself. Make sure you change the last step to the below in php.ini

zend_extension=”/the/path/to/xdebug.so”

otherwise you may receive this error: Xdebug MUST be loaded as a Zend extension.

To keep php.ini clean, in my installation, I create a new xdebug.ini in /etc/php.d/, the content is as below.

zend_extension=”/usr/lib64/php/modules/xdebug.so”

[xdebug]
xdebug.remote_enable=On
xdebug.remote_host=”localhost”
xdebug.remote_port=9000
xdebug.remote_handler=”dbgp”

Next configure Eclipse to use xdebug as debugger.

Open Eclipse, go to Window->Preferences, expand PHP->Debug, select XDebug as PHP Debugger.

Under General->Web Browser, you can also choose use external web browser.

Save your changes and close Preferences.

Next go to Run->Debug Configurations. Under PHP Web Application, create a new configuration there. Below are my settings.

You need also configure the default PHP Server to point to your server here.

Now you start to debug your php project with xdebug.

Install php-fpm

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. The traditional way was using spawn-fcgi from lighttpd project.

php-fpm is not included in CentOS6 default repository, to add it, we can install Remi repository.

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum --enablerepo=remi install php php-fpm php-common php-mysql php-pdo php-pecl-apc php-cli php-mcrypt php-xml php-gd php-mbstring

Install Nginx

Nginx is not included in CentOS6 default repository, to add it, create file /etc/yum.repos.d/nginx.repo and add the following content.

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

After that: yum -y install nginx

Configuration

By default, php-fpm is a daemon process that runs a FastCGI server on port 9000, the init script is in /etc/init.d/php-fpm. We can configure it to listen to a unix socket instead of TCP sockets, while both are supported by Nginx.

vi /etc/php-fpm.d/www.conf

#listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

... ...
# change user to nginx instead of apache
#user = apache
user = nginx
#group = apache
group = nginx

vi /etc/nginx/conf.d/default.conf

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 or unix socket
#
location ~ \.php$ {
      #include /etc/nginx/fastcgi.conf;
      fastcgi_split_path_info  ^(.+\.php)(.*)$;
      #fastcgi_pass   127.0.0.1:9000;
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      #fastcgi_param  SCRIPT_FILENAME  $request_filename;
      include fastcgi_params;
}

If you receive the error ‘File not found’, double check the fastcgi_param SCRIPT_FILENAME line. Make sure the correct php file is passed to FastCGI.

Start the service

service nginx start
service php-fpm start

I am trying out magento in my CentOS 5.6. The php minimum requirement is 5.2 and CentOS 5.6 adds php53 as a php replacement, which is not installed by default.

To install php53, remove the installed php5.1 first.

yum -y remove php*

Install php53 and some common extensions.

yum -y install php53 php53-mysql php53-xml php53-gd

When I tried to install php53-mcrypt, it returned that package not available, so I must build it by myself.

Install php53-devel and libmcrypt-devel first. You need add rpmforge repository and you can refer to this post.

yum -y install php53-devel libmcrypt-devel

Download the php 5.3.3 source code from php.net.

cd /the/path/to/php5.3.3/ext/mcyrpt/
phpize
aclocal
./configure
make
make install

Next add the following line to php.ini

extension=mcrypt.so

Restart your apache