unable to implement php with nginx - php

I have nginx working with arch linux. I am having trouble adding php and fastcgi. I installed the php and php-fpm packages. php-fpm service is on. When I access a file with a php extension, the browser displays the nginx error page and I get the following nginx error log message added:
2013/01/02 22:39:43 [error] 721#0: *27 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.13, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.19"
the contents of the php file seem to make no difference. I have been unable to locate php error log or create my own. I added the following to /etc/php/php.ini:
error_log = /var/log/php_error.log
and I made the file world writable but nothing goes there. I added the following to /etc/nginx/nginx.conf:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi.conf;
}
uncommenting the commented lines changes nothing. Commenting the uncommented lines changes the browser page displayed from error page to file not found page.
Any help would be greatly appreciated. Thanks in advance.

Try changing your configuration file to this:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}

I figured it out. I had to uncomment a line in the php-fpm.conf file to have fastcgi listen on the right port.

Related

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

I am trying to install nginx and php-fpm docker images in AWS Fargate without any success running out of options, what I see is either the nginx container unable to route the traffic to php container or php container unable to read files from EFS file system.
I have EFS file system holding sample index.html and index.php files in root folder and this EFS is shared with two containers nginx and php-fpm mounted from volumes in AWS Task definition creation wizard.
when I navigate to the ELB friendly DNS name I can successfully load index.html page but not index.php page I get "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: *.amazonaws.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000".
Changed upstream value to php:9000; or 127.0.0.1:9000; or resolver 127.0.0.11 none of them worked.
I got same error locally but if I hardcore fastcgi upstream and server_name fields with actual IP address in default.conf and listen=ipaddress of phpcontainer:9000 in www.conf file in pgp-fpm container this resolved the issue locally but in AWS fargate the IP's all ways change after restarting container any advise appreciated.
server {
listen 80;
#root /usr/share/nginx/html;
#local testing for docker-compose up
root /php;
index index.php index.html index.htm;
#server_name *.amazonaws.com;
server_name x.x.x.x; #local testing
#set $upstream 127.0.0.1:9000;
set $upstream x.x.x.x:9000; #local testing
location / {
try_files $uri $uri/ =404;
}
location /dataroot/ {
internal;
alias /usr/share/nginx/html/moodledata/;
}
location ~ ^(.+\.php)(.*)$ {
#root /usr/share/nginx/html;
#local testing
root /php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_index index.php;
fastcgi_pass $upstream;
include /etc/nginx/mime.types;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Finally figured out the issue after trying multiple combinations, its causing from root path which is different to each container nginx (/usr/shr/nginx/html) and php (/var/www/html)....the error message doesnt help for newbies working in php and linux world.
After changing the nginx root path as /var/www/html same as php-fpm container /var/www/html the issue was resolved of course the permissions has to be taken care of if your accessing the EFS volume mounts.

Nginx throwing 502 Bad Gateway on blog (subdomain)

So, I was trying to setup mysite.com/blog initially, but got pretty fed up with it, so i setup blog.mysite.com instead.
It successfully loads the index file: index.htm, but if i try to access a file info.php it fails saying: 502 bad gateway, i checked /var/log/nginx/error.log and it says:
2016/12/17 09:24:13 [error] 1042#0: *4 connect() failed (111:
Connection refused) while connecting to upstream, client: x.xx.xx.xx,
server: blog.mysite.com, request: "GET /info.php HTTP/1.1", upstream:
"fastcgi://127.0.0.1:9000", host: "blog.mysite.com"
I installed php via: sudo apt-get install php5-fpm php5-mysql from this tutorial: link
My nginx config in /etc/nginx/sites-enabled/myblog is:
server {
listen 80;
root /home/www/flask-deploy/myblog;
fastcgi_index index.php;
index index.html index.htm index.nginx-debian.html;
server_name blog.mysite.com www.blog.mysite.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
What am I doing wrong?
Thank you so much!
First of all, your nginx config has bad path to php-fpm.sock. (You error.log is right ;) )
1) Which PHP version are you using? Use:
php -v
2) Be sure php-fpm is installed it is very important for nginx
sudo apt-get install php-fpm
3) Set correct path to php-fpm
For example, I use PHP7.0 and my path is:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
Some my projects are running on PHP5.6 and path is:
fastcgi_pass 127.0.0.1:9000;
4) Restart nginx and php-fpm (php5.6-fpm, php7.0-fpm...)
sudo service nginx restart
sudo service php5.6-fpm restart

unable to get hhvm working with nginx

