php is being downloaded instead of being loaded - php

I got a nginx docker container which actually works. Because when i do curl localhost:7070 on my host i get the content of the site as return. Then i made a config for nginx on my host, the problem is when i try to open the site in a browser it downloads the php file. I will sahre both configs of host nginx and docker nginx.
Nginx host conf:
server {
#listen 80 default_server;
#listen [::]:80 default_server;
# SSL configuration
listen 7171 ssl default_server;
listen [::]:7171 ssl default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name SERVERNAME;
location / {
proxy_pass http://127.0.0.1:7070;
#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;
#proxy_set_header X-Forwarded-Host $server_name;
}
}
nginx docker conf:
server {
listen 80;
server_name default_server;
root /usr/share/nginx/selfoss;
index index.php index.html;
location ~* \ (gif|jpg|png) {
expires 30d;
}
location ~ ^/(favicons|thumbnails)/.*$ {
try_files $uri /data/$uri;
}
location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
deny all;
}
location / {
index index.php;
try_files $uri /public/$uri /index.php$is_args$args;
}
}
I know alittle about nginx normal config but im noob at php so what is the thing that im missing here?
I have seen posts about php-fpm and fastcgi but i have no clue ho or where to do that config changes.

You are missing a configured PHP-FPM in your Nginx-Config and of course a running PHP-FPM (inside the same Docker-Container or in a separate Docker-Container).
Example Nginx-Config inside your server{} section
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
proxy_read_timeout 300;
fastcgi_read_timeout 300;
fastcgi_pass unix:/var/run/sockets/php/www.sock;
}

Related

Adding Flask app in existing nginx PHP configuration

The configuration below is the configuration for a web application in PHP, and it's working (I faked the site's name to https://sub.mysite.nl).
server {
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
## some certificate info ##
root /path/to/www;
index index.php index.htm index.html;
server_name sub.mysite.nl;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
## some logging info ##
}
server {
if ($host = sub.mysite.nl) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name sub.mysite.nl;
return 404; # managed by Certbot
}
Now I want to add a Flask app in a subfolder e.g. https://sub.mysite.nl/flaskapp.
The block below is what I got from the Flask Mega Tutorial I followed, see specifically this chapter: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux (under Setting Up Nginx). I think I need to put this under location /flaskapp/ but I'm not sure how to proceed, because when I do this and go to https://sub.mysite.com/flaskapp it gives me 404 Not Found.
location /flaskapp {
proxy_pass http://localhost:8000;
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;
}
Do I need to change the routing in my Flask app?
Ok, I've been fooling around a bit and it seems like editing the routes in my Flask app provides the easiest solution. For this, I use the Flask nginx block from my original post at the bottom.
So instead of #app.route('/'), I use #app.route('/flaskapp/').
And #app.route('/view_profile/<username>') becomes #app.route('/flaskapp/view_profile/<username>').

Cannot get index.php page to display in docker container

