How to install Laravel + Livewire + Filament on production

I will setup a domain on a Linux Debian production server using Nginx and PHP 8.1

First the nginx config after generated the certbot https certificates:

server {
	listen 80;
	server_name mydomain.com www.mydomain.com;
	return 301 https://mydomain.com$request_uri;
}

server {
	listen 443 ssl;
	server_name www.mydomain.com;
	return 301 https://mydomain.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
   root /var/www/mydomain.com/public;
    index index.php;
    server_name mydomain.com;
    charset   utf-8;
    gzip on;
    gzip_vary on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;

    # Remove index.php$
    if ($request_uri ~* "^(.*/)index\\.php/*(.*)") {
        return 301 $1$2;
    }

    # BLOCK USER AGENTS
    if ($block_ua) { return 444; }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \\.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_read_timeout 300;
    }
    location ~* \\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
      expires 12M;
      access_log off;
      add_header Cache-Control "public";
    }
    location ~ /\\. {
        deny  all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

Install Laravel

As I also have sites using PHP 7 in the same server, I use “composer8” and “artisan8” as aliases of “php8.1 composer” and “php8.1 artisan”.

composer8 create-project laravel/laravel mydomain.com
  • Now add db config to laravel’s .env file

Laravel breeze (laravel authentication)

composer8 require laravel/breeze --dev
artisan8 breeze:install
npm install
npm run dev
artisan8 migrate
  • Install livewire
composer8 require livewire/livewire
  • Add livewire tags to app layout
@livewireStyles
 </head>
 <body>
   .....
 @livewireScripts
 </body>
 </html>
  • Add filament
composer8 require filament/filament:"^2.0"

Add to composer json:

"post-update-cmd": [
    // ...
    "@php artisan filament:upgrade"
],

If you don’t have one, you may create a new user account using:

artisan8 make:filament-user
artisan8 livewire:publish
  • then open config/livewire.php change ‘asset_url’ => null to ‘asset_url’ => ‘https://yourdomain.com

Update App\Models\User:

namespace App\Models;
 
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable implements FilamentUser
{
    // ...
 
    public function canAccessFilament(): bool
    {
        return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
    }
}

And done! you now should have a working installation of Laravel + Livewire + Filament on production server through a real domain 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *