1

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

8
  • 1
    Does this answer your question? HTML files as PHP in Nginx Commented Apr 13, 2021 at 12:14
  • Already tried that. Didn't work. Commented Apr 13, 2021 at 12:17
  • Have you tried AddHandler php-script .html .htm in your htaccess? Commented Apr 13, 2021 at 12:19
  • This is on Nginx and not Apache. Commented Apr 13, 2021 at 12:20
  • Have you restarted server when you made changes to nginx config? Commented Apr 13, 2021 at 12:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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!
option short tag is enabled as you can see in the screenshot above. Still no luck!
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, 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.
0

Have you tried to add

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

In location ?

2 Comments

My issue is in Nginx and not Apache.
@Hugo this is nginx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.