Skip to main content

Install Mixpost Lite In an existing Laravel app

If you are using Laravel (v9 or v10), it's recommended to install Mixpost Lite as a package within your application. This approach is ideal for those already familiar with Laravel.


warning

Before proceeding, please review the server requirements and install all necessary softwares to ensure that Mixpost functions properly.

1. Installing via Composer

composer require inovector/mixpost

After installing the Mixpost package, you may execute:

php artisan mixpost:install

To ensure that the assets get republished each time Mixpost Lite is updated, we strongly advise you to add the following command to the post-update-cmd of the scripts section of your composer.json.

"scripts": {
"post-update-cmd": [
"@php artisan mixpost:publish-assets --force=true"
]
}

2. Job Batching

Mixpost uses Job Batching and you should create a database migration to build a table to contain meta information about your job batches.

If your application does not yet have this table, it may be generated using the:

php artisan queue:batches-table

Run the migrations with:

php artisan migrate

3. Publish the config file

Optionally, you can publish the config file with this command:

php artisan vendor:publish --tag=mixpost-config

4. Install Horizon

Mixpost handles various tasks in a queued way via Laravel Horizon. If your application doesn't have Horizon installed yet, follow their installation instructions.

After Horizon is installed, don't forget to set QUEUE_CONNECTION in your .env file to redis.

config/horizon.php should have been created in your project. In this config file, you must add a block named mixpost-heavy to both the production and local environment.

'environments' => [
'production' => [
'supervisor-1' => [
'maxProcesses' => 10,
'balanceMaxShift' => 1,
'balanceCooldown' => 3,
],
'mixpost-heavy' => [
'connection' => 'mixpost-redis',
'queue' => ['publish-post'],
'balance' => 'auto',
'processes' => 8,
'tries' => 1,
'timeout' => 60 * 60,
],
],

'local' => [
'supervisor-1' => [
'maxProcesses' => 3,
],
'mixpost-heavy' => [
'connection' => 'mixpost-redis',
'queue' => ['publish-post'],
'balance' => 'auto',
'processes' => 3,
'tries' => 1,
'timeout' => 60 * 60,
],
],
],

In the config/queue.php file you must add the mixpost-redis connection:

'connections' => [

// ...

'mixpost-redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 11 * 60,
'block_for' => null,
],

Don't forget to run php artisan horizon. In production, you need a way to keep your horizon processes running. For this reason, you need to configure a process monitor like Supervisor that can detect when your horizon processes exit and automatically restart them.

Before configuring a Supervisor process monitor, make sure you have installed Redis and Supervisor on your server.

Create mixpost_horizon.conf file inside of /etc/supervisor/conf.d folder and put this content:

[program:mixpost_horizon]
process_name=%(program_name)s
command=php /path-to-your-project/artisan horizon
autostart=true
autorestart=true
user=your_user_name
stopwaitsecs=3600

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start mixpost_horizon:*

5. Schedule the commands

In the console kernel app/Console/Kernel.php, you should schedule these command:

protected function schedule(Schedule $schedule)
{
// ...
$schedule->command('mixpost:run-scheduled-posts')->everyMinute();
$schedule->command('mixpost:import-account-data')->everyTwoHours();
$schedule->command('mixpost:import-account-audience')->everyThreeHours();
$schedule->command('mixpost:process-metrics')->everyThreeHours();
$schedule->command('mixpost:delete-old-data')->daily();
}

6. Add authorization to Mixpost Dashboard

Mixpost Lite does not come with any user management, we assume that you already provide this in your own app. You can use a gate check to determine who can access Mixpost.

However, we have created a separate package Mixpost Auth that you can install very easily. Read the documentation of this package to find out how to install it.

You can determine which users of your application are allowed to view the Mixpost Dashboard by defining a gate check called viewMixpost in your app/Providers/AppServiceProvider.php file.

public function boot()
{
\Illuminate\Support\Facades\Gate::define('viewMixpost', function ($user = null) {
return optional($user)->email === 'email@example.com';
});
}

Mixpost will redirect unauthorized users to the route name specified in the redirect_unauthorized_users_to_route key of the Mixpost config file.

7. Done

After performing all these steps, you should be able to visit the Mixpost Dashboard at /mixpost.