PHP

PHP Workers

PHP workers process a site’s PHP code. This includes building pages, processing background tasks, querying the database, etc.

One way to think of PHP workers is like employees at a store. Each worker can only handle one request at a time. If there are more customers than there are workers, those customers (processes) have to line up and wait for the next available worker to handle their request.

PHP workers really come into play when a site doesn’t or can’t cache most of its content. The more dynamic a website is, the more PHP workers it will likely need. Cached content doesn’t require PHP workers; they’re really only needed when the site needs to query the database to get or change information.

WordPress and PHP Workers

An uncached request on a WordPress site usually goes something like this:

  1. A visitor visits a page or performs an action on a page (e.g. adding something to a cart, submitting a form, etc.).
  2. The web server (Nginx here at Kinsta) receives that request.
  3. Nginx passes the request to PHP.
  4. PHP queries the MySQL database and gets the information it needs or makes any updates needed.
  5. PHP then uses your theme’s PHP files (and any plugin files if applicable) to generate an HTML page.
  6. PHP hands the rendered HTML page back to the web server.
  7. The page is served to the visitor.

In the process described above, Step 4 is the most time and resource-intensive (CPU and RAM). A well-optimized site with efficient PHP code and database queries will process that step fairly quickly.

On the other hand, poorly written or unoptimized PHP code and/or a lot of inefficient database queries will take much longer to get through Step 4. Requests that take longer to process monopolize PHP workers for longer periods of time.

Estimating the Number of PHP Workers Needed

How many workers a site needs depends on several factors, such as: how dynamic the site is, how optimized the site’s code is (how quickly requests can be processed), and what kind of traffic the site receives. An optimized site handles requests quickly, freeing up PHP workers for the next request in the queue.

Dynamic sites like ecommerce stores, forums, learning sites, and membership sites will usually need more PHP workers than more static, brochure-type sites. Also, the busier a site is, the more PHP workers it will usually need.

PHP Workers, CPU, and RAM

When adding PHP workers, CPU and RAM resources must be taken into account. If you increase PHP workers, but the server doesn’t have enough CPU and RAM to support those workers, this will create a bottleneck because the requests won’t be handled efficiently.

Here at Kinsta, our custom LXD containers are configured with plenty of CPU and RAM resources. Additionally, by using compute-optimized C2 virtual machines equipped with Google Cloud’s fastest CPUs, we help your site’s PHP workers run more efficiently. Our scalable infrastructure ensures that your WordPress site’s PHP workers have enough CPU resources to operate at peak performance.

If too many requests stack up in the queue because of a large influx of requests, long-running processes, or a combination of both, the site can experience performance issues that may result in 502 or 504 errors.

Using tools like Kinsta’s APM tool and the Query Monitor plugin can help you identify performance issues and slow queries. We also recommend working with a qualified performance expert to diagnose issues.

Click the Enable APM button to enable it in MyKinsta.
Click the Enable APM button to enable it in MyKinsta.

The PHP worker limit chart in MyKinsta performance analytics shows you how many times the maximum number of allocated workers has been reached on your site. This can help you determine if your site is continuously using up its PHP workers and can also show you how updating to a newer PHP engine version may help.

PHP worker limit
PHP worker limit

The cache analysis section in MyKinsta cache analytics can be used to view your site’s total cached requests and top cache bypasses.

Cache - cache component chart
Cache – cache component chart
Cache - top cache bypasses
Cache – top cache bypasses

Reduce and Optimize PHP Worker Usage

Caching

Caching is your best friend when it comes to optimizing your site and reducing the number of PHP workers needed. Remember, PHP workers aren’t needed for cached content, so cache everything you can.

Page Caching

At Kinsta, we take care of page caching for you; all sites use Nginx’s FastCGI cache module for super-fast performance.

Object Caching

Adding a persistent object cache like Redis in front of your database can boost performance and reduce the need for PHP workers. Without object caching, MySQL database queries are executed for each request, even when it’s the same query and results.

Redis stores the results of database queries in RAM so PHP can grab those results without running the query again. Removing the need for repetitive database queries allows PHP workers to conserve resources and fulfill requests more efficiently.

Check out our premium add-ons to learn more about adding Redis cache to your site.

Code Optimization

Make sure your site’s code is optimized so it is as efficient as possible. This applies to custom code, theme code, and plugin code. If you’re not sure, we recommend asking a developer to review your site’s code.

