Ubuntu 24.04 VPS
A VPS (virtual private server) gives you a whole Linux server for a few dollars a month, with full control over the web server and PHP. This guide covers installing Grav 2 on a fresh Ubuntu 24.04 LTS (Noble Numbat) VPS with Nginx and PHP 8.3, the PHP version Ubuntu 24.04 ships and Grav 2's minimum.
Create the server
Any provider that offers Ubuntu 24.04 LTS works, and the steps below are the same once you can log in. Popular choices include DigitalOcean (servers are called Droplets), Akamai Cloud, formerly Linode (Linodes), and Vultr (Cloud Compute instances). In your provider's control panel:
- Create a new server and pick Ubuntu 24.04 LTS as the image.
- Choose a size. Grav has no database and runs comfortably on the smallest plans; 1 CPU and 1 GB of memory is plenty for most sites.
- Choose a region close to your visitors, or close to you for a development server.
- Add your SSH public key so you can log in without a password. If you don't have one yet, run
ssh-keygen -t ed25519on your own computer and paste the contents of~/.ssh/id_ed25519.pub. - Create the server and note its public IP address.
Then connect as root, replacing YOUR_SERVER_IP with that address:
ssh root@YOUR_SERVER_IP
Tip
Point a domain at the server now (an A record for the IP address), so DNS has time to update before you enable HTTPS at the end of this guide.
Update System Packages
After connecting as root, update all installed packages:
apt update && apt upgrade -y
Turn on the firewall, allowing only SSH and web traffic:
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enable
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 \
php8.3-sqlite3
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.
php8.3-sqlite3is optional and lets Grav use its faster SQLite page index on large sites.
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. It is the webserver-configs/nginx.conf file shipped with every Grav release, with the server info and PHP socket filled in for this guide; when you upgrade Grav later, compare it with the shipped file and copy any changed rules across, since Nginx does not read Grav's .htaccess:
1server {
2 listen 80;
3 index index.html index.php;
4
5 ## Begin - Server Info
6 root /home/grav/www/html;
7 server_name _; # or your domain, e.g. example.com
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 - Static asset caching
60 # MUST stay below the security block. nginx picks the FIRST matching regex
61 # location, so a caching rule placed above would answer for paths the denies
62 # above are meant to refuse. For the same reason these use regex (`~*`) and
63 # never `^~`: a `^~` prefix match wins over every regex location regardless
64 # of order, which would bypass the user/accounts and user/data rules.
65
66 # Content-hashed bundles shipped by SvelteKit-based plugins and themes (the
67 # Admin panel among them). The filename changes whenever the contents do, so
68 # these can be cached permanently. `=404` keeps a missing chunk from booting
69 # PHP just to render a 404 — and, on any host that serves an SPA from a
70 # static index.html fallback, stops a deleted chunk being answered with the
71 # HTML shell at 200, which a browser cannot parse as a module.
72 location ~* /_app/immutable/.*\.(js|css|woff2?)$ {
73 try_files $uri =404;
74 access_log off;
75 add_header Cache-Control "public, max-age=31536000, immutable" always;
76 }
77
78 # Everything else static gets a moderate TTL and still revalidates via the
79 # ETag/Last-Modified nginx already sends. Deliberately NOT `immutable`:
80 # these filenames are stable across edits, so a long hard cache would pin
81 # stale media until the name changed.
82 # The try_files fallback is load-bearing, not boilerplate: Grav serves some
83 # media through index.php (cached image derivatives, plugin media proxies),
84 # so a bare static handler would hard-404 assets that PHP would have
85 # produced on demand. Adding caching must not change WHAT is served.
86 location ~* \.(jpe?g|png|gif|webp|avif|bmp|ico|svg|mp4|webm|ogg|ogv|mov|mp3|wav|m4a|flac|css|js|woff2?|ttf|otf|eot)$ {
87 try_files $uri $uri/ /index.php?$query_string;
88 expires 30d;
89 access_log off;
90 }
91 ## End - Static asset caching
92
93 ## Begin - PHP
94 location ~ \.php$ {
95 fastcgi_pass unix:/run/php/php8.3-fpm-grav.sock;
96 fastcgi_split_path_info ^(.+\.php)(/.+)$;
97 fastcgi_index index.php;
98 include fastcgi_params;
99 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
100 }
101 ## End - PHP
102}
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 Say Hello to Grav 2.0! 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 the Admin
To install Admin Next, the Grav 2 admin panel:
bin/gpm install admin2
GPM installs the API plugin and the rest of its dependencies along with it. Then visit http://YOUR_SERVER_IP/admin to create your admin account.
Warning
Do not run bin/gpm install admin. That is the classic Grav 1.x admin and it does not work on Grav 2. If you would rather skip this step, download the Grav + Admin package (https://getgrav.org/download/core/grav-admin/latest) in the install step above instead.
Optional: Enable HTTPS with Let's Encrypt
For production sites, enable HTTPS using Certbot. Your domain must already point at the server, and the server_name line in /etc/nginx/sites-available/grav must be your domain instead of _ (reload Nginx after changing it):
apt install -y certbot python3-certbot-nginx
certbot --nginx -d example.com
Certbot will automatically configure Nginx for SSL and set up auto-renewal.