1

Right, sorry for the previous, recent questions I've asked about nginx, this is just doing my head in.

I've enabled PHP using fastcgi_php and it works just fine, however, when I access to a PHP file, e.g.

http://domain.com/info.php

(which applies to var/www/domain.com/www/info.php)

Which includes the phpinfo() and nginx throws an 404 error... if I access to a normal file, e.g.

http://domain.com/index.html

It seems fine... how come nginx won't process with the PHP files?

This is the etc/nginx/sites-enabled/domain.com.

server {
    listen   80;
    server_name  domain.com *.domain.com;

    access_log /var/www/domain.com/logs/nginx_access.log;
    error_log  /var/www/domain.com/logs/nginx_error.log;

    root   /var/www/domain.com/www/;

    location / {
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/nginx-default;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }

    # Enable PHP
    include /etc/nginx/fastcgi_php;
}

I have no clue what to do further...

EDIT:

My /etc/nginx/fastcgi_php

location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php-fastcgi/php.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}
MacMac
  • 1,931
  • 8
  • 30
  • 38

2 Answers2

1

You have fundamentally misunderstood how PHP works with Nginx. Your assumption that Apache == Nginx is incorrect. Nginx does not embed PHP within itself so if you try to access a PHP file then it's PHP throwing the 404 and not Nginx.

Sadly you have hidden your PHP configuration with include /etc/nginx/fastcgi_php; so I can't actually tell you if you've configured Nginx correctly, so please do provide your full configuration.

Meanwhile, you can verify that PHP is actually throwing the 404 by checking the returned headers and checking if you have X-Powered-By: PHP/5.3.8. If this is present then you need to verify that you're sending PHP the proper file path via the SCRIPT_FILENAME fastcgi_param. If that one is correct then PHP cannot read the file due to improper permissions, this can either be read access on the file itself or execute access on any of the parent directories.

Martin Fjordvald
  • 7,589
  • 1
  • 28
  • 35
1

First make sure you have installed php-cgi apt-get install php5-cgi. Next create a startup script for fastCGI PHP.

Create the file /etc/init.d/fastcgi-php with the following content:

#!/bin/bash
BIND_DIR=/var/run/php-fastcgi
BIND="$BIND_DIR/php.sock"
USER=www-data
PHP_FCGI_CHILDREN=8
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
    echo -n "Starting PHP FastCGI: "
    mkdir $BIND_DIR
    chown -R $USER $BIND_DIR
    start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
    RETVAL=$?
    echo "$PHP_CGI_NAME."
}
stop() {
    echo -n "Stopping PHP FastCGI: "
    killall -q -w -u $USER $PHP_CGI
    RETVAL=$?
    rm -rf $BIND_DIR
    echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
        start
  ;;
    stop)
        stop
  ;;
    restart)
        stop
        start
  ;;
    *)
        echo "Usage: php-fastcgi {start|stop|restart}"
        exit 1
  ;;
esac
exit $RETVAL

Finally run the following commands (all as root if in Debian, or with sudo if in Ubuntu)

chmod 755 /etc/init.d/fastcgi-php
update-rc.d fastcgi-php defaults
/etc/init.d/fastcgi-php start

and

/etc/init.d/nginx reload

Now check if the info.php file shows up correctly.

Otherwise please post the output of the domains error log and access log.

George Tasioulis
  • 1,969
  • 2
  • 16
  • 17
  • It works! However, I got **two** errors when doing your steps `update-rc.d: warning: /etc/init.d/fastcgi-php missing LSB information` and - `/etc/init.d/fastcgi-php: line 1: !#/bin/bash: No such file or directory`. Is there anything to worry about? – MacMac Sep 30 '11 at 16:37
  • One another thing, if access to a PHP file that doesn't exist, I get this output in my browser: `No input file specified.` – MacMac Sep 30 '11 at 16:39
  • Sorry my fault: The first line should be `#!/bin/bash` instead of `!#/bin/bash` I also corrected it in my answer. – George Tasioulis Sep 30 '11 at 16:41
  • Regarding the `No input file specified` try adding `error_page 404 /404.html;` above `error_page 500 502 503 504 /50x.html; in your `/etc/nginx/sites-enabled/domain.com` file and restart nginx. – George Tasioulis Sep 30 '11 at 16:46
  • I did that, didn't prevent the `No input file specified` error. The file also exists in `/var/www/domain.com/www/404.html`. – MacMac Sep 30 '11 at 17:00
  • Another try: can you add a new line with `fastcgi_intercept_errors on;` on your `/etc/nginx/fastcgi_php` file right above `fastcgi_pass unix:/var/run/php-fastcgi/php.sock;` ? Restart nginx and check again. – George Tasioulis Sep 30 '11 at 17:16
  • Nice, glad it worked :-) – George Tasioulis Sep 30 '11 at 17:19
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/1483/discussion-between-youbook-and-george-tasioulis) – MacMac Sep 30 '11 at 20:27
  • I need a little help with something, if that's not a problem? – MacMac Sep 30 '11 at 20:27
  • Really? php-cgi? Is his a joke or something? At the very least you should recommend php-fpm – Martin Fjordvald Sep 30 '11 at 22:02
  • @MartinF man check his question first... he already HAD php-cgi installed. I just went on with his current configuration. – George Tasioulis Oct 01 '11 at 01:18
  • Check out your own answer: "First make sure you have installed php-cgi apt-get install php5-cgi. Next create a startup script for fastCGI PHP." This is still one of those "Shoot yourself in the foot" answers. – Martin Fjordvald Oct 01 '11 at 09:41