How can I enable PHP on nginx?

Hello world!

I would need to create PHP script to pull data from a server to display on a html webpage. I tried:

   <ul>
     <?php for($i=1;$i<=5;$i++){ ?>
        <li>Menu Item <?php echo $i; ?></li>
      <?php } ?>
    </ul> 

It should create 4 list items but it did not so I guess there is no php enabled on the nginx docker server.

I see some docker-compose files that have something with php, I just seem to be too stupid to understand anything else than html, so can someone tell me what I would need to add to my compose file so I can have php run on the server?

you’ll need two containers.

Nginx is an http server, it serves http request by replying with either file contents, error pages… or it can “reverse proxy” http request to another http server, … or it can proxy a request to php or other things using something called fastcgi.

It has a configuration file where you can tell it what to do with each requestt… For example: if the URL ends with .php, it can forward it to a socket (file system or port) where php is listening, otherwise it can serve it from a directory.

So, you can start php:8.0.0-fpm-alpine version of php container with www/data volume pointing at your webroot and port 9000 listening for fastcgi requests. And you can start nginx:latest-alpine with a -v mounted in the same webroot, and a config file mount pointing to 9000 with all the relevant parameters.

Technically, you could just use a single container, then again technically you could just write your app to answer http in c++ with all the static assets compiled in… you asked about nginx and php , so there you go.