aws + centos 7 + built nginx ngx_pagespeed + php-fpm = file not found - php

I have tried everything and have verified everything is running on the same group.
Also chown'd files to nginx:nginx and chmod'd them to 755
Here is server block
server {
listen 80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# Pagespeed main settings
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}

Followed this tutorial about SELinux. I am new to CentOS so I was unfamiliar with all of the security parameters.
https://www.cloudinsidr.com/content/troubleshooting-php-7-tcp-sockets-with-selinux-on-centos-7-rhelfedora/

Related

NGINX config for php backend and JS frontend

I'm trying to serve my frontend app under /, but have requests for /oauth2 pass off to a php backend. Here is my latest nginx config attempt:
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location /oauth2 {
root /application/public;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
#try_files /index.php$is_args$args =404;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
location / {
try_files $uri $uri/ /index.html;
}
}
I've tried just about every combination of config I can think of and just can't get it to work. Most of the time I end up with 404s.
Both my nginx and php docker containers have the same /application directory mounted.
With the above config, any requests to /oauth2/blah are being picked up by the location block at the bottom and therefore back to my frontend. This is probably my biggest problem - the /oauth2 location block to my mind is more "specific" so why isn't it "winning"?
I tried the commented out try_files line instead (to see whether index.php being the "fallback" value had an effect on specificity), and nginx just started downloading the index.php file rather than passing on the request. Help?
This is the approach that I use:
attempt to serve js / static pages first
if 1.) fails, pass to PHP backend
define a location for handling .php
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ #php;
}
location #php {
root /application/public;
index index.php;
try_files $uri $document_root/index.php?$query_string;
# $document_root/index.php is the important part due to how root and alias directives work
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
The location /oauth2 only wins when the URL you try is exactly website.com/oauth2. Add ^~ and the route will win all of the URLs starting with /oauth2, like this:
location ^~ /oauth2 {
For reference I eventually found a simple working solution (below).
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ /index.html;
}
location /oauth2 {
try_files $uri $uri/ #php;
}
location #php {
include /etc/nginx/fastcgi_params;
fastcgi_pass dockerphp;
fastcgi_param SCRIPT_FILENAME /application/public/index.php;
}
}

Nginx + PHP-FPM redirect to static PHP file

Some details about my setup first:
I am serving a static webapp (HTML + JS) from default Nginx webroot
I have a PHP-FPM server running on localhost:9000
The destination file should be /api/webroot/index.php for FPM (always, no need to try_files etc.)
I need to forward all /api and /api-debug calls to arrive at localhost:9000, and the /app/webroot/index.php should handle all these requests.
I have the following working Nginx configuration:
upstream fastcgi_backend {
server localhost:9000;
keepalive 30;
}
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
location ~ ^/(api|api-debug)/ {
root /app/webroot;
index index.php;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /app/webroot/$fastcgi_script_name;
include fastcgi_params;
}
}
}
}
I just want to make it more simple and efficient, because as I see it now it's a mess.
I tried to adjust for example
try_files $uri /api/index.php$is_args$args;
to
try_files $uri /api/webroot/index.php$is_args$args;
and it failed... The only reason that it works is that /api/index.php includes /api/webroot/index.php, but I see it's inefficient.
I found debugging nginx config hard, because it's not easy to test.
Thank you very much for your help in advance!
The simplest solution would be to hardwire SCRIPT_FILENAME with a value of /app/webroot/index.php and remove one of your location blocks altogether.
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ ^/(api|api-debug)/ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot/index.php;
fastcgi_pass fastcgi_backend;
}
Alternatively, to keep the flexibility of specifying a URI with a .php extension, you could simplify the configuration with:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
rewrite ^/(api|api-debug)/ /index.php last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot$uri;
fastcgi_pass fastcgi_backend;
}

When should I use php-fpm, php-cli, php-mbstring with phpmyadmin/nginx config

After following many tutorials on nginx for a basic phpymadmin setup, I have concluded that I am confused as to why certain tutorials recommend certain php modules while others do not.
My working configuration is with php7-fpm, php7-mbstring.
server {
listen 80;
listen [::]:80;
server_name name ip;
root /var/www/html/phpmyadmin;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ ^/(.*\.php)$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Most tutorials suggest php-fpm, php-cli, and another module.
Do I need these, and why would I need these modules for phpmyadmin?
Is my nginx configuration the correct way to have it function properly (not the most secure), or should I add any other locations/modules to my nginx configuration such as,
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /path/;

Using Django and PHP in Nginx

I'm using Django in a Vhost and PHP in another, now I want to make one file with the two things, a part for Django and another for PHP.
I've read this question but it's not helping me.
How to run django and wordpress on NGINX server using same domain?
this one didn't help either
How can I configure nginx to serve a Django app and a Wordpress site?
This is part of what I have in the PHP config:
root /var/www/agendav/web/public;
# Add index.php to the list if you are using PHP
index index.php;
location ~ ^(.+\.php)(.*)$ {
try_files $fastcgi_script_name =404;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
This is the full Django part:
upstream django {
server unix:///tmp/mysite.sock;
}
root /path/to/project/folder;
charset utf-8;
index index.html index.htm index.php;
if ($allowed_country = no) {
return 444;
}
# max upload size
client_max_body_size 75M;
# Django media
location /media {
alias /path/to/mysite/media/;
}
location /static {
alias /path/to/mysite/static/;
}
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass pass;
include /path/to/uwsgi_params;
}
location /.well-known/acme-challenge/ {
allow all;
}
And I'm trying to add this to the other conf file (that's already working)
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
for what I've read, that should do, but all I've getting is a 404 error, or a blank page and no readings in the nginx log or even in the agendav(the app in PHP) ones.
What am I doing wrong?

NginX with FastCGI and WordPress in a subdirectory - "No input file specified"

I'm trying to migrate a WordPress blog to a subdirectory of my website (i.e. example.com/blog/). Using NginX and FastCGI, I've managed to get the entire WordPress site working on port 8080, but as soon as I attempt to apply rules to place it in /blog, I get "No input file specified"
I think because I can get PHP and WordPress working, I can assume that there are no issues with my installation or the permissions of the relevant folders.
This is what my virtual host looks like (I am running a rails site at "/"):
server {
listen 80;
server_name example.com localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /blog/ {
root /home/deploy/www/blog.example.com/html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
location ~ \.php$ {
root /home/deploy/www/blog.example.com/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
root /home/deploy/www/example.com/public;
passenger_enabled on;
index index.html index.htm;
}
I have also tried this configuration for the same result:
location ~ ^/blog/.*(\.php)?$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
root /home/deploy/www/blog.example.com/html;
try_files $uri =404;
# fastcgi_split_path_info ^(/blog/.+\.php)(.*)$;
#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 127.0.0.1:9000;
}
What am I doing wrong?
Are you using WordPress with Multisite?
I am not clear about your setup, but a tutorial from this list will surely help you: http://rtcamp.com/wordpress-nginx/tutorial
If you can share more details, I can guide you better.
From where does 8080 coming into picture? R u using Nginx with Apache??
"No input file specified" looks like an error related to incorrect location of PHP file...
Try changing
enter code herefastcgi_index index.php;
to
try_files index.php blog/index.php
Assuming 'blog' is a folder where you moved your WordPress.

Categories