Host a PHP site with nginx and PHP-FPM

This guide sets up a PHP site in your container. nginx is the web server, and PHP-FPM runs the PHP code. You then publish the site over HTTPS with one route in Domain Routing. The installation takes about half a minute.

What you need

1. Install nginx and PHP-FPM

Install the packages and check the PHP version
sudo apt update
sudo apt install -y nginx php-fpm
php -v

After the installation, nginx listens on port 80. You point the route at this port in step 4.

2. Configure nginx to run PHP files

Open the default site with sudo nano /etc/nginx/sites-available/default. Replace everything in it with the block below and save the file.

/etc/nginx/sites-available/default
server {
    listen 80 default_server;
    root /var/www/html;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}
Check the configuration and reload nginx
sudo nginx -t
sudo systemctl reload nginx

3. Add your first page

Make the web root writable for your user, then open the page file
sudo chown -R "$USER":www-data /var/www/html
nano /var/www/html/index.php
/var/www/html/index.php
<?php
echo "Hello from PHP " . PHP_VERSION;
Request the page from the container
curl http://localhost/

The output starts with Hello from PHP and ends with the PHP version number.

To host your own site instead, upload its files to /var/www/html with SFTP or scp. Both use the same port as SSH (2137).

4. Publish it

  1. Open Domain Routing in the dashboard

    Click "Use Free Subdomain". It fills in a full name such as myapp.vps.nasiadka.pl. Change myapp to the name you want, so that the field holds the full name. The part before .vps.nasiadka.pl must be a single word (no dots), not already taken, and not a reserved name such as app, api, admin, www or dev. To use your own domain instead, type it in the same field after you add it in DNS Management. See Point your own domain at your VPS.

  2. Set "Target Port" to 80 and click "Add Route"

    Port 80 is already filled in. Your site is now live over HTTPS, and the certificate is managed for you.

Troubleshooting

  • A 502 error usually means PHP-FPM is not running. Check with systemctl status php8.4-fpm, using the version number that php -v shows.
  • A 403 or 404 for a PHP file usually means a wrong file name, a wrong root path or missing file permissions. PHP-FPM runs as www-data and must be able to read the files.
  • Read the nginx error log with sudo tail -f /var/log/nginx/error.log.
  • Do not leave a page that calls phpinfo() on a public site, because it reveals details about your server.

Follow along on your own container

Already have an account? Sign in to try this guide on your container. Accounts are created by invitation.

Sign in