Setup Raspberry Pi to serve a Laravel based website
06.02.2025The official website of Laravel has already a documentation on howto install it on a Linux OS, but it requires to trust the installer script. This would have been run directly on the local machine without prior checking the contents and what is really done. Of course I manually checked the install script and did not like what I found. The installed elements would not be tracked by the package manager, it would install another certificate authority list for the local installation. Things I consider non-maintainable or unsafe. So I wanted to go another (more clean?) way on a Raspberry Pi.
- First I needed to install the necessary tools using the commandline.
- I wanted to use
nginx
to serve the websites. - I wanted to use
php-fpm
because it is faster than the apache and/or cgi combinations. And of course because it runs on a Raspberry Pi. - I needed
composer
for php packages.
aptitude install nginx php-fpm composer
- I wanted to use
-
I then configured nginx to serve a new site by adding a file
/etc/nginx/sites-available/laravel-www
:server { listen 80 default_server; listen [::]:80 default_server; root /var/www/laravel-www; index index.php; server_name _; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass unix:/run/php/php-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi.conf; fastcgi_hide_header X-Powered-By; } }
This already contains the necessary parts for serving on a HTTP server (no SSL here at the moment!) using the installed PHP version. I used the version-free
php-fpm
symlink so updating/changing to another PHP version is easier later. - The site was then activated to be served already, as it was not public on the internet:
ln -s /etc/nginx/sites-available/laravel-www /etc/nginx/sites-enabled/laravel-www nginx -s reload
- Now I used
composer
to get the basic laravel installation:cd /var/www/ mkdir laravel-www composer create-project laravel/laravel laravel-www
- For the installation to work, I needed to setup some access rights, as
nginx
is running aswww-data
user:chown -R www-data.www-data /var/www/laravel-www
- For the Laravel project itself, my first start should used the
sqlite
database to just show that the laravel part is working. So I initialized it using the default configurations in the/var/www/laravel-www/.env
file via:cd /var/www/laravel-www php artisan migrate
- Now I had the basic laravel project up and running (For later reference: Laravel v11.41.3 (PHP v8.2.7))