nginx version: nginx/1.18.0 (Ubuntu 20)
PHP 7.4.3 (cli) (built: Feb 24 2022 14:55:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
I've created a simple page in html, for execute a script in php with a button.
Here my html:
<html>
<head>
</head>
<body>
hello
<form action="path-of-myphp.php" method="post">
<input type="submit" name="backupMySql" value="Backup"/>
</form>
</body>
</html>
and here my php
<?php
echo "Hello";
?>
Here my nginx config
server {
server_name mysite.com;
location / {
proxy_pass https://localhost:5003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ \.(php)$ {
include /etc/nginx/fastcgi.conf;
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = mysite.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name mysite.com;
listen 80;
return 404; # managed by Certbot
}
I see correctly the button, but when i click on it, i am redirected to another url, and this url is www.mysite.com/path-of-myphp.php
error nginx
I don't undestand why my nginx can't execute this simple php, meanwile by shell i can execute all..
Anyone have tips?
thank very much
The nginx error is a 404 not found error and it means that nginx can't find your file.
Due to your nginx configuration your website root is in /var/www/html, so make sure that your php file is inside this directory:
/var/www/html/path-of-myphp.php
Please take a look also to the permissions of your php file. It has to be readable by www-data user
chown (your user or www-data):www-data /var/www/html/path-of-myphp.php
chmod 755 /var/www/html/path-of-myphp.php
In this way the www-data user has at least read and execution permissions by the group.
Related
I am setting up an nginx web server that can run PHP files, particularly for the selfoss rss viewer. For some reason, when I visit my website, it downloads the index.php file instead of executing it on the server. I am running this on a raspberry pi 1B.
Yes, I did restart nginx and clear my web browser cache. It didn't fix anything.
Here is my PHP version:
$ php -v
PHP 7.4.30 (cli) (built: Jul 7 2022 15:51:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
Here is my nginx configuration:
server {
server_name rss.getty.nz;
root /var/www/rss.getty;
access_log /var/www/rss.getty/rss.accesss.log;
error_log /var/www/rss.getty/rss.error.log;
index index.php;
location / {
try_files $uri /public/$uri /index.php$is_args$args =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf ;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock ;
}
location ~* \ (gif|jpg|png) {
expires 30d;
}
location ~ ^/(favicons|thumbnails)/.*$ {
try_files $uri /data/$uri;
}
location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
deny all;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/rss.getty.nz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/rss.getty.nz/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
access_log /var/www/rss.getty/rss.accesss.log;
error_log /var/www/rss.getty/rss.error.log;
}
server {
if ($host = rss.getty.nz) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name rss.getty.nz;
return 404; # managed by Certbot
}
Here is the contents of /etc/nginx/snippets/fastcgi-php.conf:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
Thanks to Richard Smith in the comments, I have the answer. It was the =404 in this section:
location / {
try_files $uri /public/$uri /index.php$is_args$args =404;
}
Removing that stops the web browser from downloading.
After much wailing and gnashing of teeth, I've got adminer up and running on my Ubuntu 20.04 server running a LEMP stack. The database management seems to work fine, but it looks ugly as sin, without even the default skin on (see screenshot). That would be fine except that it makes it hard to use the program effectively.
I tried putting a new adminer.css file into the directory with adminer.php (see second screenshot), but it didn't work. I've also gone down a lot of rabbit holes here and on sourceforge with no luck. Most of them point to some issue with .css serving through https:// but I don't know enough to make sense of what's happening or not happening.
So I turn to you, great hive mind. What else can I try? Below is my nginx server block for reference:
server {
server_name adminer.WEBSITE.COM;
root /var/www/html/adminer;
index index.php index.html index.htm index.nginx-debian.html
access_log /var/log/nginx/adminer.access.log;
error_log /var/log/nginx/adminer.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
include /etc/nginx/mime.types;
}
location ~\.php$ {
auth_basic "Please enter username and password";
auth_basic_user_file /etc/nginx/PASSWORDFILE;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/adminer.WEBSITE.COM/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/adminer.WEBSITE.COM/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = adminer.WEBSITE.COM) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name adminer.WEBSITE.COM;
listen 80;
return 404; # managed by Certbot
}
So I am running an Express.js/Passport.js web server with Nginx on CentOS 7.
Here's the problem: I cannot display any endpoint that starts with w. Trying to do so results in a Cannot GET /wiki/Main_Page message. However, changing that same route from e.g. /welcome to /selcome works just fine.
Most likely culprit:
I installed Mediawiki but removed it not long after. IIRC there was some kind of setting that prettied up URLs that starting with /w after the domain. So I'm guessing a rewrite rule is persisting... just have no idea where.
Here are my config files:
/srv/node/example.com/app/routes/auth.js
module.exports = function(app, passport) {
//Changing the URLs below from welcome to selcome works fine
app.get('/activate', passport.authenticate('user-activate-account', {
successRedirect: '/welcome',
failureRedirect: '/404',
failureFlash: true
}));
...
app.get('/welcome', isLoggedIn, function(req, res) {
res.render('inside', {
page_title: 'Welcome!',
inc_style: true,
style_sheet: 'style/dashboard.css',
portal: function() {
return 'welcome';
}
});
});
};
/etc/nginx.conf
user nginx nginx;
error_log /var/log/nginx/error.log info; # [ debug | info | notice | warn | error | crit ]
events {
worker_connections 1024;
}
http {
include mime.types;
include /etc/nginx/sites_enabled/.conf; //Really *.conf but more readable this way
include /etc/letsencrypt/options-ssl-nginx.conf;
server_names_hash_bucket_size 64;
# Compression - requires gzip and gzip static modules.
gzip on;
gzip_static on;
gzip_vary on;
gzip_http_version 1.1;
gzip_min_length 700;
# Compression levels over 6 do not give an appreciable improvement
# in compression ratio, but take more resources.
gzip_comp_level 6;
# IE 6 and lower do not support gzip with Vary correctly.
gzip_disable "msie6";
# Before nginx 0.7.63:
#gzip_disable "MSIE [1-6]\.";
# Redirect http traffic to https
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
# Catch-all server for requests to invalid hosts.
# Also catches vulnerability scanners probing IP addresses.
server {
listen 443 ssl;
server_name bogus;
root /var/empty;
return 444;
location / {
try_files $uri $uri/ =404;
}
}
# If running php as fastcgi, specify php upstream.
upstream php {
server unix:/var/run/php7.2-fpm.socket;
}
}
/etc/nginx/sites_available/example.com.conf
#sub.example.com
server {
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
listen 443 ssl;
server_name sub.example.com;
access_log /var/log/nginx/sub.example.com.access.log;
error_log /var/log/nginx/sub.example.com.error.log;
location /something\.js {
alias /var/www/html/example.com/sub.example.com/design/;
location ~* \.(gif|jpe?g|png|svg|webm)$ {
try_files $uri =404;
expires 30d;
}
}
location / {
proxy_pass http://localhost:3847;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
So far I have
Purged Nginx config files, and every config file in the include tree, of any legacy rules.
Cleared Nginx cache
My node.js project folder does not even contain the string wiki anywhere except for NPM links in the node_modules folder.
All the PHP files in /etc, /etc/php.d, /etc/php-fpm.d do not contain the string wiki
Deleted mediawiki folder
Restarted nginx
Restarted php-fpm
Restarted entire machine
I'm genuinely baffled at where this problem could be. Any ideas?
Turns out it was the browser. Despite clearing history multiple times and using incognito mode, nothing changed. But using another browser worked flawlessly. I feel like an idiot, but at least I can finally put this rage-inducing mystery behind me.
Was using: Firefox (I guess private mode isn't enough)
Chrome showed it was a browser error.
I'm trying to setup Nginx as a reverse proxy for Apache, from what I've read, it allows nginx to serve static content and Apache handles the backend PHP stuff, but i cant seem to get Apache to render.
I'm on CentOS7, i installed nginx just using yum install nginx, then i installed PHP7.2 by doing the following;
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php72
yum install php72 php72-php-fpm php72-php-mysqlnd php72-php-opcache php72-php-xml php72-php-xmlrpc php72-php-gd php72-php-mbstring php72-php-json
running php72 -v gives me
PHP 7.2.13 (cli) (built: Dec 8 2018 10:59:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend
Technologies
I then ran
ln -s /usr/bin/php72 /usr/bin/php
As yum installs the command as php72
I edited nginx.conf and changed the user from nginx to apache and changed the server block to;
server {
listen 80 default;
server_name 108.xxx.xxx.xxx;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /var/www/html;
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
I also added /etc/nginx/conf.d/proxy.conf with the following;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
I then installed Apache2 via yum install httpd.
I have then edited the Apache2 httpd.conf file;
- Listen 80
+ Listen 127.0.0.1:8080
I also edited /etc/opt/remi/php72/php-fpm.d/www.conf and changed user and group to apache and also
listen = /var/run/php-fpm.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660
These are the only changes I have made.
I added 2 files to /var/www/html, index.html and index.php ... The index.html works perfect, and when i check with browserspy, it says that it is being served by Nginx, excellent. But when I run the index.php file it displays the actual php code and doesn't render it.
I have never really worked with Apache2 before, so i am unsure of how to look for the error. When i loook in Apache2 modules directory i can't find any PHP modules
ls -lah /etc/httpd/modules/ | grep php
returns nothing at all.
Any help would be really greatful, I have been looking for a solution for days.
Thanks
Nginx can definitely execute PHP scripts without the need of proxying back to Apache.
The reason why you're seeing just the PHP code rather than the website, is because your Apache configuration likely does not have the PHP module enabled.
You can do this by running yum --enablerepo=remi install php and running service apache2 restart to restart the server with the new configuration.
Installing the base PHP packages also adds the required modules for PHP files to be executed by Apache.
This should allow your server to start executing the PHP scripts as you expect.
If you'd like to instead run your PHP website via Nginx instead, you'll need to make some slight modifications to your Nginx configuration.
Firstly, you'll need to replace your location block to use the files on your local filesystem and then point any .php file to run using PHP-FPM.
location / {
root /var/www/html;
try_files $uri $uri/ =404;
}
location ~ \.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;
}
On Ubuntu 16.04 with Bash I've established LEMP and I can't access a WordPress app from browser although DB data is according (as I recall), and document root app dir has the right permissions (see below).
Nginx conf
nginx.conf (all default).
nginx default (all default).
Nginx app conf (seven first lines are mine - the rest is Certbot).
WordPress app wp-config.php
This is the only part I changed in wp-config.php:
define('DB_NAME', 'example.com');
define('DB_USER', 'example.com');
define('DB_PASSWORD', 'example.password');
My resets before testing
chown -R www-data:www-data /var/www/html/
chmod -R a-x,a=rX,u+w /var/www/html/
systemctl restart nginx.service
/etc/init.d/php*-fpm restart
Nginx logs
I checked all three Nginx logs (-t,access and error). The first two logs output no error, but error outputs this:
directory index of "/var/www/html/example.com/" is forbidden
My question
Why is the entrance forbidden, given the above data? What I miss?
Notes:
This problem is unique to the WordPress app. It doesn't happen with the non-HTTPS, PHPmyadmin app.
The DB user and the DB name of the WordPress app are identical to one another and also identical to the app's dir name.
The server root at your Nginxapp.conf should be:
root /var/www/html;
Not:
root /var/www/html/example.com/;
BTW, this setting has been defined in the Nginx virtual machine default.conf, it is kind of redundant to redefine it again at the app conf file.
Update
With your comment of not working after changing the root to root /var/www/html;, I further check your setting, and notice that you do not have a location directive for handling the root directory, try to change this line in your virtual host setting from:
try_files $uri $uri/ =404;
to:
location / {
try_files $uri $uri/ /index.php?$args;
}
I was able to solve it with this conf:
server {
root /var/www/html/judith-law.co.il;
server_name judith-law.co.il www.judith-law.co.il;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
expires 365d;
}
# managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
return 404; # managed by Certbot
}