I have nginx and hhvm installed and running as processes on a ubuntu VM. The configs are copied below. I have an index.php file in /usr/share/nginx/html (pointed to by nginx) but when I try to access the site beign served by nginx, I get the following error about hhvm:
/var/log/nginx/error.log
2016/10/04 12:03:05 [crit] 12443#0: *1 connect() to unix:/var/run/hhvm/hhvm.sock failed (2: No such file or directory) while connecting to upstr
eam, client: xx.xxx.xxx.xxx, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/hhvm/hhvm.sock:", host: "xx.xx.xx.xx"
Nginx config (part of config)
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
index index.php;
try_files $uri $uri/ $uri/index.php$args /index.php$args;
}
location /mysql {
index index.php;
}
location ~* \.(php)$ {
try_files $uri = 404;
location ~ \..*/.*\.php$ {return 404;}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have checked - bothh nginx and hhvm processes are running. Any thoughts on how to troubleshoot this further?
Update:
I changed the server.ini file in /etc/hhvm to the following and restarted the service but it does not appear to have done the job
hhvm.server.file.socket=/var/run/hhvm/hhvm.sock
I commented out the 9000 port being used previously.
Update:
After making the server.init change and restarting hhvm, I get the following error in /var/log/hhvm/error.log:
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to start page server
Shutting down due to failure(s) to bind in HttpServer::runAndExitProcess
The /var/run/hhvm directory is owned by www-data
I ended up running hhvm on port 9000 and pointed nginx (fasgcgi_pas) to point to the port (instead of using the file socket option).

502 Bad Gateway with PHP7 and Nginx 1.9.9

I'm getting a 502 Bad Gateway error with PHP7 and nginx 1.9.9 installed on Ubuntu 14.04 when I try to access any .php files. .html files load as expected.
I've updated the default.conf to:
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.html index.htm index.php;
server_name localhost;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I've updated php.ini with cgi.fix_pathinfo = 0 and then rebooted the server, and am still getting the 502 error with all .php files. I have checked to ensure php7.0-fpm.sock is installed and in the proper location.
This is the error I'm getting from the nginx log 2016/01/19 19:14:54 [error] 1466#1466: *1 open() "/usr/share/nginx/html/xmlrpc.php" failed (2: No such file or directory), client: 85.159.237.13, server: localhost, request: "POST /xmlrpc.php HTTP/1.0", host: "my.ip.address"
I've searched for the answer for quite a while and I'm out of ideas. Does anyone have any suggestions?
This is mostly because your nginx and php7.0-fpm were not run under the same user. Edit nginx.conf and change "user nginx" to "user www-data"
By the way, "client: 85.159.237.13", that was a script boy, I think.

nginx rewrite issues

I just switched from apache to nginx, just for testing and I experience the following problem. I am using this config for nginx
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Now I have a php script makethumbs.php that automatically resize images displayed on my website. With apache, works just fine. With nginx I get this error:
2011/12/29 15:13:17 [error] 15548#0: *9 open() "/usr/share/nginx/html/makethumbs.php/0737438664-22.jpg" failed (20: Not a directory), client: 193.138.192.81, server: www.escortele.eu, request: "GET /makethumbs.php/0737438664-22.jpg?width=48&height=64&image=/members/escorte/0737438664-22.jpg HTTP/1.1", host: "escortele.eu:88", referrer: "http://escortele.eu:88/"
The problem is that it sees makethumbs.php as a directory and it should be a script not a directory.
I can't figure it out what rewrite rule to use, only for makethumbs.php so it acts like a script and not like a directory.
You should have pasted the rest of your configuration file because what you pasted has nothing to do with what you want.
That error is because you have "try_files $uri $uri/ /index.php;" somewhere in your config. You need to remove the $uri/ from that to fix the error you pasted.
The problem is not in
location / {
try_files $uri $uri/ /index.php;
}
That part is good. You need it.
Since you didn't post your entire config I can't be sure what you're problem is, but I can tell you what my problem was in the same type of situation. Following the recomendation of others, I had a location defined for image files as follows:
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
That was the problem. Because Nginx uses the most specific location that matches, the URL ending with .jpg would match this location and this location doesn't tell it to use index.php. I just got rid of this location and it worked.
Additional tips:
define your root in the server block and not in location blocks.
I don't think you don't need "fastcgi_index index.php;"
You don't need to hard-code your document root:
change
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
to
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
This is the script I am using for thumbnails: http://escorte.pro:88/makethumbs.txt
My nginx config is as follows: http://escorte.pro:88/nginx.txt
The error I get is:
2014/08/27 14:59:07 [error] 20986#0: *86 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0737835261-79.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0737835261-79.jpg?width=48&height=64&image=/members/escorte/0737835261-79.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
2014/08/27 14:59:07 [error] 20986#0: *87 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0743844296-60.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0743844296-60.jpg?width=48&height=64&image=/members/escorte/0743844296-60.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
The makethumbs.php script works perfectly fine on apache
Any clues?
I hope this info si more complete than the previous...

Categories