Introduction

At the beginning of NginX installation, you may have some problems with PHP files that are being downloaded instead of executing. It’s because of misconfigurations. You need to review NginX and PHP-FPM Configurations Files. Some solutions on the web are not fully consequent. In this article, we are going to check all the possible actions for fixing the issue.

 

Solution

  1. Make sure FastCGI module has been installed on the server.
  2. Create a file named nginx_php.conf in the main NginX directory — If the file already exists, make a backup of the file:
    sudo nano /etc/nginx/nginx_php.conf
  3. Put/Append/Modify the following lines — to force NginX to handle PHP files using FastCGI module:
    location ~ \.php$
    {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
    if (-f $request_filename)
    {
    fastcgi_pass unix:/var/run/php-fpm/www.sock;
    }
    }
  4. Check PHP-FPM Configurations File by:
    sudo nano /etc/php-fpm.d/www.conf
  5. In this file, find and check whether the configuration lines below have been set correctly or not. If not, make a backup of the file www.conf, then, modify the following lines to the configurations below:
    user = nginx
    group = nginx
    
    listen = /run/php-fpm/www.sock
    
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
  6. OPTIONAL: Additionally, you can specify limits for FastCGI module by creating a new file — e.g. /etc/nginx/fastcgi_limits.conf — and don’t forget to include the file in the location tag in step 3:
    a) Modify the nginx_php.conf file:

    sudo nano /etc/nginx/nginx_php.conf

    b) Place the line below in the location tag, just before if condition:

    include /etc/nginx/fastcgi_limits.conf;

    c) Create/Open the file in an editor:

    sudo nano /etc/nginx/fastcgi_limits.conf

    d) Place the lines below to the file — you can change them to whatever you want:

    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    fastcgi_intercept_errors on;
    fastcgi_param HTTP_PROXY "";
  7. Restart NginX and PHP-FPM Servies by:
    sudo service nginx restart && sudo service php-fpm restart
  8. If the problem has not resolved and the server keeps getting Connection Timed Out error, try disabling SELinux.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments