Skip to content

Instantly share code, notes, and snippets.

@kuzminT
Last active May 1, 2019 13:41
Show Gist options
  • Save kuzminT/328d3f4a5df488d5a2ebef5416ca46c8 to your computer and use it in GitHub Desktop.
Save kuzminT/328d3f4a5df488d5a2ebef5416ca46c8 to your computer and use it in GitHub Desktop.
Php tips, docs and usefool links
## Example config for xdebug ini. Use with phpstorm ide.
zend_extension=xdebug.so
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.remote_host="localhost"
xdebug.remote_port=9010
xdebug.idekey="PHPSTORM"
xdebug.remote_handler=dbgp
xdebug.remote_autostart=1
xdebug.profiler_enable=off
xdebug.profiler_output_dir=/var/xdebug/profiler/
xdebug.profiler_enable_trigger = 1
xdebug.trace_format=0
xdebug.collect_params=4
xdebug.collect_return=1
; human readable plain text
; full variable contents and variable name.
; show return values
  • Background jobs with php and resque

    # Params for php-resque
    QUEUE : The name of the queues to poll
    INTERVAL : The polling interval (waiting time in seconds between each polling). Default to 5 seconds.
    APP_INCLUDE : Path to your application autoloader. Workers need to know where to find your jobs classes
    COUNT : Number of workers to create. All workers will have the same properties. Will always create one worker by default.
    REDIS_BACKEND : Address of your Redis server, formatted as hostname:port. e.g: 127.0.0.1:6379, or localhost:6379. Default to localhost:6379
    VERBOSE : 1 to enable verbose, will print basic debugging informations
    VVERBOSE : 1 to enable advanced verbose, will print detailed debugging informations
    

Запуск php-resque:

  nohup sudo -u www-data QUEUE=notification php resque.php >> /path/to/your/logfile.log 2>&1 &

Stop starting workers: sudo killall php

Find all active workers: sudo ps aux | grep resque

Аутентификация через соцсети

Works with images

Testing

Полезные ресурсы

Works with dates

# convert to timestamp and back to other formats
$d =  DateTime::createFromFormat('j.m.Y', '26.02.2019');
echo $d->format('U');
print "\n";
echo $d->format('Y/m/d');

# Converting an Epoch Timestamp to Time and Date
# Parts
$when = new DateTime("@163727100");
$when->setTimezone(new DateTimeZone('America/Los_Angeles'));
$parts = explode('/', $when->format('Y/m/d/H/i/s'));
// Year, month, day, hour, minute, second
// $parts is array('1975', '03','10', '16','45', '00'))

Nginx and apache

Redis

How to Set Up a Redis Server as a Session Handler for PHP

Базы данных

Optimal configuration for php.ini

opcache.memory_consumption=128
opcache.interned_strings_buffer= 16
opcache.max_accelerated_files=4000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

max_execution_time = 5

Opcache settings on php.net

opcache.memory_consumption

The amount of memory (in megabytes) allocated for the opcode cache. This should be large enough to store the compiled opcode for all of your application’s PHP scripts. If you have a small PHP application with few scripts, this can be a lower value like 16 MB. If your PHP application is large with many scripts, use a larger value like 64 MB.

opcache.interned_strings_buffer = 16

The amount of memory (in megabytes) used to store interned strings. What the heck is an interned string? That was my first question, too. The PHP interpreter, behind the scenes, detects multiple instances of identical strings and stores the string in memory once and uses pointers whenever the string is used again. This saves memory. By default, PHP’s string interning is isolated in each PHP process. This setting lets all PHP-FPM pool processes store their interned strings in a shared buffer so that interned strings can be referenced across multiple PHP- FPM pool processes. This saves even more memory. The default value is 4 MB, but I prefer to bump this to 16 MB.

opcache.max_accelerated_files = 4000

The maximum number of PHP scripts that can be stored in the opcode cache. You can use any number between 200 and 100000. I use 4000 . Make sure this number is larger than the number of files in your PHP application.

opcache.validate_timestamps = 1

When this setting is enabled, PHP checks PHP scripts for changes on the interval of time specified by the opcache.revalidate_freq setting. If this setting is dis‐ abled, PHP does not check PHP scripts for changes, and you must clear the opcode cache manually. I recommend you enable this setting during develop‐ ment and disable this setting during production.

opcache.revalidate_freq = 0

How often (in seconds) PHP checks compiled PHP files for changes. The benefit of a cache is to avoid recompiling PHP scripts on each request. This setting determines how long the opcode cache is considered fresh. After this time inter‐ val, PHP checks PHP scripts for changes. If PHP detects a change, PHP recom‐ piles and recaches the script. I use a value of 0 seconds. This value requires PHP to revalidate PHP files on every request if and only if you enable the opcache.val idate_timestamps setting. This means PHP revalidates files on every request during development (a good thing). This setting is moot during production because the opcache.validate_timestamps setting is disabled anyway.

opcache.fast_shutdown = 1

This prompts the opcache to use a faster shutdown sequence by delegating object deconstruction and memory release to the Zend Engine memory manager. Documentation is lacking for this setting. All you need to know is turn this on.

max_execution_time = 5

The max_execution_time setting in your php.ini file determines the maximum length of time that a single PHP process can run before terminating. By default, this is set to 30 seconds. You don’t want PHP processes running for 30 seconds. We want our applications to be super-fast (measured in milliseconds). I recommend you change this to 5 seconds:

File uploads

file_uploads = 1
upload_max_filesize = 10M
max_file_uploads = 3

Output buffering

output_buffering = 4096
implicit_flush = false

Gzip

gzip on;
gzip_vary on;
gzip_types text/plain text/xml text/css text/javascript application/x-
javascript;
gzip_com_level 4;    

Xdebug example config for phpstorm

zend_extension=xdebug.so 
xdebug.remote_enable = on 
xdebug.remote_connect_back = on 
xdebug.remote_host="localhost"  
xdebug.remote_port=9010 
xdebug.idekey="PHPSTORM" 
xdebug.remote_handler=dbgp 
xdebug.remote_autostart=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment