Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Opcache work in PHP?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the working principle of Opcache in PHP. I hope you will get something after reading this article. Let's discuss it together.

In PHP projects, especially in scenarios with high concurrency and high traffic, how to improve the response time of PHP is a very important work.

And Opcache is an indispensable component to optimize the performance of PHP, especially in projects that apply the PHP framework.

1. Overview

Before we understand the functions of OPCache, it is necessary to understand how PHP-FPM + Nginx works and how PHP scripts are interpreted and executed.

1.1 the working mechanism of PHP-FPM + Nginx

The request from Web browser to Nginx, and then to the completion of PHP processing, goes through the following five steps:

Step 1: start the service

Start PHP-FPM. PHP-FPM supports two communication modes: TCP socket and Unix socket

PHP-FPM starts two types of processes: the Master process, which is responsible for monitoring ports, assigning tasks, and managing the Worker process, and the Worker process, which is PHP's cgi program, which is responsible for interpreting, compiling and executing PHP scripts.

Start Nginx. First, the ngx_http_fastcgi_module module will be loaded, the FastCGI execution environment will be initialized, and the FastCGI protocol request agent will be implemented.

Note here that the worker process (cgi process) of fastcgi is managed by PHP-FPM, not Nginx. Nginx is just an agent.

Step 2: Request = > Nginx

Nginx receives the request and selects an appropriate handler based on the location configuration

This is the handler of the agent PHP

Step 3: Nginx = > PHP-FPM

Nginx translates the request into a fastcgi request

Master process sent to PHP-FPM through TCP socket/Unix Socket

Step 4: PHP-FPM Master = > Worker

The PHP-FPM master process received the request

Assign Worker process to execute PHP script, if there is no idle Worker, return 502error

The Worker (php-cgi) process executes the PHP script and returns a 504 error if it times out

The processing ends and the result is returned

Step 5: PHP-FPM Worker = > Master = > Nginx

The PHP-FPM Worker process returns the processing result, closes the connection and waits for the next request

The PHP-FPM Master process returns the processing result through Socket

Nginx Handler sequentially sends each response buffer to the first filter →, the second →, and so on, the → final response is sent to the client

1.2 mechanisms for interpreting and executing PHP scripts

Now that we understand the overall processing flow of PHP + Nginx, let's take a look at the specific execution flow of the PHP script

First, let's look at an example:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report