0 votes

Execute a script PHP

I have a script that imports data from one system to another so it is a time consuming system as it transfers almost 300,000 records.

For this I created a PHP class using the laravel framework but I get a 504 error when executing the script.

Gateway Timeout The gateway did not receive a timely response from the upstream server or application.

Apache/2.4.29 (Ubuntu) Server at local Port 80

So what I do is, in the constructor class I set a series of php parameters, but with all that I still get the error.

The class code looks like this:

class CarsController extends Controller
{

function __construct() {
    ini_set('max_execution_time', 999999);
    ini_set('max_input_time', 999999);
    ini_set('default_socket_timeout', 999999);
    ini_set('memory_limit','4096M');

    parent::__construct();
}

public function readMigration()
{
//Realizar las acciones SQL
}
}

The php configuration I use php7.2-fpm and I have the following configuration

; Default timeout for socket based streams (seconds)
; http://php.net/default-socket-timeout
default_socket_timeout = 60

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 36000

; Maximum amount of time each script may spend parsing request data. It's a good
; idea to limit this time on productions servers in order to eliminate unexpectedly
; long running scripts.
; Note: This directive is hardcoded to -1 for the CLI SAPI
; Default Value: -1 (Unlimited)
; Development Value: 60 (60 seconds)
; Production Value: 60 (60 seconds)
; http://php.net/max-input-time
max_input_time = 36000

; Maximum input variable nesting level
; http://php.net/max-input-nesting-level
;max_input_nesting_level = 64

; How many GET/POST/COOKIE input variables may be accepted
; max_input_vars = 1000

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = -1

1voto

OscarGarcia Points 15361

The problem you have is in the FCGI connection that exists between the Apache server and the PHP FPM that puts a timeout of 30 seconds maximum in the reception of data.

In Ubuntu this file is located in /etc/apache2/conf-available/php7.2-fpm.conf o /etc/apache2/conf-available/php7.1-fpm.conf and contains:

# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        # Deny access to raw php sources by default
        # To re-enable it's recommended to enable access to the files
        # only in specific virtual host or directory
        Require all denied
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

You must change the line that reads:

SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"

For the following (to increase the time to 30 minutes):

SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost timeout=1800"

Or configure the following directives instead of the block <FilesMatch> :

<Proxy "unix:/run/php/php7.1-fpm.sock|fcgi://localhost">
   ProxySet timeout=1800
</Proxy>
<FilesMatch ".+\.ph(ar|p|tml)$">
   SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"
</FilesMatch>

I have been able to test this latest edition, but I still recommend that you do this type of work through the CLI and not through the web server. Increasing the maximum timeout can trigger problems later. As a general rule a web page should be generated in the shortest possible time.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X