Custom Code

If your website contains any custom code snippets in plugins or your theme, make sure they’re truly necessary and well-written.

Plugins

Take a good look at the plugins used on the site and make sure they’re truly needed, don’t duplicate features, and are the best option for the need they fulfill. If any plugins aren’t compatible with the latest version of WordPress and PHP, it may be time to consider other options. If you have plugins on your site that aren’t being used, removing those is recommended.

Theme

Use a lightweight and performant theme. Avoid themes that contain functionality that’s best implemented via separate plugins (e.g., SEO, search filtering, custom fields, image sliders/slideshows, etc.) or isn’t needed for your site.

Update PHP

Use the latest PHP version for faster performance. PHP benchmarks show that each PHP version is faster than the previous one.

Enable Kinsta’s CDN

Enabling Kinsta’s CDN provides another boost of efficiency and optimization for your site. Kinsta’s CDN is our high-performance HTTP/3 CDN powered by Cloudflare, provided to you at no additional cost. With it enabled, your site will be able to serve static assets from locations across the world.

Consult With a Performance Expert

If you’re familiar with site optimization, this is an optional step. An expert can help you analyze all aspects of your sites, identify bottlenecks, and implement solutions.

Restart PHP

At Kinsta, we have an awesome feature we call self-healing PHP. This comes in handy if you happen to get a 502 error or if something else goes wrong. If PHP goes down for any reason, our system automatically tries to auto-restart it back up. In a lot of cases, this is all that’s needed, and you’re good to go!

On rare occasions, it might be a bigger issue. Our system automatically logs this for our Systems Administration (sysadmin) team to check out. Otherwise, you can still restart PHP for any of your websites individually with a simple click of a button from MyKinsta.

How to Restart PHP Manually

To restart PHP on your WordPress site, follow the steps below.

  1. Log in to MyKinsta.
  2. Go to WordPress Sites and select the site on which you’d like to restart PHP.
  3. Go to the Tools tab and find the Restart PHP section.
  4. Click the Restart PHP button.
Restart PHP in MyKinsta.
Restart PHP in MyKinsta.

Please note that restarting PHP will take about 5-10 seconds. You will receive a notification at the bottom of the screen when it is completed.

Note: You will also need to delete your site’s cache to see the changes.

Kinsta’s PHP Memory Limit

Kinsta’s default PHP memory limit is 256MB, which is more than enough for most WordPress plugins and sites. This limit exists to prevent PHP scripts from consuming excessive memory. If you set the limit too high, a misconfigured or broken script can cause serious issues by using up too much memory.

If your site is set up correctly at Kinsta, you shouldn’t run into a memory limit error. If you see this error, we recommend checking your WordPress settings to make sure it hasn’t been accidentally set too low.

Checking and Changing Your PHP Memory Limit

To check your current PHP memory limit for WordPress, log in to your site’s WordPress dashboard and navigate to Tools > Site Health.

Go to the Info tab and click the arrow icon next to the Server section to expand this section and view your PHP memory limit.

If the memory limit is lower than 256M, check your wp-config.php file to see if the WP_MEMORY_LIMIT has been modified and adjust it if needed.

If the memory limit is 256M, but you’re having issues with PHP memory, we recommend checking and testing plugins and themes. You can use a staging environment to safely deactivate and reactivate plugins and themes to identify the source of the memory usage.

If the error persists and you cannot identify the source, you can open a new chat with our Support team to check the logs and rule out any issues on the server side.

Updating PHP

We’ve made updating your site’s PHP version as easy as possible in MyKinsta.

If your site is ready to update PHP, log in to MyKinsta and go to WordPress Sites > sitename > Tools.

Under PHP Engine, click on the drop-down and select the PHP version you want to update your site to.

Modify PHP engine button in MyKinsta.
Modify PHP engine button in MyKinsta.

In the Modify PHP version modal/pop-up that appears, click the Modify PHP version button to confirm the change.

Modify PHP version.
Modify PHP version.

At the end of the update, your PHP engine will be restarted, and the backend (WordPress dashboard) of your site may be down for a couple of seconds. The front of the site will remain up, and visitors will not experience any downtime.

While the update process is running, you can navigate to other parts of MyKinsta, but some actions, like cache management, will be unavailable until the PHP engine has restarted.

Once the update is complete (usually within 3 minutes or less), you’ll receive a notification in MyKinsta that it’s finished.

Was this article helpful?