Nginx downloads php instead of running it - php

Iv'e setup an Nginx php server on a linux REHL machine.
When accessing html files all goes well, but trying to access php file, the file is downloaded instead of being executed.
This is my nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
...and this is the server block:
server {
listen 80;
server_name {mywebsitename};
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html/{mywebsitename}/;
}
location /ngx_status_2462 {
stub_status on;
access_log off;
allow all;
}
location ~ \.php$ {
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/{mywebsitename}$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

It might be because of the mimetype you're sending:
default_type application/octet-stream;
See: http://mimeapplication.net/octet-stream

I just had this exact same problem. I was using Ubuntu 12.04 and Linux Mint 14 so different OS but likely to have the same issues.
A couple of issues may happening. Firstly, you need to have php5-fpm installed (FastCGI Process Manager). I was trying to run it with my standard version of PHP but it was not working - http://www.php.net/manual/en/install.fpm.php
I also had Apache installed, and even if it weren't running it must have had some conflict because once I uninstalled Apache I was able to execute the PHP files.
I would also look at this line
fastcgi_pass 127.0.0.1:9000;
And consider changing it to
fastcgi_pass unix:/var/run/php5-fpm.sock;
Here is a detailed guide to installation of Nginx and PHP5-FPM for RHEL (and other OS's)
http://www.if-not-true-then-false.com/2011/install-nginx-php-fpm-on-fedora-centos-red-hat-rhel/

You need to change the user to nginx instead of apache in this file a/etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx
and of course restart service php-fpm restart and service nginx restart

Comment out default_type application/octet-stream;

Related

Nginx & php-fpm - host unreachable

I have docker swarm containing one nginx and one php-fpm service. My problem is, that from other services in swarm, I randomly get error Failed to connect to nginx-fpm port 8081: Host unreachable.
nginx and fpm images are from official docker images with little config changes.
nginx-fpm dockerfile
FROM nginx:1.13.12-alpine
COPY ./nginx/config/ /etc/nginx/
ADD ./nginx/docker/entrypoint.sh /bin/
EXPOSE 8081
ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
nginx config
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
accept_mutex on;
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 8081;
server_name worker;
keepalive_timeout 30;
send_timeout 30s;
fastcgi_read_timeout 30s;
client_max_body_size 1024M;
root /app/www;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}
}
I tried event enabling nginx_status, but I can only see 1 active connection (or host unreachable).
It looks like to me, that nginx is only able to handle one connection at time, but I cannot find reason why... any help appreciated
How are you running your service in Swarm?
Generally, you need to expose the service port externally, something like this:
$ docker service create --name my_service \
--replicas 3 \
--publish published=8081,target=8081 \
nginx-fpm

OSX nginx php-fpm 7 setup

