NGINX: connect() to unix:/var/run/php7.0-fpm.sock failed (2: No such file or directory)

126,036

Solution 1

Had the same problem. Solution is very easy.

In nginx conf file you are trying upstreaming to

unix:/var/run/php7.0-fpm.sock

Correct path is

unix:/var/run/php/php7.0-fpm.sock


There is a mention about this in the documentation

Nginx communicates with PHP-FPM using a Unix domain socket. Sockets map to a path on the filesystem, and our PHP 7 installation uses a new path by default:

PHP 5 /var/run/php5-fpm.sock

PHP 7 /var/run/php/php7.0-fpm.sock

Solution 2

In Ubuntu 18.04 the problem for me was that it's currently using PHP 7.2, but the sites-available default file has:

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

Updating the version on that line so that it's the 7.2 instead of 7.0 fixed the issue for me.

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

Solution 3

Check status:

sudo service php7.0-fpm status

OR

sudo systemctl status php7.0-fpm

Try with if already running

sudo service php7.0-fpm restart

OR

sudo systemctl reload php7.0-fpm

Else try with

sudo service php7.0-fpm start

OR

sudo systemctl start php7.0-fpm

Solution 4

Edit your /etc/php/7.0/fpm/pool.d/www.conf file and find the following line:

listen = 127.0.0.1:9000

And comment it out or replace it with the following:

listen = /var/run/php7.0-fpm.sock

Solution 5

In /etc/nginx/nginx.conf

user nginx;

If you web server work at user www-data need write

user www-data;

Share:
126,036

Related videos on Youtube

kramer65
Author by

kramer65

Updated on July 09, 2022

Comments

  • kramer65
    kramer65 almost 2 years

    I'm trying to follow this Ansible tutorial while adjusting it for Ubuntu 16.04 with php7. Below this message you'll find my Ansible file. After running it and trying to visit the page in the browser I get a 404, and the following in the nginx error logs:

    2016/10/15 13:13:20 [crit] 28771#28771: *7 connect() to unix:/var/run/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.xxx.xxx.xx, server: 95.xx.xx.xx, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7.0-fpm.sock:", host: "95.xx.xx.xx"

    So I checked if the socket file exists, and it seems to exist, but ls behaves weird:

    $ sudo ls -l /var/run/php
    total 4
    -rw-r--r-- 1 root     root     5 Oct 15 13:00 php7.0-fpm.pid
    srw-rw---- 1 www-data www-data 0 Oct 15 13:00 php7.0-fpm.sock
    $ sudo ls -l /var/run/php7
    ls: cannot access '/var/run/php7': No such file or directory
    $ sudo ls -l /var/run/php7.0-fpm.sock
    ls: cannot access '/var/run/php7.0-fpm.sock': No such file or directory
    

    Why can ls find the socket file if I search it by part of the name php while it cannot find the socket file when I list more than that php7 or even the full name php7.0-fpm.sock?

    And most importantly, how can I make this work with nginx? All tips are welcome!

    below I pasted my Ansible file

    ---
    - hosts: php
      become: true
    
      tasks:
      - name: install packages
        apt: name={{ item }} update_cache=yes state=latest
        with_items:
          - git
          - mcrypt
          - nginx
          - php-cli
          - php-curl
          - php-fpm
          - php-intl
          - php-json
          - php-mcrypt
          - php-mbstring
          - php-sqlite3
          - php-xml
          - sqlite3
    
      - name: enable mbstring
        shell: phpenmod mbstring
        notify:
          - restart php7.0-fpm
          - restart nginx
    
      - name: create /var/www/ directory
        file: dest=/var/www/ state=directory owner=www-data group=www-data mode=0700
    
      - name: Clone git repository
        git: >
          dest=/var/www/laravel
          repo=https://github.com/laravel/laravel.git
          update=no
        become: true
        become_user: www-data
        register: cloned
    
      - name: install composer
        shell: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
        args:
          creates: /usr/local/bin/composer
    
      - name: composer create-project
        composer: command=create-project working_dir=/var/www/laravel optimize_autoloader=no
        become: true
        become_user: www-data
        when: cloned|changed
    
      - name: set APP_DEBUG=false
        lineinfile: dest=/var/www/laravel/.env regexp='^APP_DEBUG=' line=APP_DEBUG=false
    
      - name: set APP_ENV=production
        lineinfile: dest=/var/www/laravel/.env regexp='^APP_ENV=' line=APP_ENV=production
    
      - name: Configure nginx
        template: src=nginx.conf dest=/etc/nginx/sites-available/default
        notify:
          - restart php5-fpm
          - restart nginx
    
      handlers:
        - name: restart php7.0-fpm
          service: name=php7.0-fpm state=restarted
    
        - name: restart nginx
          service: name=nginx state=restarted
    
        - name: reload nginx
          service: name=nginx state=reloaded
    
    • oneindelijk
      oneindelijk over 6 years
      You are listing the contents of the directory with ls -l /var/run/php. Try ls -l /var/run/php/php7.0-fpm.sock
  • Vincent Decaux
    Vincent Decaux over 6 years
    Nice one ! works for me on Debian 8 + php7.2-fpm. A lot of sites don't use this path and it didn't work.
  • Sergio Belevskij
    Sergio Belevskij over 5 years
    check your php socket: ls /var/run/php*/**.sock and use this output
  • Peter Krauss
    Peter Krauss almost 5 years
    What is "nginx conf file"? Plase complete path file name or regex for it... It is /etc/nginx/nginx.conf?
  • Peter Krauss
    Peter Krauss almost 5 years
    About @SergioBelevskij ls, my showing modern /var/run/php/php7.2-fpm.sock... use it instead indecated?
  • Wojciech Jakubas
    Wojciech Jakubas over 4 years
    Status gave me Active: failed for some reason. I had to run sudo service php7.2-fpm start for service to start. What could put php service in Active: failed mode? Not a clue.
  • itsazzad
    itsazzad over 4 years
    @WojciechJakubas You could check the log and also in the journal
  • Frederick Álvarez
    Frederick Álvarez over 3 years
    on centos7 it is systemctl status php-fpm
  • Oleg Reym
    Oleg Reym over 2 years
    Twice saved me! Thanks a lot!