Showing posts with label php-fpm. Show all posts
Showing posts with label php-fpm. Show all posts

php opcache

With the release of Ubuntu 14.04 and the proliferation of PHP 5.5, there is going to be a migration away from Alternative Performance Cache (APC) and toward PHP’s new built-in OPcache.

This is a logical move that seems destined for any interpreted language.  As websites have become more and more complicated with many processes running, opcode caching has become a necessity – fortunately, it’s simple to implement.

The php.net site has a nice page of all the runtime options available, but we will cover the basics here to get you started quickly.

All you need to do to get OPcache set up is to make changes in the php.ini file on your server.

Open php.ini In Your Favorite Text Editor
To get started open your php.ini file.

Apache web-servers

/etc/php5/apache2/php.ini
Nginx web-servers with PHP-FPM

/etc/php5/fpm/php.ini
Enable the OPcache
To enable the OPcache, change to the following lines — easy enough!


;opcache.enable=0
Change to:


opcache.enable=1
Note: you have to uncomment this line as well as change the “0″ to “1″.

Modify the Amount of RAM the OPcache Will Use
With OPcache, there is a trade-off between speed and the amount of RAM used. The more RAM you are willing to dedicate to storing opcode, the more opcode that can be stored. There is a diminishing return at some point, because some code will execute rarely, or your code base might not be that big. It is worth playing with this setting to see where you get the best performance-versus-RAM trade-off.  This setting is in megabytes.


;opcache.memory_consumption=64
Change to:


opcache.memory_consumption=128
Boost the Number of Scripts that Can Be Cached
OPcache has a strange setting that requires you to not only adjust the amount of RAM, but also define the number of scripts that can be cached. You have the option of tuning this parameter for your own application too, especially if you find that your hit rate is not close to 100 percent.


;opcache.max_accelerated_files=2000
Change to:


opcache.max_accelerated_files=4000
Change the Revalidate Frequency
To make sure that the OPcache notices when you change your PHP code, you can set the revalidate frequency. Basically, this will tell the cache how often to check the timestamp on the files. This is measured in seconds.


;opcache_revalidate_freq = 2
Change to:


opcache_revalidate_freq = 240
Verify that the PHP OPcache Mod is Enabled
Believe it or not, that converts most of the settings you will need to get started. PHP5 has its own module system (since 5.4), so make sure that OPcache is enabled.


sudo php5enmod opcache
Restart PHP and Your Server
You should now be all set to start using PHP 5.5’s OPcache. You just need to restart your server to get it going.

Apache Web-Servers

sudo service apache2 restart
Nginx Web-Servers

sudo service nginx restart

nginx php-fpm wordpress centos 6.x

