nginx wont run php in html

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have been trying for weeks to get Nginx, and php7.2 to work together. I want to write html files, with embeded php tags. php files work splendidly. HTML files work as well. but when I try to embed php into html, the source code is printed to the html source. I have tried a variety of settings in my default config file for nginx, as well as numerous tutorials. It appears I am missing something but I cannot figure out what. Messing with it, I go throught various stages of inoperability, but I cannot get the php embeded part to work.

Here is my /etc/nginx/sites-available/default page:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=off;

    root /home/tinker/public_html;
    index index.html index.php index.htm;

    server_name 192.168.1.103;

    location / { 
        try_files $uri $uri/ =404;
        autoindex on; 
    }   

    error_page 404 /404.html;
    #error_page 500 502 503 504 /404.html;
    location = /50x.html {
    }   

   location ~ \.php$|\.html|\.htm {
        try_files $uri =404;

  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  #fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  include fastcgi_params;

  fastcgi_pass unix:/run/php/php7.2-fpm.sock;
#
 #      # fastcgi_index index.php;
 #      #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  #      fastcgi_index   index.php;
 #       fastcgi_param   PATH_INFO         $fastcgi_path_info;
 #      # fastcgi_param   PATH_TRANSLATED   $document_root$fastcgi_path_info;
 #      fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;    

    }   
}

how can i fix this?

profilepic.png
manpreet 2 years ago

 

you should install PHP and change Nginx config file

first, install PHP follow line:

sudo apt install php7.2 php7.2fpm

and change Nginx config file like the following:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.