0

I recently migrated a site from Apache to Nginx and now php code in html doesn't get parsed. .PHP pages parses without a problem as you can see in the attached image output of phpinfo.php in the bottom of this post.

This is how the code looks like in pages such as index.html:

<!-- Copyright -->
<? include("includes/copyright.php") ?>

        </body>
</html>

As you can see, there's no <?php tag wrapping the code, but still, Apache would execute the php include, which in this case adds a footer with copyright info to the page.

Again, this is NOT a .php file but a .html instead.

If I wrap the code in a <?php ?> tag and rename the file from .html to .php, the code will parse, but THIS IS NOT an option as the site has hundreds of pages and each file has multiple php commands such as the include above.

I've followed instructions in many posts but still, couldn't get it to work... Will really appreciate if someone could shed a light as I'm really not sure what else to try.

/etc/nginx/sites-available/mysite.com.conf

server {
        server_name mysite.com www.mysite.com;
        listen 111.111.11.111;
        root /home/mysite/public_html;
        index index.php index.htm index.html;
        access_log /var/log/virtualmin/mysite.com_access_log;
        error_log /var/log/virtualmin/mysite.com_error_log;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/mysite/public_html$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /home/mysite/public_html;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS $https;

        location ~ \.(php|html)$ {
                try_files $uri $fastcgi_script_name =404;
                fastcgi_pass unix:/var/php-nginx/1618253277225737.sock/socket;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        listen 111.111.11.111:443 ssl;
        ssl_certificate /home/mysite/ssl.combined;
        ssl_certificate_key /home/mysite/ssl.key;

.htaccess from Apache server:

AddType application/x-httpd-ea-php56 .html .htm
ErrorDocument 404 /error-page.html 
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript

# BEGIN gzip file compression
# Compress HTML, CSS, JavaScript, Text, XML and fonts
...

Output of phpinfo.php

short_open_tag is enabled

Hugo
  • 135
  • 1
  • 1
  • 7

2 Answers2

2

<? is a Short Open Tag.

Since PHP 5.3 (from 2009!) support is off by default. (It conflicts with other processing instructions, such as the XML declaration).

You can turn it on using the short_open_tag directive in php.ini

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • This is what I have on /etc/php/7.4/cgi/php.ini (according to the output of phpinfo.php above this is where my config file is... See image attachment above) ` ; short_open_tag ; Default Value: On ; Development Value: Off ; Production Value: Off ` I went ahead and added `short_open_tag = On` Then ran the following: `sudo systemctl restart php7.4-fpm.service` `sudo systemctl restart nginx` Same problem. No luck! – Hugo Apr 13 '21 at 12:43
  • option short tag is enabled as you can see in the screenshot above. Still no luck! – Hugo Apr 13 '21 at 13:45
  • I appreciate that Quentin put this answer because it's usually the one which we have to answer all the time on SO. @Hugo your server was misconfigured which happens but is rare given the history of what we've seen on here. In the future don't use ShortTags, there is a reason why they are disabled by default now. – hppycoder Apr 13 '21 at 14:05
  • @hppycoder, I believe Quentin answer makes sense. I understand the implications of using short open tags but unfortunately I have inherited this website and legacy code. – Hugo Apr 13 '21 at 14:21
0

Have you tried to add

types {
    application/x-httpd-ea-php56 html htm;
}

In location ?

Areg
  • 1,414
  • 1
  • 19
  • 39