1 of 53

Supercharged PHP & Server Optimizations

By Josh Copeland

2 of 53

Josh Copeland

  • Programming Since 2006
  • Working with PHP since 2010
  • Founder/CTO of Remote Dev Force
  • PHP Vegas UG Leader since 2012

Socials: @OGProgammer

3 of 53

Remote Dev Force

  • Specializing in PHP, TF, & AWS
  • 12+ Sr. Developers
  • Hundreds of completed projects
  • Hire one dev or an entire team

Josh@RemoteDevForce.com

4 of 53

Table of contents

Servers

PHP

Frameworks

01

02

03

How to optimize linux servers, apache/nginx, and database servers.

Optimizing PHP FPM & Core.

What your options are for optimizing Laravel & Symfony.

5 of 53

Metrics

  • Speed Index

  • Time to Interact

  • DNS Lookup Time

  • Error Rate

  • Peak Response Time
  • User Satisfaction
    • Mobile First

  • Time to First Byte
    • Web Server

  • Time to First Paint
    • Minify JS/CSS + CDN

6 of 53

Metrics

7 of 53

Servers

01

8 of 53

Memory

Finding Memory Allocation & Utilization with β€œfree” command

free

9 of 53

Swap Memory

To disable or not to disable, that is the question.

free

10 of 53

CPU

dstat

11 of 53

Processes

Find what process is using a majority of the systems resources.

top

12 of 53

Processes

htop

13 of 53

Disk

df -h

14 of 53

Monitor Disk/Memory Usage

15 of 53

Disk

ulimit -n 4096

β€œToo many open files”

ulimit

vi /etc/security/limits.conf

* soft nofile 1024

* hard nofile 4096

16 of 53

Network Traffic

nload

17 of 53

Network Traffic by Process

nethogs

18 of 53

AWS Graphs

AWS

19 of 53

AWS Graphs

AWS

20 of 53

Web Servers

  • HTTP/2
  • Gzip
  • Buffer Size
  • Log Buffering
  • Low timeout values

nginx/apache

21 of 53

Caching

  • Network caching
  • HTTP caching
  • Reverse proxy server caching
  • Database caching

22 of 53

Varnish Cache

23 of 53

PHP

02

24 of 53

Upgrade PHP

5 > 7 > 8

Upgrade PHP

25 of 53

Profile your code

Xdebug + Webgrind

Blackfire.io

Any PHP APM (NewRelic/Datadog/etc.)

See hot spots? Refactor those.

Code Profiling

26 of 53

Use the right data types

  • Array
  • Object
  • NULL
  • Resource
  • Boolean
  • String
  • Integer
  • Float

27 of 53

php -m

Double check enabled modules

php-m

28 of 53

php.ini

disable_functions = system, exec, shell_exec, passthru, phpinfo, show_source, highlight_file, popen, fopen_with_path, dbmopen, dbase_open, chdir, filepro, filepro_rowcount, filepro_retrieve, posix_mkfifo, pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wifcontinued, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_get_handler, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, pcntl_async_signals

disable_functions

29 of 53

php.ini

realpath_cache_size = 4096K -> 8M

realpath_cache_ttl = 120 -> 600

This value should be increased on systems where PHP opens many files, to reflect the quantity of the file operations performed.

realpath_cache_size

30 of 53

php-fpm tuning

pm = dynamic -> static

pm.max_children = 10 -> memory_limit Γ· total available memory

For high traffic sites, a static amount of php-fpm workers is ideal.

dynamic is ok for low traffic or admin backend sites.

ondemand is good for cpanel/mass hosting for multiple pools that get little/no traffic.

php-fpm process management

31 of 53

Setting

Value

max_children

(Total RAM – Memory used for Linux, DB, etc.) / process size

start_servers

Number of CPU cores x 4

min_spare_servers

Number of CPU cores x 2

max_spare_servers

Same as start_servers

Dynamic calculation

32 of 53

php.ini or pool.d/www.conf

memory_limit = 128M -> 512M*

Large queries, xdebug, or heavy reports will bloat memory usage.

Offload report queries to background php cli workers.

* The lower the better but symfony/laravel tend to eat up memory.

Memory limit

33 of 53

OPCache & JIT

40%+ Increase in speed

34 of 53

OPcache Ext.

PHP 5.2, 5.3, 5.4 has a PECL Extension

PHP 5.5+ requires it to be compiled as a β€œshared extension”

To use OPCache with Xdebug, you need to load OPCache before Xdebug.

opcache

35 of 53

OPcache + JIT - php.ini

zend_extension=opcache.so

opcache.enable=1

opcache.enable_cli=1

opcache.jit_buffer_size=500000000

opcache.jit=1235

Opcache + jit

36 of 53

OPcache + JIT - opcache.ini

[opcache]

opcache.enable=1

; 0 means it will check on every request

; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in production

opcache.revalidate_freq=0

opcache.validate_timestamps=1

opcache.max_accelerated_files=10000

opcache.memory_consumption=192

opcache.max_wasted_percentage=10

opcache.interned_strings_buffer=16

opcache.fast_shutdown=1

Longhorn + jit

37 of 53

OPCache Preload

Check your framework or find a package.

https://github.com/Laragear/Preload

https://symfony.com/blog/php-preloading-and-symfony

opcache.preload=/app/config/preload.php

Opcache preload

38 of 53

Database queries are the bottleneck 🍾

  • Avoid full table scans

  • Add Indexes to queries that search too many rows

  • Check slow query logs

  • Avoid running queries in a loop

DB

39 of 53

Frameworks

03

40 of 53

Async php

41 of 53

Do you even PROD?

APP_ENV to prod

42 of 53

Laravel

Update!

43 of 53

Laravel

Debugbar

44 of 53

Laravel

Debugbar

45 of 53

Laravel

Laravel Octane uses Open Swoole, Swoole, and RoadRunner.

Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.

Octane

46 of 53

Eloquent

if (!Cache::has('activeTeams'))

{

$teams = User::Where('status','Active')->with('Teams')->get();

Cache::put('activeTeams', $teams, Carbon::now()->addMinutes(1));

}

else{

$teams = Cache::get('activeTeams');

}

Results Cache

47 of 53

Symfony

48 of 53

Doctrine

  • result_cache_driver
  • query_cache_driver
  • metadata_cache_driver
  • second_level_cache (2LC)
  • region_cache_driver

49 of 53

Check your framework docs

  • Caching
  • Methods
  • Testing
  • Best Practices
  • ORM
  • DI/Services/Facades

50 of 53

Continue the hunt for optimizations

2pm Tomorrow

10am Tomorrow

51 of 53

Bring it around town.

What do you got to work with? What do you need?

What can you squeeze out of what you have?

Cache and optimize what you aren’t.

Make what you have better.

Server

Refactor

PHP

Framework

52 of 53

RemoteDevForce.com

We can help!

53 of 53

Thanks!

Do you have any questions?

Josh@RemoteDevForce.com

+1 (702) 328-8657

@RemoteDevForce

@OGProgrammer

CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon, and infographics & images by Freepik