March 19, 2024 · 5 min read · 493 views
When it comes to deploying applications, developers seek platforms that offer simplicity, efficiency, and robust features. DigitalOcean has been a go-to cloud computing provider since 2012, known for its straightforward virtual machines, or "Droplets," and a suite of services designed for developers and small-to-medium-sized businesses. Meanwhile, Laravel Forge is the premier tool for server management, allowing for the effortless deployment of PHP applications, including Laravel and Filament sites, on various cloud hosts like DigitalOcean.
Before initiating the deployment of your Filament app, ensure you have a domain with an A Record pointing to your DigitalOcean server. This step is critical as it establishes the connection between your domain name and the server's IP address, paving the way for users to access your app.
To kick off the deployment, head over to the Laravel Forge website. Here, you can manage your servers and applications with ease. Log in to your account, select "DigitalOcean" as your cloud provider, and begin the process of setting up your Filament app.
Forge simplifies the creation of a new site on your DigitalOcean server. Specify your root domain and ensure the selected PHP version is compatible with your Filament project, as indicated in the composer.json
file. In our case, we had to change the default PHP version "8.0" to our project requirements of PHP8.1
. Toggle the button Create Database
and write the database name of your project.
Linking your Git repository to Forge is a breeze. Input your repository details, choose the correct branch, and let Forge install your Filament site's code onto your DigitalOcean server.
Customize your Filament site's environment within Forge:
Update the APP_NAME
Secure your APP_URL with HTTPS
APP_KEY= should be a unique, random string. If this key is missing, you can generate one by navigating to the command section in Forge and executing "[email protected] artisan key:generate".
APP_NAME="Your Site"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=https://yoursite.com
Security is a top priority, and Forge's integration with Let's Encrypt allows you to easily secure your Filament app with free TLS certificates, ensuring encrypted connections for your users on DigitalOcean. Go to SSL and click Let's Encrypt to create your certificate. Wait a few moments and you will see your certificate in the table as displayed in our image below.
Forge's push-to-deploy feature streamlines the deployment process. There are only a few small changes that we should do. Copy the highlighted section below and add that to your deployment script.
cd /home/forge/yoursite.com
git pull origin $FORGE_SITE_BRANCH
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
$FORGE_PHP artisan optimize:clear
$FORGE_PHP artisan cache:clear
$FORGE_PHP artisan config:cache
$FORGE_PHP artisan route:cache
$FORGE_PHP artisan view:cache
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
Follow this section only if your app needs the queue. When you're working on your web app, some tasks can slow things down during regular requests. The cool thing with Laravel is that you can create queued jobs. These let time-intensive tasks happen in the background, making your app super responsive and giving your users a smoother experience.
On Laravel Forge, navigate to the Queues section and only change the Maximum Seconds Per Job to 300
, that's more than enough to wait for a job to be finished. Clicking Create
will launch your queue worker.
It's essential to configure the queue correctly to ensure that your jobs will run. Within your project's .env section, set your QUEUE_CONNECTION to redis
to make use of its fast, in-memory data store capabilities. Here's a snippet for clarity:
QUEUE_CONNECTION=redis
REDIS_DATABASE=0
If you're running multiple sites on the same server, assign each one to a distinct Redis database to prevent any overlap. Simply increment the REDIS_DATABASE number accordingly (e.g., 0, 1, 2, etc.).
Below, you'll find a full example of our frequently used configuration, which you can tailor to fit the specific needs of your Filament app:
APP_NAME="Lucky Media"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL="https://www.luckymedia.dev"
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_app_database_name
DB_USERNAME=forge
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=redis
REDIS_DATABASE=0
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=""
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
DEBUGBAR_ENABLED=false
Keep in mind: Don't forget to change your DB_DATABASE
field to the database name that you chose at the beginning of this script.
Before saving the modified Environment file, make sure you tick the option Run php artisan config:cache after saving
and that will make sure that the app will be updated with the new changes.
Before deploying the app we need to make sure that we have an admin user for initial login. This can be done by including a production seeder in your app. Make sure that you wrap all seeders within an if condition
if you don't want them to appear in production:
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => Hash::make('password'),
'admin' => true,
]);
// these seeders are for local development only
if (App::environment('local')) {
$this->call([
// Other seeders
]);
}
}
}
If you already have this on your project then simply head over to Forge and select the Commands tab. Type the following command to seed your database with the admin user.
php artisan db:seed --force
We need the --force
flag because our app is in production. Now we can use the seeded admin user to log in to our app.
Hit the Deploy Now button and wait for the output. Done! After a successful deployment, your Filament app is up and running on DigitalOcean, ready for users to enjoy. If you navigate to your app link you should see the login screen unless you have created a public page too. Use your admin credentials to log in and navigate through your app.
Laravel Forge has provided a seamless and secure deployment experience, from server management to live deployment. Enjoy the benefits of a well-deployed Filament app on a robust DigitalOcean server, and take advantage of the powerful features that Forge offers for ongoing management and monitoring.
If you need help with a Laravel project let's get in touch.
Lucky Media is proud to be recognized as a Top Laravel Development Agency
Technologies:
Related Posts
Stay up to date
Be updated with all news, products and tips we share!