Nginx PHP setup - No input file specified - php

I know it's a very much known issue. I have tried almost everything but couldn't fix yet.
Intent is to create a blog using php/wordpress with already existing nginx as web server. Nginx is already being used as a web server to a Rails app.
Here's what my nginx.conf looks like
user centos;
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /home/centos/.rvm/gems/ruby-2.1.4#tripshelf/gems/passenger-5.1.2;
passenger_ruby /home/centos/.rvm/gems/ruby-2.1.4#tripshelf/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
rails_env staging;
server {
listen 80;
server_name xx.xxx.xxx.xx;
location / {
root /data/staging-tiger/current/public/;
passenger_enabled on;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/blog/;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
I followed digital ocean's tutorial on how to install LEMP stack.
After installation, when I hit http://example.com/info.php I get
No input file specified
On further research, I tried:
Setting the right permissions to document root - /data/blog with proper execute permissions to /, /data, /data/blog and /data/blog/info.php
php location block has its own index and root directives.
Nginx is running as centos user. Here's the output of ps aux | grep nginx
root 11510 0.0 0.1 53984 1320 ? Ss 07:06 0:00 nginx: master process /opt/nginx/sbin/nginx
centos 11513 0.0 0.2 54364 2612 ? S 07:06 0:00 nginx: worker process
centos 13471 0.0 0.0 103312 876 pts/1 S+ 07:42 0:00 grep nginx
User and Group are set to centos in /etc/php-fpm.d/www.conf file.
user = centos
group = centos
Running stat on Document root shows
File: /data/blog/info.php
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 525709 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 500/ centos) Gid: ( 500/ centos)
Access: 2017-10-19 16:47:54.528000890 +0000
Modify: 2017-10-19 16:47:54.528000890 +0000
Change: 2017-10-20 06:18:28.000001084 +0000
I have been wrapping my head around this but no breakthrough so far. Please help.
Thanks in advance

The guide followed wasn't for WordPress, so the nginx config is probably a little off.
Try amending the PHP block, notice the added fastcgi_param lines.
location ~ \.php$ {
root /data/blog/;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
If that fails, there might be something you can use from either
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
Or the more detailed
https://codex.wordpress.org/Nginx
Also, it should be generating the errors in the nginx log file.
clear && tail -f /var/log/nginx/error.log

Related

Nginx error : "Primary script unknown ", how can I resolve that ? Thank you

Config :
macOS Mojave
Debian 9.9.0 64bits on virtualbox 6.0.8, on port 192.168.56.50
php fpm 7.2 on debian
nginx/stable,now 1.16.0-1~stretch on debian
Nginx and php fpm have www-data as user.
Dirs :
lrwxrwxrwx www-data www-data /var/www/all -> /media/sf_web
drwxrwx--- www-data www-data /var/www
Config nginx :
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name _;
charset utf-8;
location / {
root /var/www/all/;
try_files $uri /index.html 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 /usr/share/nginx/html;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
When i do this command : sudo ls -l /var/www/all/ , i get :
drwxrwx--- 1 root vboxsf temp_converter
drwxrwx--- 1 root vboxsf myproject
I want to show projects folders under /media/sf_web using Firefox (or another web browser) but it doesn't work.
When i try to connect on this ip , nginx show "File not found" and in error log i see "Primary script unknow".
e.j :
http://192.168.56.50
http://192.168.56.50/myproject/index.php
You haven't set a global root statement, so Nginx will look for PHP files in the default root. You need to move the root statement from inside the location / block into server block scope.
The try_files statement is completely wrong.
Try:
root /var/www/all/;
location / {
try_files $uri $uri/ /index.php;
}
...
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

Nginx not running php files after pear install

I installed Nginx & PHP using this guide here:
Nginx install guide
yum install php php-mysql php-fpm
edited /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody
ran:
systemctl start php-fpm
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I then tested
http://<my server ip>/phpinfo.php
and it worked!
I then installed pear
yum install php-pear
but now php files don't work, the browser tries to download them instead of running them.
I've tried:
rebooting the server
restarting nginx
restarting php-fpm
checking all the config files to make sure they as the same as above.
I'm completely stuck. I don't know what to check to get php work again. This is the first time I've installed Nginx. I've looked around on the net for answers and on here.
I'm running Centos 7
help :)
UPDATE:
I've tried a much more shortened config file:
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
but that didn't work either.
I also tried:
fastcgi_pass 127.0.0.1:9000;
instead of:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
but that didn't work too.
UPDATE 2
i found out that if I go to:
http://my ip/phpinfo.php - it works!
but if I go to:
http://domainname/phpinfo.php - it tries to download the PHP file instead of running it.
How do I make php files run while using the domain name instead of the IP?
According your second update, you have problem with Listen directive. nginx listen only IP-address or Domain name that mentioned in Listen directive. Maximillian gives you right answer. It you put listen 80 in your configuration file, you'll solve your problem, but there would be wotk only one site, with this listen. If you would like to configure PHP on your domain you could configure listen yourdomain.com:80.
Have you tried to edit
/etc/nginx/sites-enabled/default ?
Then you need to edit it as follows.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
afterward, you have to restart php-fpm and nginx
service nginx restart && service php-fpm restart
Try with below config for location,
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;
}
And do clear cache beforehand.
For your 2nd update try below in server block,
listen 127.0.0.1:8000; #your ip with port
server_name example.com; #domain name
Recently I also need to setup linux and php environment, so I strictly follow your step to setup it.
I encounter this two issue when I setup the LEMP environment,
a. nginx issue, post and answer
b. php-fpm issue, post and answer
I don't encounter the issue you face, so I show the environment for you, you may compare with yours.
Centos Version
vi /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
Mysql version
mysql -u root -p
//enter password
Server version: 5.5.52-MariaDB MariaDB Server
Php version
php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Pear info
pear list // refer to this post
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.11 stable
Console_Getopt 1.3.1 stable
PEAR 1.9.4 stable
Structures_Graph 1.0.4 stable
XML_Util 1.2.1 stable
Nginx config file
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.236.129;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
info.php vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>

PHP-FPM sending empty response with Nginx on macOS

I installed nginx 1.10.3 and php 5.5.38 as a development server on macOS 10.12.4
When I try a test php file in my browser the body is empty but the response headers seem ok:
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Wed, 29 Mar 2017 11:35:21 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.38
There are no errors in php-fpm.log or nginx/error.log
my nginx.conf has:
server {
listen 80;
server_name wordpress.bob;
root /Users/mark/Sites/wordpress;
include /usr/local/etc/nginx/global_restrictions.conf;
include /usr/local/etc/nginx/wordpress.conf;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/usr/local/var/run/php-www.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
wordpress.bob is a local hostname for testing pointing to 127.0.0.1 in etc/hosts
php-fpm.conf has:
listen = '/usr/local/var/run/php-www.sock'
Any ideas what I'm doing wrong?
It's hard to help without the ability to read all the configuration files.
You just posted one, not the included ones nor php-fpm.conf. This is not a disapproval (a wall of configuration files is not quite appropriate in a question) but it's just to point out that the configuration file we "don't see" may differ depending on installation.
Anyway I see some differences from the configuration file I have on a server for a wordpress site.
Here are some hints considering that as you don't get any errors php-fpm is running and nginx can "communicate" to it via the socket (otherwise you would get a bad gateway error).
At the beginning...
server {
listen 80;
server_name wordpress.bob;
root /Users/mark/Sites/wordpress;
index index.php; # <-- ADD THIS
Make sure in the included wordpress.conf you have
location / {
try_files $uri $uri/ /index.php?$args;
}
The last part...
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
fastcgi_max_temp_file_size 0;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 5s;
fastcgi_read_timeout 5s;
include fastcgi.conf; # <--- fastcgi.conf, NOT fastcgi_params
fastcgi_pass /usr/local/var/run/php-www.sock;
}
The difference between fastcgi.conf and fastcgi_params (on my installation) is just one line:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
If this line is missing php code is not able to read $_SERVER['SCRIPT_FILENAME'] and (I think) this may break wordpress code resulting in empty output.
Finally make sure php-fpm worker processes have privileges to access /usr/local/var/run/php-www.sock
Usually the socket has the same owner:group of the workers.
The workers user and group is set in php-fpm.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.
user = ......
group = ......
To install NGINX with Homebrew :
$ brew install nginx
Run NGINX :
$ sudo nginx
Test the localhost nginx :
http://localhost:8080
NGINX configuration file should be in :
$ /usr/local/etc/nginx/nginx.conf
If you want to change the default port :
$ sudo nginx -s stop
$ vim /usr/local/etc/nginx/nginx.conf
Change the : listen 8080;
To : listen 80;
To save and Conf and start NGINX run :
$ sudo nginx
Then, according to your problem, you might simply be pointing to a an empty PHP file. Try to print a phpinfo() then look for "DOCUMENT_ROOT" to see where it goes.

403 Forbidden when file permissions are correct

I'm trying to create a server on nginx with multiple laravel (a php framework) sites under the same subdomain labs.rasouza.com.br/cdm, labs.rasouza.com.br/app2, labs.rasouza.com.br/app3, etc.
nginx and php are properly configured to run as web user, and my root folder is under /home/web:
drwxrwxrwx 5 web web 4.0K Aug 18 13:56 cdm
-rwxr-xr-x 1 web web 24 Aug 12 02:53 info.php
drwxr-xr-x 13 web web 4.0K Aug 12 04:15 owncloud
drwxr-xr-x 2 web web 4.0K Aug 18 15:23 tmp
My sites-available are configured like this:
server {
listen 80;
root /home/web;
index index.php index.html index.htm;
server_name labs.rasouza.com.br;
access_log /home/web/tmp/labs.access.log;
error_log /home/web/tmp/labs.error.log;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/cdm/(.+\.php)$ {
alias /home/web/cdm/current/public/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Whenever I try to access labs.rasouza.com.br/cdm I get the 403 Forbidden page and the error log is: directory index of "/home/web/cdm/" is forbidden. When I try to access info.php it goes ok. File permissions are also ok, web user owns everything
What am I doing wrong?

Can't get Baikal running in a subdirectory

I try to install Baïkal on a dedicated host with the "regular package". I am using Nginx as webserver but I can't get it running. The official docs are only dedicated to run Baikal on a subdomain (http://baikal.mydomain.com) instead in a subdirectory (http://mydomain.com/baikal). When I open http://mydomain.com/baikal/card.php/addressbooks/IstMe/default/ I only get a "File not found". Any help would be appreciated.
My nginx.conf looks like this one:
location /baikal {
alias /usr/share/webapps/baikal/html;
index index.php;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
location ~ ^/baikal/(.+\.php)$ {
alias /usr/share/webapps/baikal/html/$1;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
location ~* /baikal/(\.ht|Core|Specific) {
deny all;
return 404;
}
I'd got the same problem. The folowing very simple instance configuration from this article worked great for me:
server {
listen [::]:443 ssl;
server_name yourdomain.tld;
root /usr/share/nginx/baikal/html;
index index.php;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
charset utf-8;
location ~ /(\.ht|Core|Specific) {
deny all;
return 404;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
quite an old post but i've been redirected here searching for a solution to the very same problem ^^
the're's a post about this issue and a possible solution.
here is the configuration for NGINX (this is cut&paste, it is not my work):
location ^~ /baikal { # triggers location of baikal installation, and stop looking for other matches
index index.php;
charset utf-8;
# curiosity killed the cat
location ~ ^/baikal/(?:\.ht|Core|Specific) {
deny all;
}
# this corresponds to the recommended regex for matching php files
# and piping it to php-fpm
location ~ ^(.+\.php)(.*) {
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# case insensitive matching of static files for maximum caching time
location ~* \.(?:jpg|gif|ico|png|css|js|svg)$ {
expires max; add_header Cache-Control public;
}
}
i use apache so i had no way to test it but this is the starting point i'm using to solve the problem on my web server.
Have you tried to create 2 symlinks from root to html directory like that :
cd /var/www/baikal
sudo ln -s html/card.php card.php
sudo ln -s html/cal.php cal.php
Which should gave that result :
ls -lah /var/www/baikal
total 72K
drwxrwxr-x 6 www-data www-data 4,0K nov. 19 12:40 .
drwxr-xr-x 25 www-data www-data 4,0K nov. 19 12:54 ..
lrwxrwxrwx 1 root root 12 nov. 19 12:40 cal.php -> html/cal.php
lrwxrwxrwx 1 root root 13 nov. 19 12:40 card.php -> html/card.php
This seems to work for my installation.

Categories