Sunday, 17 July 2016

How to install php7 (zts) + pthreads on Ubuntu 14.04

Update and Install Dependencies
apt-get update

apt-get install -y bison autoconf build-essential pkg-config git-core libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev
Remove any existing php7 and recreate php7 and other subdirectories
rm -rf /etc/php7

mkdir -p /etc/php7
mkdir -p /etc/php7/cli
mkdir -p /etc/php7/etc
Remove any php-src folder in your current working directory and reinstall from git branch
rm -rf php-src

git clone https://github.com/php/php-src.git --depth=1
Change directory to ext to download pthreads from git
cd php-src/ext

git clone https://github.com/krakjoe/pthreads -b master pthreads
Go back to php-src as your current working directory
cd ..
Build the php-src
./buildconf --force
Set configure options, enable zts, fpm, and other extensions applicable to your own case
CONFIGURE_STRING="--prefix=/etc/php7 --with-bz2 --with-zlib --enable-zip --disable-cgi \
   --enable-soap --enable-intl --with-mcrypt --with-openssl --with-readline --with-curl \
   --enable-ftp --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
   --enable-sockets --enable-pcntl --with-pspell --with-enchant --with-gettext \
   --with-gd --enable-exif --with-jpeg-dir --with-png-dir --with-freetype-dir --with-xsl \
   --enable-bcmath --enable-mbstring --enable-calendar --enable-simplexml --enable-json \
   --enable-hash --enable-session --enable-xml --enable-wddx --enable-opcache \
   --with-pcre-regex --with-config-file-path=/etc/php7/cli \
   --with-config-file-scan-dir=/etc/php7/etc --enable-cli --enable-maintainer-zts \
   --with-tsrm-pthreads --enable-debug --enable-fpm \
   --with-fpm-user=www-data --with-fpm-group=www-data"
Run configure script and install
./configure $CONFIGURE_STRING

make && make install
Make the following helper programs executable
chmod o+x /etc/php7/bin/phpize

chmod o+x /etc/php7/bin/php-config
Now change directory to pthreads and run phpize
cd ext/pthreads*

/etc/php7/bin/phpize
Set configuration options for pthreads, but make sure enable-pthreads=shared which is the main aspect of the configuration
./configure --prefix='/etc/php7' --with-libdir='/lib/x86_64-linux-gnu' \ 
    --enable-pthreads=shared --with-php-config='/etc/php7/bin/php-config'

make && make install
FPM Installation involves copying configuration files to appropriate positions and making changes where necessary.
#back to current working directory php-src

cd ../../

#php.ini to be used by fpm-fcgi

cp -r php.ini-production /etc/php7/cli/php.ini

sed -i 's/;date.timezone =.*/date.timezone = Africa\/Lagos/' /etc/php7/cli/php.ini

cp /etc/php7/etc/php-fpm.conf.default /etc/php7/etc/php-fpm.conf

cp /etc/php7/etc/php-fpm.d/www.conf.default /etc/php7/etc/php-fpm.d/www.conf

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

sed -i 's/Provides:          php-fpm/Provides:          php7-fpm/' /etc/init.d/php7-fpm

sed -i 's#^php_fpm_BIN=.*#php_fpm_BIN=/usr/sbin/php7-fpm#' /etc/init.d/php7-fpm

sed -i 's#^php_fpm_CONF=.*#php_fpm_CONF=/etc/php7/etc/php-fpm.conf#' /etc/init.d/php7-fpm

sed -i 's#^php_fpm_PID=.*#php_fpm_PID=/var/run/php7-fpm.pid#' /etc/init.d/php7-fpm
Add pthreads.so to php-cli.ini, this will used by command line interface (CLI)
cp php.ini-production /etc/php7/cli/php-cli.ini

echo "extension=pthreads.so" > /etc/php7/cli/php-cli.ini
Add other extensions and link binaries
echo "zend_extension=opcache.so" >> /etc/php7/cli/php.ini

ln --symbolic /etc/php7/bin/php /usr/bin/php

ln --symbolic /etc/php7/sbin/php-fpm /usr/sbin/php7-fpm

chmod +x /etc/init.d/php7-fpm

update-rc.d php7-fpm defaults

