nginx isn't parsing php in nextcloud install - php

I'm trying to set up a nextcloud server on a raspberry pi 3 (raspian stretch).
I've been generally successful in getting the stack built using nginx and mysql. However, I've encountered a problem with nginx not parsing php.
That is, when I navigate to the pi's nextcloud install, the browser prompts me to open / save index.php (the next cloud login screen), which tells me the server isn't parsing it at all. I found one link to the nextcloud documentation which suggested adding:
index index.html index.htm index.php
to the nginx.conf file. Unfortunately, it appears to have no effect.
Any tips on how I can go about troubleshooting this or other ideas to try?
EDIT: based on comments and first answer, I have updated my /etc/nginx
EDIT #2: Reverted to the default nginx.conf file and added #Mason Stedman's code in his answer below to /etc/nginx/sites-enabled/default. Also, used the following tutorial (through step 7) to set up the stack after re-flashing raspian stretch:
https://linoxide.com/debian/install-nextcloud-10-nginx-debian-8/
For completeness, my /etc/nginx/sites-enabled/default file is below. For the server name, I had no idea what to put in, so I used the static IP address I assigned to it on my network (I'm not exposing this to the internet, BTW):
server {
listen 80;
listen [::]:80;
server_name 192.168.xx.xx;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/;
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}

Try adding this to your nginx config:
Edit: Here is an example of the full thing you will need to put in there to work. I can edit again if you need SSH support, or a more comprehensive example, but this example will work on just about any configuration.
server {
listen 80;
listen [::]:80;
server_name site.com www.site.com;
access_log /var/log/nginx/site.com.access.log;
error_log /var/log/nginx/site.com.error.log;
root /var/www/site.com/htdocs/;
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
You need to send php files to a parser. You can change the version in that to the version of php you wanna use.

Related

Nginx - PHP file is downloaded instead of 404 error (Piwigo Docker)

I will preface this by saying that I almost always use Apache, but am trying Nginx while moving some stuff to a docker container. I am using the Piwigo Docker image from LinuxServer.io located here, and there is an open issue for this pro, but it has yet to be resolved. So, I am hoping that someone here could possibly point me in the right direction. For testing, if anyone is interested, the issue can be seen on a completely clean pull after setup.
Issue: Accessing any page or folder that doesn't exist gives one of two outcomes.
Outcome 1: attempt to access /fakepage.html or /fakepage = PHP file is downloaded instead of served.
Outcome 2: attempt to access /fakepage.php = empty page with "File not found" in the top left.
All configs are shown in the Github link above, but shown here for context.
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
set $root /app/www/public;
if (!-d /app/www/public) {
set $root /config/www;
}
root $root;
index index.html index.htm index.php;
location / {
# enable for basic auth
#auth_basic "Restricted";
#auth_basic_user_file /config/nginx/.htpasswd;
try_files $uri $uri/ /index.html /index.php$is_args$args =404;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess/.htpasswd files
location ~ /\.ht {
deny all;
}
}
When 404 pages should be shown/logged, instead everything is logged as a 200 regardless of file downloaded or "file not found" error shown.
This issue exists on HTTP or HTTPS attempts.
I have tried this in the following Docker Hosts; Rocky 8, Rocky 9, Windows 10, and Ubuntu 22.04
I have changed themes in Piwigo and checked permissions and paths as suggested in other posts.
Here is a sample of what the downloaded php file looks like.
<?php
// +-----------------------------------------------------------------------+
// | This file is part of Piwigo. |
// | |
// | For copyright and license information, please view the COPYING.txt |
// | file that was distributed with this source code. |
// +-----------------------------------------------------------------------+
//--------------------------------------------------------------------- include
define('PHPWG_ROOT_PATH','./');
include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
// Check Access and exit when user status is not ok
check_status(ACCESS_GUEST);
// access authorization check
if (isset($page['category']))
{
check_restrictions($page['category']['id']);
}
if ($page['start']>0 && $page['start']>=count($page['items']))

Second Wordpress site in subdirectory of first Wordpress site returns 404 using NGINX server block

I have a Wordpress site on root, and a second Wordpress site in subdirectory /blog. I have been tasked with migrating these two Wordpress installs from one server to another. Here is the folder structure on the Ubuntu server:
/var/www/html/example.com/html # WP install 1
/var/www/html/example.com/html/blog # WP install 2
The old server works fine, however visiting https://example.com/blog (or any folder within) on the new server returns a 404 error.
Below is the configuration file, /etc/nginx/sites-available/example.com
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/example.com/html;
index index.html index.htm index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
While I have been very careful to ensure the databases and directory structure match across the servers, one difference is the php has been updated on the new server from 7.0 to 7.4.
I have tried a variety of different configurations to get this to work, but so far I have not been successful. Any assistance would be greatly appreciated.
Okay, SOLVED. I made a mistake and imported the same database for both WP installs, and went down this rabbit hole instead of checking the databases. The configuration file is correct, just as it always was. Whew!

Configure multi site on nginx server through IP using Digital Ocean

Problem: Unable to hit two websites handled under one nginx server i.e. <<ip-address>> & <<ip-address>>/web2
Configuration on Digital Ocean:
1 Droplet / Ubuntu 18 / LEMP
I have two test PHP website in the CodeIgniter framework
Folder config for 1st Website: /var/www/html/web1/
Folder config for 2nd Website: /var/www/html/web2/
Nginx Server Block configuration for two sites
web1.com
server {
listen 80;
root /var/www/html/web1;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
web2.com
server {
listen 80;
root /var/www/html/web2;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>/web2;
location /web2/ {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I am totally new to nginx server, I do according to the documentation provided by a community under a digital ocean.
Please help!
Thanks.
What you're trying to do is not how nginx works out of the box. It could, with a lot of fiddling, end up working that way, but I don't think it's worth the effort.
See, nginx configuration expects server_name to be either a FQDN (fully qualified domain name) or an IP address, but not a full URL with path.
In your case, the request for ip-address/web2 is probably actually matching web1's config (so pointing you to /var/www/html/web1/web2/ which doesn't exist)
Best way to work this out (assuming you want to keep both sites on the same droplet): get a FQDN for each site. It could be a subdomain for a domain you already have (i.e. web1.sharad.com and web2.sharad.com)... Then on each of nginx's config files use the appropriate server name (web1.sharad.com and web2.sharad.com), check for typos and errors with sudo nginx -t and if all is OK restart nginx with sudo systemctl restart nginx

Why nginx always redirects to one of my virtual hosts?

I have several virtual hosts configured with nginx. They are very simiral to each other and for most of them, everything seems to be working fine.
However,
If I hit the server through a url that does not have any virtual host configured, nginx always redirects to one of the existing virtual hosts (seems to be the first one in the list.
Same happens if I simply go there using IP address.
Most of the virtual hosts look like this:
server {
server_name iibs.co;
return 302 $scheme://www.iibs.co$request_uri;
}
server {
server_name www.iibs.co;
root /var/www/iibs.co;
index index.php;
include global/restrictions.conf;
client_max_body_size 64M;
# Additional rules go here.
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
# Only include one of the files below.
# include global/wordpress.conf;
# include global/wordpress-ms-subdir.conf;
# include global/wordpress-ms-subdomain.conf;
}
I just tried adding another one, very similar just with different domain and root. And that particular one, also getting redirected to the same first vhost on the list.
At this point, I have no idea where and what should I looks for.
This is an example of default NGINX config for prevent redirects to the first of the existing virtual hosts .
### Block all illegal host headers. Taken from a discussion on nginx
### forums. Cf. http://forum.nginx.org/read.php?2,3482,3518 following
### a suggestion by Maxim Dounin. Also suggested in
### http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names.
server {
listen 80 default_server; # IPv4
#listen [::]:80 default_server ipv6only=on; # IPv6
server_name _;
server_name "";
return 444;
}

NGINX server is downloading php files

i'm trying to setup nginx on my vps and i made it however when i'm try to use .php files it download then instead of runing them. This is my nginx.conf
server {
listen 80;
server_name site;
root /var/www/;
index index.php index.html;
}
Any ideas how to fix it?
(i have php5-fpm installed)
For pass the PHP scripts to FastCGI you must add this into server config:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
You really need to enable php-cgi/fast-cgi or -cli depending on your server/vps configuration.
share with us more information to be able to help you (like config files etc.)

Categories