Home  >  Article  >  Backend Development  >  How to solve the problem of PHP-CGI process automatically closing

How to solve the problem of PHP-CGI process automatically closing

PHPz
PHPzOriginal
2023-03-29 11:28:511384browse

Linux PHP-CGI process automatically closes

The PHP-CGI process is a very common way to run PHP scripts, but sometimes these processes will automatically close, causing the website to not run properly. This article will introduce how to solve the problem of PHP-CGI process automatically closing.

  1. View Logs

First, you need to check your system logs to determine why the PHP-CGI process shut down automatically. In most Linux distributions, system log files are located in the /var/log directory. You can use the following command to view recent system logs:

$ sudo tail -f /var/log/syslog

If the PHP-CGI process automatically shuts down due to insufficient memory or other resources, you will see information similar to the following in the log file:

Apr 15 14:35:26 myserver kernel: Out of memory: Kill process 1234 (php-cgi) score 5678 or sacrifice child

The above information indicates that the system has killed the PHP-CGI process to release memory.

  1. Adjust the process limit

If your system automatically shuts down the PHP-CGI process due to insufficient resources, you can solve the problem by adjusting the process limit. Process limits can be adjusted using the ulimit command. For example, the following command will increase the maximum amount of memory that can be used by each process to 512MB:

$ ulimit -v 524288

You can add this command to the PHP-CGI process startup script to immediately start the PHP-CGI process Take effect.

  1. Adjust the CGI timeout

In addition, if your PHP-CGI process automatically shuts down because it runs for too long, you can solve this by adjusting the CGI timeout. question. CGI timeout can be set in your web server configuration file. For example, the following Nginx configuration file sets the CGI timeout to 30 seconds:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 30;
    include fastcgi_params;
}

You can set fastcgi_read_timeout to an appropriate value that your PHP application requires.

  1. Use Process Manager

Finally, if you encounter an unresolved problem of automatic shutdown of the PHP-CGI process, you may consider using Process Manager. Process Manager can automatically monitor PHP-CGI processes and automatically restart them after they crash or stop. Common process managers include Supervisor and systemd.

Before using process managers, you need to understand how to use them. Taking Supervisor as an example here, you can use the following command to install it:

$ sudo apt-get install supervisor

After installation, you need to create a Supervisor configuration file, for example:

[program:php-cgi]
command=/usr/bin/php-cgi -b 127.0.0.1:9000
autostart=true
autorestart=true
redirect_stderr=true

The above configuration file can make Supervisor Automatically start and monitor PHP-CGI processes. You can modify the configuration file according to your PHP-CGI process settings.

Finally, you need to start Supervisor using the following command:

$ sudo systemctl start supervisor

Summary:

The PHP-CGI process is a common way of running PHP scripts in web servers. If your PHP-CGI process automatically shuts down, you can solve the problem by viewing system logs, adjusting process limits, adjusting CGI timeouts, and using the process manager. To ensure that the PHP-CGI process is always available, you can use a combination of the above methods.

The above is the detailed content of How to solve the problem of PHP-CGI process automatically closing. 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