PHP-FPM sending empty response with Nginx on macOS - php

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.

Related

Updating PHP Version on Ubuntu 20.04 x64 for Wordpress App

I currently get a warning on Wordpress saying I am on an insecure version of PHP (7.3.3).
I've been trying to follow the instructions on the following page to update the version to PHP 8.1.
https://www.cloudbooklet.com/how-to-install-or-upgrade-php-8-1-on-ubuntu-20-04/
I was able to install and enable php8.1 but stuck with the remaining steps. The article tells me to update a few lines in the location block of a conf file but I can't find it.
I looked at files like wordpress_https conf but could only find lines like this:
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
Any pointers on where I need to update the reference to php8.1? It's nginx server on Obuntu 20.04. It's for a Wordpress application installed on Vultr. Thanks.
Try this in your site' nginx config file. Comment out everything in your file or just backup the file and try this.
upstream php {
server unix:/tmp/php-cgi.socket;
server php:9000;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl;
server_name example.test www.example.test;
ssl_certificate /etc/nginx/ssl/example.test.pem;
ssl_certificate_key /etc/nginx/ssl/example.test-key.pem;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
#The only job of this block is to redirect http to https
server {
listen 80;
listen [::]:80;
server_name example.test www.example.test;
return 301 https://$server_name$request_uri;
}
Depending on your OS, the nginx config file for your website will either be in /etc/nginx/conf.d or in /etc/nginx/sites-available/ .
The above configuration was taken from this WordPress docker dev env.
After making the edits in the correct conf file, test nginx:
sudo nginx -t
If all is well in the conf file restart nginx based on your system i.e :
sudo service nginx restart
If this works for you, ensure you search for more security details you can add in your configuration to improve it. If it does not work for you, you can generate WP specifc Nginx configurations using DigitalOcean.

Nginx PHP setup - No input file specified

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

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(); ?>

Run multiple nginx on one dedicated server ubuntu

Is it possible to run multiple NGINX on a single Dedicated server?
I have a dedicated server with 256gb of ram, and I am running multiple PHP scripts on it but it's getting hangs because of memory used with PHP.
when I check
free -m
it's not even using 1% of memory.
So, I am guessing its has some to do with NGINX.
Can I install multiple NGINX on this server and use them like
5.5.5.5:8080, 5.5.5.5:8081, 5.5.5.5:8082
I have already allocated 20 GB memory to PHP, but still not working Properly.
Reason :- NGINX gives 504 Gateway Time-out
Either PHP or NGINX is misconfigured
You may run multiple instances of nginx on the same server provided that some conditions are met. But this is not the solution you should look for (also this may not solve your problem at all).
I got my Ubuntu / PHP / Nginx server set this way (it actually also runs some Node.js servers in parallel). Here is a configuration example which works fine on a AWS EC2 medium instance (m3).
upstream xxx {
# server unix:/var/run/php5-fpm.sock;
server 127.0.0.1:9000 max_fails=0 fail_timeout=10s weight=1;
ip_hash;
keepalive 512;
}
server {
listen 80;
listen 8080;
listen 443 ssl;
#listen [::]:80 ipv6only=on;
server_name xxx.mydomain.io yyy.mydomain.io;
if ( $http_x_forwarded_proto = 'http' ) {
return 301 https://$server_name$request_uri;
}
root /home/ubuntu/www/xxxroot;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ ^/(status|ping)$ {
access_log off;
allow 127.0.0.1;
#allow 1.2.3.4#your-ip;
#deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass adn;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /xxxroot/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_param DOCUMENT_ROOT /home/ubuntu/www/xxxroot;
# send bad requests to 404
#fastcgi_intercept_errors on;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Hope it helps,
I think you are running into a timeout. Your PHP-Scripts seams to run to long.
Check following:
max_execution_time in your php.ini
request_terminate_timeout in www.conf of your PHP-FPM configuration
fastcgi_read_timeout in http section or location section of your nginx configuration.
Nginx is designed more to be used as a reverse proxy or load balancer than to control application logic and run php scripts. Running multiple instances of nginx that each execute php isn't really playing to the server application's strengths. As an alternative, I'd recommend using nginx to proxy between one or more apache instances, which are better suited to executing heavy php scripts. http://kbeezie.com/apache-with-nginx/ contains information on getting apache and nginx to play nicely together.

nginx - laravel - hhvm-Fastcgi get error 500

I install a LEMP server in ubuntu 12.04 LTS 64
whit HHVM Fastcgi Service
and i install laravel via laravel.phar ( and test via composer too )
when in get my site in brwoser do not display any error but in chrome developer console get error 500
i can't see any error in error.log file ( laravel - hhvm , nginx )
the storage directory Permissions is 777
and my nginx.conf and vhosts file have basic configuration
when i use PHP CLI or hhvm command it's worked good
thanks for help me :)
my location block
location ~ \.(hh|php)$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
The problem with HHVM is it doesn't show much error, You have to keep watching the HHVM or Laravel error logs.
You'll want to pay close attention to your error logs. HHVM doesn't
report errors to the browser by default.
Check the HHVM logs!
$ tail -n 50 -f /var/log/hhvm/error.log
Check your Laravel logs!
$ tail -n 50 -f /path/to/laravel/app/storage/logs/laravel.log
config reference
Create a file /etc/nginx/hhvm.conf if it doesn't exist yet. Insert the ff:
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Then include it on your nginx virtual host config.
eg. /etc/nginx/sites-available/laravel
Now add this for Laravel, edit as needed:
server {
listen 80 default_server;
root /vagrant/laravel/public;
index index.html index.htm index.php;
server_name localhost;
access_log /var/log/nginx/localhost.laravel-access.log;
error_log /var/log/nginx/locahost.laravel-error.log error;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
error_page 404 /index.php;
include hhvm.conf; # INCLUDE HHVM HERE
# Deny .htaccess file access
location ~ /\.ht {
deny all;
}
}
Then reload Nginx:
$ sudo service nginx reload
Since the X-Powered-By header is set by HHVM I assume your NGINX is configured correct. A 500 error mostly comes from a syntax error or an exception thrown in your application. Maybe your fastcgi settings in NGINX are still wrong. What's inside the location *\.php block?
Try for a less error-prone setup and run php artisan serve to locally host your project.
You can modify Laravel's handle exception class to display the errors while HHVM is being used.
Full details here: https://github.com/laravel/framework/issues/8744#issue-76454458
I have tested this and It works well on Laravel 5.2/5.3 on Homestead with HHVM.

Categories