Sample PHP page on localhost shows a blank page - php

I follow this guide to setup a sample WordPress site on my Linux machine. The packages are changed to the newer php7.2-gd, php7.2-curl, and libssh2-php. My /etc/nginx/sites-available/davewordpress file is as follow:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name localhost;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
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_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
After creating the davewordpress link in /etc/nginx/sites-enabled and restarting nginx and php7.2-fpm, my localhost gives a blank page. What have I done wrong?

OK, I run into this link while searching for the solution, and it really helps by pointing to the location ~ \.php$ block. I comment out two lines and have it working. That blocks now looks like this:
location ~ \.php$ {
#try_files $uri =404;
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
#fastcgi_index index.php;
include fastcgi_params;
}
Using sudo nginx -t, the first line reports this problem:
"try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5
The second line reports this problem:
"fastcgi_index" directive is duplicate in /etc/nginx/sites-enabled/u1804wordpress:27
Glad to finally figure it out. I understand the first error. However, can anybody explain to me the second one?

Related

how to setup nginx having react pwa in one folder and php apis in other?

Disclaimer: I'm not a sys admin :)
I have the following setup:
- react pwa in /var/www/react/build
- php application under /var/www/html
- site url mysite.loc
I want all requests except mysite.loc/fapi/* and mysite.loc/api/* to be handled by react app and (fapi|api)/* by php app.
I am trying with the following nginx config:
server {
listen 80;
listen [::]:80;
server_name mysite.loc;
root /var/www/react/build;
index index.html index.php;
location ^~ /fapi {
rewrite ^/fapi/?(.*)$ /html/fapi/$1 last;
}
location ^~ /api {
rewrite ^/api/?(.*)$ /html/api/$1 last;
}
location ^~ /html {
index index.php;
root /var/www;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
}
}
when accessing mysite.loc/fapi/user/get this config returns the following error:
"/var/www/react/build/index.php" failed (2: No such file or directory)
what I was expecting was that the nginx would send request to /var/www/html/fapi/index.php. Not ideal but something I can work with.. Ideally I would like the request to go to /var/www/html/index.php, request being /fapi/user/get.
Can you point me into the right direction?
Thanks.

NGINX Location Blocks for PHP subpaths

I am trying to filter a path to specifically do the following (in this order I think):
Allow all traffic from everywhere in the world to the path /PROD/index.php?/report (and anything after /report like the examples below) and block traffic to anywhere else
ALLOW: /PROD/index.php?/report
ALLOW: /PROD/index.php?/report/
ALLOW: /PROD/index.php?/report/werwf
DENY: /PROD/index.php?/{anything else}
Allow all traffic from any IP address in the range we own (denoted by x.x.x.x/16)
The current NGINX config I have is:
server {
listen 443 http2 ssl;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
ssl_certificate /etc/nginx/ssl/fsmunkireport.stockton.edu.cer;
ssl_certificate_key /etc/nginx/ssl/fsmunkireport.stockton.edu.key;
#ssl_dhparam /etc/nginx/ssl/dhparam.pem;
location = / {
return 301 https://fqdn/PROD/index.php?;
}
location ~ ^\/PROD\/index.php\?\/report(.*) {
allow all;
index index.php;
autoindex on;
location ~* \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /secure/PPRD/$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}
}
location /PROD {
allow x.x.x.x/16;
deny all;
index index.php;
autoindex on;
location ~* \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /secure/PPRD/$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}
}
}
The issue I am running into is filtering through that php subpath. Since it isn't a literal directory I think I am just confusing myself. Basically at this point all traffic is allowed via NGINX which isn't the desired behavior.
Any assistance would be greatly appreciated.

Nginx with angular and codeigniter applications in subfolders

I'm trying to make a website with codeigniter in the backend and angularjs in the frontend.
For the backend I'm using nginx as a server.
Currently I have problems with proper configuration of the nginx. What I want to achieve is to have codeigniter and angularjs applications in separate folders. And What I want is to access as follows
codeigniter from my_website.com/api
angular from my_website.com
And in terms of folder I want to keep them in:
codeigniter: /var/www/html/api
angular: /var/www/html/angular
So far I've manage to make it work from the same folder. So now I have codeigniter in /var/www/html and angular folder is in the same directory. And that way I can access codeigniter from my_website.com/ and angular from my_website.com/app which is not what I want.
I was trying multiple different setups for nginx /etc/nginx/sites-available/default file, but every single time with some problems. Farthest I've got was to have those in separate folders but codeigniter was working only for default_controller and I couldn't access any other files.
That's the current working config.
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
server_name localhost;
index index.php index.htm index.html;
location /app/ {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location / {
try_files $uri $uri/ /index.php;
#index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
}
And here is simple config where I just try to run codeigniter from demo sub-folder,
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.php;
location / {
#try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$request_uri;
location = /demo/index.php {
try_files $uri $uri/ /index.php$request_uri;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/demo$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
And here my_website.com/demo/index.php works fine but my_website.com/demo/index.php/welcome shows 500 Internal Server Error
I hope I make myself clear.
I tried so many different configs found online, but I always had some troubles with that. Can any of you propose some simple solution for that problem?
Thanks,
Mateusz
I've finally managed to make it work the way I want. I've used this solution link to make it work with codeigniter in sub-directory which was the main problem. Here is my config:
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.html;
location / {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location /api/ {
alias /var/www/html/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
return 444;
}
}
Thanks to #FrédéricHenri for suggesting using the log file under /var/log/nginx/*.log, that helped with tracking the way nginx is redirecting requests. Also what I've encounter was that web-browser was caching the website(I think that is what was happening) so that was giving me false results while I was making some changes to the config file.
If you have already set up ~ .php and other stuff. then Just need to add
location /ci-project {
try_files $uri $uri/ /ci-project/index.php;
}
In your server {} object of Nginx configuration

Creating new Lumen app on nginx throwing 404 when hiting IP Adress through browser

I am trying to create a plain Lumen application on Nginx server on Digital Ocean. Following the official documentation for Laravel, I followed everything as it is said until the Laravel Installation bit.
Just instead of this
composer create-project laravel/laravel /var/www/lumen/ 4.1
I used
composer create-project --prefer-dist laravel/lumen blog` instead.
The Lumen document seems on the directory now when I use ls.
However when I hit the IP address, or IPAdress/index.php, it shows
404 Not Found - nginx/1.4.6 (Ubuntu)
Also, this is my configuration for Virtual Host file (nano /etc/nginx/sites-available/default)`:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =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;
}
}
What am I doing wrong or missing?
Also, if I try connect to the SSH directory with PhpStorm, I received an error:
ssh://root#46.101.172.134:22null /usr/bin/php
env: /var/www/Lumen: Permission denied
Process finished with exit code 126
Please also check this question for further info on this error..
Edit 2:
I tried changing my server to..
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /var/www/lumen/public;
index index.php index.html index.htm;
server_name IPAddress;
ssl_certificate /etc/nginx/ssl/nginx.crt; // I didn't set this
ssl_certificate_key /etc/nginx/ssl/nginx.key; // and this
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I used this guide to create an SSL Certificate on nginx. Then, hitting http://ipaddress gives me the Lumen/Laravel stuff.

Nginx Wordpress Configuration, PHP file in theme directory is not passed to FastCGI

I am new to NGINX configs so bear with me. Below is my configuration which works fine for the whole site:
server {
listen ...;
server_name funkyoslo.no;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/funkyoslo.webbr.org/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/funkyoslo.webbr.org/html/;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/funkyoslo.webbr.org/html/$fastcgi_script_name;
include fastcgi_params;
}
}
However, I am trying to load the file /wp-content/themes/funkyoslo/load-songs.php and it's giving me a 500 internal server error. I've checked the error logs and evidently the file isn't passed to FastCGI at all.
I tried adding the following block to no avail:
location ~ .*/wp-content/themes/funkyoslo/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/funkyoslo.webbr.org/html/wp-content/themes/funkyoslo/$fastcgi_script_name;
include fastcgi_params;
}
Any help is greatly appreciated!
Look I don't know what's wrong with your config but try this and hopefully it should work, this is the minimal config.
Notes:
Removed the index to the server block level, check link to know why
Removed root to server block level, check link to know why
Removed all extra config from the php block level, by trials I realised that I only need those two I've written.
Added http:// to fastcgi_pass, I don't know if it's really required but I got used to writting it this way.
Edit: Ok apparently I don't need the http://, I just checked.
-
server {
listen 80;
server_name funkyoslo.no;
root /usr/share/nginx/funkyoslo.webbr.org/html;
error_page 500 502 503 504 /50x.html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$request_uri;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass http://127.0.0.1:9000;
}
}

Categories