I have a docker container that I setup to display a php page, however it displays a 403 forbidden page. I have nginx running on my host machine and I enabled my site as well. The site is displaying html file but not php files. I also verified the following services are running.
nginx is running in the docker container
php-fpm service is running in the docker container.
the user www-data has permissions to execute files in the folder
server {
listen 443;
server_name af.oxygenweb.ca;
ssl_certificate /etc/letsencrypt/live/af.oxygenweb.ca/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/af.oxygenweb.ca/privkey.pem;
ssl_stapling on;
ssl_stapling_verify on;
location /.well-known {
alias /var/www/html/.well-known;
}
access_log /var/log/nginx/af/access.log;
error_log /var/log/nginx/af/error.log;
location / {
root /var/www/html;
index index.php;
proxy_pass http://172.17.0.2;
proxy_redirect http:// https://;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
#try_files $uri /index.html index.php;
}
# pass PHP scripts to FastCGI server
location ~* \.php$ {
# include snippets/fastcgi-php.conf;
# pass PHP scripts to FastCGI server
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

OROCRM Nginx Virtual Host Configuration Not Working

I have installed a orocrm on a AWS Lightsail VPS with Nginx & PHP7. orocrm installed without a hitch but I'm using Nginx for the first time and my virtual host doesn't seem to be working.
sites-available/default:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index app.php index.php index.html;
server_name 34.127.224.10;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I then purchased & pointed crmdomain.com to my instance and created another file.
sites-available/crm:
server {
listen 80;
server_name crmdomain.com www.crmdomain.com;
root /var/www/html/crm/web;
index app.php;
error_log /var/log/nginx/orocrm_error.log;
access_log /var/log/nginx/orocrm_access.log;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location #rewrite { rewrite ^/(.*)$ /app.php/$1; }
location / {
try_files $uri /app.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_index app.php;
fastcgi_read_timeout 10m;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I honestly don't know most of what is above, it's mostly based on tutorial and apache experience.
Errors
Domain based
- crmdomain.com -> 403 Forbidden nginx/1.10.0 (Ubuntu)
- crmdomain.com/app.php -> No input file specified.
- crmdomain.com/app_dev.php -> No input file specified.
- crmdomain.com/index.nginx-debian.html -> Welcome to nginx!
- crmdomain.com/user/login -> 404 Not Found nginx/1.10.0 (Ubuntu) **This is what should word**
Static IP based
- 34.127.224.10 -> 403 Forbidden nginx/1.10.0 (Ubuntu)
- 34.127.224.10/crm/web/ - No input file specified.
- 34.127.224.10/crm/web/app.php - No input file specified.
- 34.127.224.10/crm/web/app_dev.php - No input file specified.
- 34.127.224.10/index.nginx-debian.html -> Welcome to nginx!
- 34.127.224.10/crm/web/app.php/user/login -> 404 Not Found nginx/1.10.0 (Ubuntu) **This is what should word**
What am I doing wrong?
It's weird how often I answer my own questions:
I was able to change the default files and remove the extra virtual host. Since the server was only going to host the one app.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 34.127.224.10 crmdomain.com www.crmdomain.com;
root /var/www/html/crm/web;
index app.php app_dev.php index.php;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config|install)\.php(/|$) {
#fastcgi_pass 127.0.0.1:9000;
# or
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/orocrm_error.log;
access_log /var/log/nginx/orocrm_access.log;
}

Nginx + php5-fpm = 404 Error with alias location

I am an amateur front end web developer, and I recently bought a Ubuntu server to try to my hand at some backend development. I am trying to figure out how to serve a php file from an aliased location block using php5-fpm. I am getting a 404 - Page not found error. I have tried all of the proposed solutions I could find here with no luck. As I am still a beginner I would love a quick ELI5 as well and any pointers on the rest of my conf file, so I can learn something too. I should mention that the main root folder is running a flask app, and is the reason I am using an aliased location.
My virtual host:
Nginx conf file
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
root /var/www/example;
large_client_header_buffers 8 32k;
access_log /var/www/example/logs/access.log;
error_log /var/www/example/logs/error.log;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr; #$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_test;
proxy_redirect off;
}
location /test_site {
alias /var/www/test_site;
index index.php index.html index.htm;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
php5 www.conf file
[www]
...
user = www-data
group = www-data
listen = 127.0.0.1:9000
#listen = /tmp/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
...
My fastcgi_params file is default. I have checked both the php and nginx logs and there are no errors. Any help would be much appreciated!
Getting alias to work with nested locations using fastcgi is complicated.
Assuming that you have not over simplified your configuration, the test_site location does not need to use alias:
location /test_site {
root /var/www;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:127.0.0.1:9000;
include fastcgi_params;
}
}
This removes the alias directive, and solves the aliasing problem in the PHP block.
Note also: The regex on the location ~ \.php$ block was wrong. The fastcgi_split_path_info and fastcgi_index directives are unnecessary.
The nginx directives are documented here.

"Primary script unknown" PHP

I am trying to use PHP in my machine. I have nginx server running few other servers along.
Due to curiosity on how to use PHP , i tried to install php-cli and php5-fpm.
The nginx file below seems to work , however on front-end i get the error " 404". When i checked the error log , i found the error
"2016/03/29 14:28:50 [error] 19752#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: ::1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "localhost:84"
So i am assuming either my php5-fpm configuration is wrong or my nginx config is wrong .
Here is my nginx configuration file:
server {
listen 84 default_server;
listen [::]:84 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
index index.php index.html index.htm;
root /home/sijan/personal/php_site;
}
location ~ \.php$ {
try_files $uri =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;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php;
#include fastcgi_params;
}
}
I followed the following link from Digital Oceans and skip few steps to install MySQL since i will be using psql.
Any Help will be Highly appreciated.
I can't immediately see anything wrong with your nginx config but I've found that PHP-FPM and Nginx can be really really temperamental when configuring... Try the following Nginx config instead...
server {
listen 84 default_server;
listen [::]:84 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name _;
location / {
index index.php index.html index.htm;
root /home/sijan/personal/php_site;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_connect_timeout 10;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 512k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_keep_conn on;
include fastcgi_params;
}
}
Also, don't forget that you've got your Nginx server listening on port 84 in your config, not the standard port 80.

Categories