Showing posts with label Nginx. Show all posts
Showing posts with label Nginx. Show all posts

Friday, May 23, 2014

Enable PHP 5.5 Opcache on Ubuntu 14.04 with Nginx and PHP-FPM

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

Making the switch from APC to Opcache now that it's standard with Ubuntu 14.04. All the tutorials seemed to be on Apache so thought I'd share what I did. The first change and restarting php I believe is all you need to do while the other changes are for performance and will vary based on your needs. If others have recommendations or additional advice please pipe in!

sudo vim /etc/php5/fpm/php.ini

Change:
;opcache.enable=0 to opcache.enable=1

Change:
;opcache.memory_consumption=64 to opcache.memory_consumption=128

Change:
;opcache.max_accelerated_files=2000 to opcache.max_accelerated_files=4000

Change:
;opcache.revalidate_freq=2 to opcache.revalidate_freq=60

Then restart:
sudo service php5-fpm restart
sudo service nginx restart

Friday, May 16, 2014

Increase file upload size limit in PHP-Nginx

If Nginx aborts your connection when uploading large files, you will see something like below in Nginx’s error logs:
[error] 25556#0: *52 client intended to send too large body:
This means, you need to increase PHP file-upload size limit. Following steps given below will help you troubleshoot this!

Changes in php.ini

To change max file upload size to 100MB
Edit…
vim /etc/php5/fpm/php.ini
Set…
upload_max_filesize = 100M
post_max_size = 100M

Notes:

  1. Technically,  post_max_size should always be larger than upload_max_filesize but for large numbers like 100M you can safely make them equal.
  2. There is another variable max_input_time which can limit upload size but I have never seen it creating any issue. If your application supports uploads of file-size in GBs, you may need to adjust it accordingly. I am using PHP-FPM behind Nginx from very long time and I think in such kind of setup, its Nginx to which a client uploads file and then Nginx copies it to PHP. As Nginx to PHP copying will be local operation max_input_time may never create issue. I also believe Nginx may not copy the file but merely hand-over the location of file or descriptor records to PHP!
You may like to read these posts which explains PHP file upload related config in some details.

Change in Nginx config

Add following line to http{..} block in nginx config:
http {
 #...
        client_max_body_size 100m;
 #...
}
Note: For very large files, you may need to change value of client_body_timeout parameter. Default is 60s.

Reload PHP-FPM & Nginx

service php5-fpm reload
service nginx reload

Changes in WordPress-Multisite

If you are running WordPress Multisite setup, then you may need to make one more change at the WordPress end.
Go to: Network Admin Dashboard >> Settings. Look for Upload Settings
Also change value for Max upload file size
https://rtcamp.com/tutorials/php/increase-file-upload-size-limit/