This guide covers installing Grav on a fresh Ubuntu 24.04 LTS (Noble Numbat) VPS with Nginx and PHP 8.3.
Initial Server Setup
First, set up a local /etc/hosts entry to give your server IP a friendly name such as digitalocean.dev. This makes it easier to SSH to your server:
Update System Packages
After connecting as root, update all installed packages:
apt update && apt upgrade -y
Install Required Packages
Install Nginx, PHP 8.3, and essential extensions for Grav:
apt install -y vim zip unzip nginx git \
php8.3-fpm php8.3-cli php8.3-gd php8.3-curl \
php8.3-mbstring php8.3-xml php8.3-zip php8.3-intl php8.3-apcu
This installs:
- Nginx - High-performance web server
- PHP 8.3-FPM - FastCGI Process Manager for PHP
- PHP Extensions - Required by Grav for image processing, caching, etc.
Configure PHP-FPM
Edit the PHP configuration for better security:
vim /etc/php/8.3/fpm/php.ini
Find cgi.fix_pathinfo (use /cgi.fix_pathinfo in vim to search), uncomment it and set to 0:
cgi.fix_pathinfo=0
Warning
This setting prevents PHP from executing the closest matching file when the requested file isn't found - a significant security risk if left enabled.
Create a Dedicated User
Create a grav user to run the site (don't run web apps as root):
adduser grav
Provide a strong password when prompted.
Configure PHP-FPM Pool
Create a dedicated PHP-FPM pool for the grav user:
cd /etc/php/8.3/fpm/pool.d
mv www.conf www.conf.bak
vim grav.conf
Add the following configuration:
1[grav]
2user = grav
3group = grav
4
5listen = /run/php/php8.3-fpm-grav.sock
6
7listen.owner = www-data
8listen.group = www-data
9
10pm = dynamic
11pm.max_children = 10
12pm.start_servers = 3
13pm.min_spare_servers = 2
14pm.max_spare_servers = 5
15
16chdir = /
Create Web Directory
Switch to the grav user and create the web directory:
su - grav
mkdir -p ~/www/html
Create a test file to verify the setup:
echo '<?php phpinfo();' > ~/www/html/info.php
exit
Configure Nginx
Create the Nginx server block:
vim /etc/nginx/sites-available/grav
Add the following configuration:
1server {
2 listen 80;
3 index index.html index.php;
4
5 ## Begin - Server Info
6 root /home/grav/www/html;
7 server_name _;
8 ## End - Server Info
9
10 ## Begin - Index
11 location / {
12 try_files $uri $uri/ /index.php?$query_string;
13 }
14 ## End - Index
15
16 ## Begin - Security
17 # deny hidden files and directories at any depth, except ACME challenges
18 location ~ (^|/)\.(?!well-known(?:/|$)) { return 403; }
19 # deny all direct access for these folders
20 location ~* ^/(\.git|cache|bin|logs|backup|tmp|webserver-configs|tests)/.*$ { return 403; }
21 # deny all direct access to these sensitive user folders, whatever the file type
22 location ~* ^/user/(env/[^/]+/)?config/.*$ { return 403; }
23 # allow avatar images under user/accounts to be served directly, whether stored
24 # at user/accounts/avatars/<file> (flatfile accounts) or user/accounts/<username>/
25 # <file> (Flex folder storage); this must come before the user/accounts deny so it
26 # wins the first-match. SVG is intentionally excluded as a stored-XSS vector.
27 location ~* ^/user/(env/[^/]+/)?accounts/[^/]+/[^/]+\.(jpe?g|png|gif|webp|avif|bmp|ico)$ { try_files $uri =404; }
28 # deny everything else under user/accounts, whatever the file type
29 location ~* ^/user/(env/[^/]+/)?accounts/.*$ { return 403; }
30 # allow public media uploads under user/data (e.g. Flex Object images) to be
31 # served directly; this must come before the user/data deny so it wins the
32 # first-match. SVG is intentionally excluded as a stored-XSS vector.
33 location ~* ^/user/(env/[^/]+/)?data/.*\.(jpe?g|png|gif|webp|avif|bmp|ico|mp4|webm|ogg|ogv|mov|mp3|wav|m4a|flac|pdf)$ { try_files $uri =404; }
34 # deny everything else under user/data, whatever the file type
35 location ~* ^/user/(env/[^/]+/)?data/.*$ { return 403; }
36 # Serve page media only through the page route, so plugins can apply the
37 # page's `access` rules to it. DO NOT uncomment without first setting
38 # `pages.media_route_urls: true` in user/config/system.yaml, or every media
39 # URL on the site becomes a 403.
40 # location ~* ^/user/pages/.*$ { return 403; }
41 # deny running scripts in the public cache folders (image derivatives and combined assets)
42 location ~* ^/(images|assets)/.*\.(php|php2|php3|php4|php5|php7|php8|phar|phtml|pht|phtm|phps|pl|py|cgi|sh|bat)$ { return 403; }
43 # deny running scripts inside core system folders
44 location ~* ^/(system|vendor)/.*\.(txt|xml|md|html|htm|shtml|shtm|json|yaml|yml|php|php2|php3|php4|php5|phar|phtml|pl|py|cgi|twig|sh|bat)$ { return 403; }
45 # deny running scripts inside user folder
46 location ~* ^/user/.*\.(txt|md|json|yaml|yml|php|php2|php3|php4|php5|phar|phtml|pl|py|cgi|twig|sh|bat)$ { return 403; }
47 # deny access to specific files in the root folder
48 location ~* ^/(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
49 # deny access to .env environment files (expression purposefully not anchored)
50 location ~ /\.env(\.|$) { return 403; }
51 # deny Markdown source files such as README.md and CHANGELOG.md; a page route
52 # ending in .md is not a file, so it still reaches Grav's Markdown output
53 location ~* \.md$ {
54 if (-f $request_filename) { return 403; }
55 try_files $uri /index.php?$query_string;
56 }
57 ## End - Security
58
59 ## Begin - PHP
60 location ~ \.php$ {
61 fastcgi_pass unix:/run/php/php8.3-fpm-grav.sock;
62 fastcgi_split_path_info ^(.+\.php)(/.+)$;
63 fastcgi_index index.php;
64 include fastcgi_params;
65 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
66 }
67 ## End - PHP
68}
Enable the site and remove the default:
ln -s /etc/nginx/sites-available/grav /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default
Test the configuration:
nginx -t
You should see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Start Services
Restart Nginx and PHP-FPM:
systemctl restart nginx
systemctl restart php8.3-fpm
Verify PHP is working by visiting http://YOUR_SERVER_IP/info.php. You should see the PHP info page with PHP 8.3 and APCu listed.
Caution
Remove the info.php file after testing: rm /home/grav/www/html/info.php
Install Grav
Switch to the grav user and download Grav:
su - grav
cd ~/www
wget -O grav.zip https://getgrav.org/download/core/grav/latest
unzip grav.zip
rm -rf html
mv grav html
Verify Installation
Visit http://YOUR_SERVER_IP and you should see the Grav is Running! page.
Test CLI Tools
Since you're running as the grav user, CLI tools work out of the box:
cd ~/www/html
bin/grav clear
Output:
Clearing cache
Cleared: cache/twig/*
Cleared: cache/compiled/*
Touched: /home/grav/www/html/user/config/system.yaml
GPM commands also work:
bin/gpm index
Optional: Install Admin Plugin
To install the Grav Admin panel:
bin/gpm install admin
Then visit http://YOUR_SERVER_IP/admin to create your admin account.
Optional: Enable HTTPS with Let's Encrypt
For production sites, enable HTTPS using Certbot:
apt install -y certbot python3-certbot-nginx
certbot --nginx -d my-site.example
Certbot will automatically configure Nginx for SSL and set up auto-renewal.