I have nginx running as confirmed by loading an html page.
The difficulty is running a .php page in same location, however this giving a 404.
fpm is shown to be installed okay using php-fpm -t
Therefore I am very sure its in .conf or a serverblock.
My goal is to set everything using serverblocks (aka VirtualHosts) as so much easier to manage differing projects so have attempted to strip nginx.conf to a minimal:
#user nobody;
worker_processes 1;
error_log /error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /usr/local/etc/nginx/sites-enabled/*;
}
and the serverblock
server {
listen 80;
server_name localhost;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /var/www;
index index.php index.html index.htm;
try_files $uri /index.php?&query_string;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 60;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
index.php fails simply with a message 'File not found' though not in the usual nginx format - just those words.
I had assumed this would be simple on a Linux based OS. It seems Apple have messed with it too much to make it viable.
I have expertise in this and have a Ubuntu based script which makes everything ready to go on this Linux flavour - apparently Apple no longer support developers.
Oh well lets see how long it takes to install a VirtualBox solution that I can edit on OSX.
This is an answer per se pending details from other people answering as I have had this working before - I just wanted a non proxy solution and was prepared to find a few days finding it. Alas Apple prevent it.

Nginx with PHP is functioning correctly but then it stops working

I'm on Windows 8.1 64 bits, PHP 5.6.12 VC11 x64 Thread Safe, nginx 1.9.3. I configured nginx with PHP the way I wanted too (I'm still learning) and I ran it. I opened index.php and it worked, the page displayed correctly. Then I closed the server, opened it again and it stopped working, it sent me the error page instead. This has happened before.
Here's my nginx.conf:
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
include fastcgi.conf;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 2;
#gzip on;
server {
listen 80 default_server;
server_name localhost;
root html;
index index.php;
#charset koi8-r;
access_log logs/host.access.log main;
location / {
try_files $uri $uri/ /index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~* \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
Here are the start bat script and js (I run the js):
JS:
var objShell = WScript.CreateObject("WScript.Shell");
var result = objShell.Run("cmd.exe /c start-nginx.bat", 0);
// Give some startup time
WScript.Sleep(3000);
// Navigate to homepage
objShell.Run("http://localhost");
BAT:
#ECHO OFF
c:\nginx\nginx.exe
c:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
ping 127.0.0.1 -n 1>NUL
echo Starting nginx
echo .
echo .
echo .
ping 127.0.0.1 >NUL
EXIT
Here are the stop bat and js (I run the JS too):
JS:
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c stop-nginx.bat", 0)
BAT:
#ECHO OFF
taskkill /f /IM nginx.exe
taskkill /f /IM php-cgi.exe
EXIT

install wordpress in osx with nginx and php failed

problem solved
I want to install wordpress osx.
These are what I done.
$ brew install nginx
$ brew install --without-apache --with-fpm --with-mysql php55
download latest wordpress.zip and extract to /var/www/wordpress_test
run php-fpm by php-fpm -g /usr/local/var/run/php-fpm.pid
write nginx config files like this.
/usr/local/etc/nginx/nginx.conf
worker_processes 1;
error_log /usr/local/log/nginx/error.log;
pid /usr/local/var/run/nginx.pid;
events {
worker_connections 256;
}
http {
include /usr/local/etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
#gzip on;
include /usr/local/etc/nginx/site-enabled/*;
}
/usr/local/etc/nginx/site-avaiable/wordpress_test
server {
listen 8080;
server_name localhost;
access_log /usr/local/log/nginx/wordpress_test.access.log;
error_log /usr/local/log/nginx/wordpress_test.error.log;
root /var/www/wordpress_test;
index index.html index.htm index.php;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location ~ /\.ht {
deny all;
}
}
create symlink /usr/local/nginx/site-available/wordpress_test to ` usr/local/nginx/site-enabled/wordpress_test
create /var/www/wordpress_test/info.php and write inside it <?php phpinfo(); ?>.
And I can see php info by accessing http://localhost:8080/info.php.
But I get error message when I access http://localhost:8080/index.php.
/usr/local/log/nginx/wordpress_test.error.log doesn't put any message.
and /usr/local/log/nginx/wordpress_test.access.log shows this message.
127.0.0.1 - - [23/Sep/2013:17:21:55 +0900] "GET /index.php HTTP/1.1" 500 537 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36"
How can I solve this problem? What should I check next?
--
I add settings like this from http://codex.wordpress.org/Nginx, then it works.
# WordPress single blog rules.
# Designed to be included in any server {} block.
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_intercept_errors on;
fastcgi_pass php;
}
This seems to be your .htaccess file not working with nginx, Have you converted your wordpress .htaccess file to nginx configuration ?
Please see the url http://winginx.com/htaccess for .htaccess to nginx conf converter.
just add a new location to your /usr/local/etc/nginx/site-avaiable/wordpress_test
location / {
try_files $uri /index.php$request_uri;
}

Nginx load balance with dedicated php-fpm server

I got server setup with nginx+php-fpm and mysql.
I have another server with only installed php-fpm, so wanted to use as load balance.
But when I am using this dedacted server with php-fpm as load balancer, I got error when opening page: "Access denied."
/etc/nginx/nginx.conf
user www-data;
worker_processes 3;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 64;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
#gzip on;
upstream php {
server dedicatedserverip:9000;
}
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/site.org.conf
server {
listen 81;
server_name site.org www.site.org;
access_log /var/log/nginx/site.org.log;
error_log /var/log/nginx/site.org.log;
root /home/www/site.org;
index index.php;
location ~ .php$ {
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/$fastcgi_script_name;
}
}
Why I got this error? When I change only the fastcgi_pass to 127.0.0.1:9000 - all work fine.
If it's a blank page with "Access denied" on it, it's caused by security.limit_extensions directive that has been added to php-fpm.
If you don't have it in your php-fpm configuration, it defaults to .php and prevents all other file types from being parsed by the PHP interpreter producing "Access denied" when trying to do so.
You received that error because the files PHP-FPM don't exist on the PHP-FPM server.
fastcgi_param SCRIPT_FILENAME /home/www/$fastcgi_script_name;
or (I use this because it's simpler for multiple vhosts)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
It seems Nginx simply provides the PHP-FPM server with the location of the file, and the PHP-FPM server then renders it. The simplest solution is to rsync the document root to the PHP-FPM server.
This post can explain details: http://code.google.com/p/sna/wiki/NginxWithPHPFPM

Categories