nginx 1.8
php-fpm 5.3
mysql 5.5
cpu core = 1 , memory = 2G
updated: 2016 April 3

 vim /etc/nginx/nginx.conf  
 user nginx;  
 worker_processes 1;  
 error_log /var/log/nginx/error.log warn;  
 pid    /var/run/nginx.pid;  
 events {  
   worker_connections 1024;  
 }  
 http {  
   include    /etc/nginx/mime.types;  
   default_type application/octet-stream;  
   log_format main '$remote_addr - $remote_user [$time_local] "$request" '  
            '$status $body_bytes_sent "$http_referer" '  
            '"$http_user_agent" "$http_x_forwarded_for"';  
   access_log /var/log/nginx/access.log main;  
   server_tokens off;  
   sendfile    on;  
   tcp_nopush   on;  
   keepalive_timeout 10;  
   #gzip on;  
   # Gzip on  
 gzip on;  
 gzip_min_length 1100;  
 gzip_buffers 4 32k;  
 gzip_types  text/plain application/x-javascript text/xml text/css;  
 ignore_invalid_headers on;  
 client_max_body_size  20m;  
 client_body_buffer_size 15m;  
 client_header_timeout 10s;  
 client_body_timeout 10s;  
 send_timeout   10s;  
 connection_pool_size 256;  
 client_header_buffer_size 4k;  
 large_client_header_buffers 4 32k;  
 request_pool_size 4k;  
 output_buffers  4 32k;  
 postpone_output 1460;  
 # Cache most accessed static files  
 open_file_cache     max=10000 inactive=10m;  
 open_file_cache_valid  2m;  
 open_file_cache_min_uses 1;  
 open_file_cache_errors  on;  
   include /etc/nginx/conf.d/*.conf;  
 }  

vim /etc/nginx/conf.d/default.conf
 server {  
  #for non www to www.domain.com  
   listen    80;  
   server_name  example.com;  
   return    301 http://www.example.com$request_uri;  
 }  
 server {  
   listen    80;  
   server_name www.example.com;  
    root  /var/www/html/example.com/public_html;  
    index index.php index.html index.htm;  
   location / {  
    root  /var/www/html/example.com/public_html;  
    index index.php index.html index.htm;  
    try_files $uri $uri/ /index.php?$args;  
   }  
   location ~ \.php$ {  
         root  /var/www/html/example.com/public_html;  
         try_files $uri =404;  
         include fastcgi_params;  
         fastcgi_pass unix:/var/run/php5-fpm.sock ;  
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
         fastcgi_index index.php;  
 fastcgi_buffer_size 4K;  
 fastcgi_buffers 64 4k;  
     }  
   location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {  
     access_log    off;  
     expires      max;  
   }  
   error_page  500 502 503 504 /50x.html;  
   location = /50x.html {  
     root  /usr/share/nginx/html;  
   }  
   location ~ /\.ht {  
     deny all;  
   }  
 }  

/etc/php-fpm.d/www.conf
listen = /var/run/php5-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666



vim /etc/my.cnf

 server {  
  #for non www to www.domain.com  
   listen    80;  
   server_name  example.com;  
   return    301 http://www.example.com$request_uri;  
 }  
 server {  
   listen    80;  
   server_name www.example.com;  
    root  /var/www/html/example.com/public_html;  
    index index.php index.html index.htm;  
   location / {  
    root  /var/www/html/example.com/public_html;  
    index index.php index.html index.htm;  
    try_files $uri $uri/ /index.php?$args;  
   }  
   location ~ \.php$ {  
         root  /var/www/html/example.com/public_html;  
         try_files $uri =404;  
         include fastcgi_params;  
         fastcgi_pass unix:/var/run/php5-fpm.sock ;  
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
         fastcgi_index index.php;  
 fastcgi_buffer_size 4K;  
 fastcgi_buffers 64 4k;  
     }  
   location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {  
     access_log    off;  
     expires      max;  
   }  
   error_page  500 502 503 504 /50x.html;  
   location = /50x.html {  
     root  /usr/share/nginx/html;  
   }  
   location ~ /\.ht {  
     deny all;  
   }  
 }  

 
## setup phpmyadmin with basic auth
 
Download phpmyadmin from phpmyadmin.net/downloads (note the php version dependency: im using php 5.3)
 
https://files.phpmyadmin.net/phpMyAdmin/4.4.15.5/phpMyAdmin-4.4.15.5-english.tar.gz 
extract the file and move it to /usr/share as /usr/share/phpMyAdmin
 
   tar xvfz phpMyAdmin-4.4.15.5-english.tar.gz     
   mv phpMyAdmin-4.4.15.5-english /usr/share/phpMyAdmin   
   ln -s /usr/share/phpMyAdmin /var/www/html/example.com/public_html/DBadmin  
 ## Create basic auth file   
   htpasswd -c /var/secret/.htpasswd jimmy   
 #Add a location block in server { ... }  
   location /DBadmin {  
   auth_basic "Admin Login Required";  
   auth_basic_user_file /var/secret/.htpasswd;  
   }   
 
Fix phpmyadmin error 

Error during session start; please check your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.

This error on my case was due to incorrect php session directory where root:apache was the default file ownership
 
ls -l /var/lib/php/
drwxrwx---. 2 root apache 4096 Mar  1 08:02 session

The FIX 

chown root:nginx /var/lib/php/session           
Remove old session files inside /var/lib/php/session
    
 
https://stavrovski.net/blog/install-and-configure-nginx-mysql-php-fpm-in-centos-6
https://www.scalescale.com/tips/nginx/improving-wordpress-performance-nginx-php-fpm-mysql-memcached-w3-total-cache/