service php7-fpm start
Make sure the following are as they are in /etc/php7/etc/php-fpm.conf
pid = /var/run/php7-fpm.pid
error_log = /var/log/php7-fpm.log
include=/etc/php7/etc/php-fpm.d/*.conf
Make sure the following are as they are in /etc/php7/etc/php-fpm.d/www.conf
user = www-data
group = www-data
listen = /var/run/php7-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
security.limit_extensions = .php .php3 .php4 .php5 .php7
Make sure the following are as they are in /etc/php7/cli/php.ini
cgi.fix_pathinfo=1
To use with Nginx do the following in your nginx/sites-available/default
upstream php7-fpm {
    server unix:/var/run/php7-fpm.sock;
}
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /usr/share/nginx/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ [^/]\.php(/|$) {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php7-fpm;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

service nginx restart
To use pthreads, remember it works via command line interface. Assuming thread.php is your parallel programming code, you can't run it in sapi/fpm mode by doing localhost/path/to/thread.php with your browser
<?php
//thread.php
    class Part extends Threaded{ public function run(){} }
    $parts = new Part();

    for($i=0;$i<4;$i++) $parts[] = $i;
?>
Instead you create a file to execute your code thread.php via command line methods like "system"
<?php
    system("php /path/to/thread.php");
?>

How to install mod pagespeed on centos

Installing mod_pagespeed for cPanel Apache installation on CentOS is really quite easy. Connect your server SSH via root user.
cd /usr/local/src
mkdir mod_pagespeed
cd mod_pagespeed
Download the mod_pagespeed RPM to build. Please make sure which arch is your server. If you have a 32-bit install of CentOS you can find your appropriate package on Google’s Installing mod_pagespeed From Packages page.
yum install at  # if you do not already have ‘at’ installed
Mostly apache version 2.2 support for this mod pagespeed version (httpd >= 2.2 is needed by mod-pagespeed-stable-1.4.26.4-3396.x86_64)
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm

rpm2cpio mod-pagespeed-stable_current_x86_64.rpm | cpio -idmv
Copy mod pagespeed.so file to apache module directory
cp usr/lib64/httpd/modules/mod_pagespeed.so /usr/local/apache/modules/

chmod 755 /usr/local/apache/modules/mod_pagespeed.so

mkdir -p /var/mod_pagespeed/{cache,files}

chown nobody.nobody /var/mod_pagespeed/*
Open pagespeed configuration file.
vi /usr/local/apache/conf/pagespeed.conf
LoadModule pagespeed_module /usr/local/apache/modules/mod_pagespeed.so

<IfModule pagespeed_module>
    ModPagespeed on
ModPagespeedFetchWithGzip on
SetOutputFilter DEFLATE

    AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

ModPagespeedFileCachePath            "/var/mod_pagespeed/cache/"
ModPagespeedGeneratedFilePrefix      "/var/mod_pagespeed/files/"

ModPagespeedRewriteLevel PassThrough

ModPagespeedFileCacheSizeKb          102400
ModPagespeedFileCacheCleanIntervalMs 3600000
ModPagespeedLRUCacheKbPerProcess     1024
ModPagespeedLRUCacheByteLimit        16384
ModPagespeedCssInlineMaxBytes        2048
ModPagespeedImageInlineMaxBytes      2048
ModPagespeedCssImageInlineMaxBytes   2048
ModPagespeedJsInlineMaxBytes         2048
ModPagespeedCssOutlineMinBytes       3000
ModPagespeedJsOutlineMinBytes        3000

ModPagespeedEnableFilters extend_cache,combine_css,move_css_to_head,rewrite_javascript,rewrite_images,add_head,rewrite_css,collapse_whitespace,remove_comments,remove_quotes,sprite_images,convert_meta_tags
ModPagespeedRespectVary on

    <Location /mod_pagespeed_beacon>
          SetHandler mod_pagespeed_beacon
    </Location>

    <Location /mod_pagespeed_statistics>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler mod_pagespeed_statistics
    </Location>
</IfModule>
Make a copy of your apache configuration httpd.conf file.
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf_back
Edit and include the pagespeed configuration.
vi /usr/local/apache/conf/httpd.conf
Include “/usr/local/apache/conf/pagespeed.conf”
Save and restart apache service.
/etc/init.d/httpd restart
Thats finished… Test your page speed modules on website headers.
curl -I http://yourwebsite.com
HTTP/1.1 200 OK
Date: Wed, 17 Oct 2012 06:58:03 GMT
Server: Apache
X-Mod-Pagespeed: 1.0.22.7-2005
Cache-Control: max-age=0, no-cache
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
You have successfully installed mod_pagespeed module on your cPanel server.

USB mouse on Ubuntu stops working

apt-get install linux-image-generic