3

On my server, I am using nginx with php5-fpm (not Apache).

All the HTML, JavaScript and php code in my index.php file runs fine. But if I rename the file to index.html, the embedded php code doesn't execute. (All the HTML and JavaScript code continue to work fine.)

Several answers recommend adding the line

AddType application/x-httpd-php5 .html .htm

to the .htaccess file, but after downloading, installing and configuring nginx and php5-fpm, there was no .htaccess file on my server. So I created one, pasted the above line and placed the file in the website's root directory along with index.html. I restarted nginx, but the php code still does not work. (To be absolutely sure, I simply changed the file extension from html to php and everything is perfect.)

When I use firebug on the rendered page in my browser, the php tag and code don't appear to be there.

CC Inc
  • 5,842
  • 3
  • 33
  • 64
W R Knight
  • 59
  • 1
  • 5
  • 1
    What do you mean by " When I use firebug on the rendered page in my browser, the php tag and code don't appear to be there."? – CY5 Aug 16 '15 at 18:42
  • Anything helpful here? http://stackoverflow.com/questions/8644783/html-files-as-php-in-nginx – Shomz Aug 16 '15 at 18:47
  • 1
    `.htaccess` files are an apache-only feature and nginx will never support such a feature. You need to add something like that to the vHost config. – Charlotte Dunois Aug 16 '15 at 18:49
  • http://www.softwareprojects.com/resources/programming/t-nginx-html-files-as-php-1817.html – pool pro Aug 16 '15 at 19:02
  • I don't know what you mean by vHost and there is no such file on my server. If I have to create one, I have no idea where to put it. – W R Knight Aug 16 '15 at 19:51
  • There appear to be no virtual host configuration files on my server. There is also no /etc/nginx/conf.d folder and there appear to be no virtual host configuration files anywhere. Do I have to create these and does someone have a good example for what they should contain? – W R Knight Aug 16 '15 at 21:15

3 Answers3

1

OK, finally found the solution. Someone had posted that I needed to change the location statement in my configuration file to

location ~ \.(php|html)$ {

so that both php and html files would be run through fastcgi. When I tried this, I got an "access denied" error. The problem there was that by default, nginx is restricted to running only php files through fastcgi. This can be changed by uncommenting the line near the bottom of:

/etc/php5/fpm/pool.d/www.conf

that reads

;security.limit_extensions = .php .php3 .php4 .php5

and adding .html so that it now reads

security.limit_extensions = .php .html

when I reloaded and restarted nginx, voila! It works perfect.

While these two changes allow ngnix to process php code inside html files, the other side of the story is that this increases the vulnerability of your server to malicious php code embedded in html files that might get uploaded to it. The recommendation that I received was to give all your files with php code in them the .php extension.

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
W R Knight
  • 59
  • 1
  • 5
  • I don't know why the backslash didn't show up in the location statement in front of \.(php|html) in my answer, but it has to be there for the code to work. I am unfamiliar with the editing features and quirks on this site. – W R Knight Aug 19 '15 at 14:47
1

Wow, just spent hours on this!!! The final solution is correct from above.

It appears that if "/etc/php5/fpm/pool.d/www.conf" has the full original line:security.limit_extensions = .php .php3 .php4 .php5 you can not just ADD .html .js

The line reads:security.limit_extensions = .php .html and now it works!!!

0

Edit your conf file:

location ~ \.htm$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.htm;
            include        fastcgi.conf;
  }
Muntashir Akon
  • 8,740
  • 2
  • 27
  • 38
  • Didn't realize at first. Thanks for informing anyway. @CharlotteDunois – Muntashir Akon Aug 16 '15 at 19:18
  • Not sure what happened. There was no block like the one you posted. I changed the .php$ to .htm$ in the php location block and nothing happened. I added a new block exactly as you posted and got an access denied message. – W R Knight Aug 16 '15 at 20:10
  • OK, I made a goofy mistake and forgot to put $ after .htm. Having fixed that mistake and retried, webpage returned but no php input. – W R Knight Aug 16 '15 at 20:23
  • Also location ~ \.(php|html|htm)$ { results in access denied message. – W R Knight Aug 16 '15 at 21:16
  • It would be great if you posted the error message along with your edits in conf @user309572 – Muntashir Akon Aug 17 '15 at 07:36