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
- An open SSH connection to your container. If you have not connected yet, follow Connect to your VPS over SSH.
- Optional: a domain of your own, set up as described in Point your own domain at your VPS. Without it, you use a free
*.vps.nasiadka.plsubdomain.
1. Install nginx and PHP-FPM
sudo apt update
sudo apt install -y nginx php-fpm
php -vAfter 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.
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;
}
}sudo nginx -t
sudo systemctl reload nginx3. Add your first page
sudo chown -R "$USER":www-data /var/www/html
nano /var/www/html/index.php<?php
echo "Hello from PHP " . PHP_VERSION;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
Open Domain Routing in the dashboard
Click "Use Free Subdomain". It fills in a full name such as
myapp.vps.nasiadka.pl. Changemyappto the name you want, so that the field holds the full name. The part before.vps.nasiadka.plmust be a single word (no dots), not already taken, and not a reserved name such asapp,api,admin,wwwordev. 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.Set "Target Port" to 80 and click "Add Route"
Port
80is 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 thatphp -vshows. - A 403 or 404 for a PHP file usually means a wrong file name, a wrong
rootpath or missing file permissions. PHP-FPM runs aswww-dataand 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.