search
HomeBackend DevelopmentPHP ProblemWhat to do if php-cgi stops under Linux

What to do if php-cgi stops under Linux

Sep 26, 2021 pm 04:32 PM
linuxphp-cgi

The solution to php-cgi stopping under Linux is: change the nginx configuration item, reduce the number of FastCGI requests, and try to keep the buffers unchanged, such as [fastcgi_buffer_size 128k;].

What to do if php-cgi stops under Linux

#The operating environment of this article: linux 5.9.8, php 7, thinkpad t480 computer.

Foreword:

I accidentally discovered that a server in production (centos 5.2 64-bit 4G memory) always had the problem that the website could not be opened. Later, I went to the server to check and found that both nginx and php-cgi were running, but the cpu usage of php-cgi was 0 at this time. So I immediately checked the file handle limit:

ulimit -n

The result was: 1024, which is too small for services in production.

Most of the solutions given on the Internet are to directly enter

ulimit -SHn 51200 # 51200可自己根据应用调整

The disadvantage of this method is obvious. Once you log out, the settings will become invalid.

It is also said that the command should be written directly to /etc/rc.d/rc.local. Today I found a correct way.

Open /etc/security/limits.conf, inside There are very detailed comments. Find the following settings (insert if not)

The code is as follows

* soft nofile 51200
* hard nofile 51200

Log in again after exiting and check the number of handles. It has been correctly set to 51200.

If you encounter a similar situation, you can check to see if the number of file handles is set too small.

Solution to 502 Bad Gateway when accessing

Nginx 502 Bad Gateway means that the requested php-cgi has been executed, but for some reason (usually a problem with reading resources) The execution is not completed and the php-cgi process is terminated. Generally, websites with too high concurrency are prone to this error. There are many reasons why 502 Bad Gateway appears, but most people can solve it by modifying the parameters below.

Change several configuration items of nginx, reduce the number of FastCGI requests, and try to keep the buffers unchanged:

The code is as follows

fastcgi_buffer_size 128k;
fastcgi_buffers 2 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

Open /usr/local/php/etc /php-fpm.conf file, modify the following parameters:

The code is as follows

25
requests">2048
65535
30s
60s

request_terminate_timeout refers to the execution script time of fast-cgi, which defaults to 0s. The meaning of 0s is to let php-cgi continue to execute without time limit. If you set it to 0s here, then when a 502 Bad Gateway appears, the 502 status will continue and will not change. But if you set it to 5s, then php-cgi will automatically recover after 5s. This value can be set according to the performance of your server. Here I set it to 60s.

max_children represents the processing process of php-cgi. If max_children is set to a smaller value, such as 5-10, then php-cgi will be "very tired", the processing speed will be very slow, and the waiting time will be longer. If a request is not processed for a long time, a 504 Gateway Time-out error will occur. Setting max_children also needs to be set according to the performance of the server. As the number of processes increases, the memory usage will increase accordingly. Under normal circumstances, the memory consumed by each php-cgi is about 20M. Here I set it to 25.

The default static processing method of php-fpm will cause the php-cgi process to occupy memory for a long time and cannot be released. This is also one of the reasons for nginx errors, so you can change the processing method of php-fpm to apache -like mode.

After the modification is completed, execute lu-restart (LuManager server management system).

Recommended learning: php training

The above is the detailed content of What to do if php-cgi stops under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.