All Blog Articles

Payload NextJS: Deploying Payload with MongoDB on Coolify

Media
Sandro WegmannMay 21, 2025
Deploy Payload CMS with MongoDB on Coolify: Affordable self-hosting with full GDPR compliance and data control.

In today's digital landscape, the ability to host your applications independently can be a game changer. In this guide, we will explore how to set up a Payload NextJS project with a MongoDB database using Coolify. The process not only allows you to run production workloads affordably but also ensures you remain fully GDPR compliant. Whether you're a developer looking to streamline your database management or simply curious about the capabilities of Payload and MongoDB, this article will provide you with all the insights you need.

This article provides a detailed written step-by-step tutorial based on our comprehensive video guide available on YouTube at https://www.youtube.com/watch?v=OOFIMV5Up3A. If you're a visual learner or prefer following along with video instruction, the accompanying video covers the same content while offering additional context and real-time demonstrations of each step in the process. Whether you prefer reading or watching, both resources will guide you through deploying Payload with MongoDB on Coolify for independent, cost-effective hosting.

Table of Contents

Understanding the Need for Self-Hosting

When it comes to managing databases, many developers rely on third-party services like MongoDB Atlas for ease of use and convenience. However, there are several compelling reasons to consider self-hosting your database. First and foremost, self-hosting can reduce dependency on external services, giving you greater control over your data. This independence is crucial for various reasons:

  • Cost Efficiency: While MongoDB Atlas offers a generous free tier, costs can escalate quickly with larger datasets or dedicated clusters. For example, a dedicated server can start at around $50 a month, which can be quite steep.
  • Data Control: By self-hosting, you eliminate risks associated with changing collection schemas in SQL databases like Postgres. Mistakes can lead to data loss, a risk that is mitigated with NoSQL databases like MongoDB.
  • Performance Benefits: Hosting your database on the same server as your Payload application can lead to performance improvements since data doesn’t have to traverse the public internet.
  • GDPR Compliance: For businesses operating in Europe, GDPR compliance is crucial. Self-hosting your database allows for greater control over your data management practices.

Setting Up Your Payload Project

Now that we've established the importance of self-hosting, let’s dive into the practical steps of setting up a Payload project with MongoDB on Coolify. First, ensure you have a GitHub repository ready for your project.

Follow these steps to create your Payload project:

  1. Clone your empty GitHub repository to your local machine.
  2. Run the command npx create payload app@beta to initialize your Payload application. This command will set up the latest beta version of Payload.
  3. When prompted, name your project. For this example, we'll call it payload-mongotest.
  4. Select the blank project template and MongoDB as your database option.
  5. Leave the local connection string as is for now; it will be replaced later.

Once the setup is complete, you can verify that your local Payload project is running correctly.

Configuring Coolify

Before we create our database in Coolify, let’s configure it for optimal performance. One feature to take advantage of is the wildcard domain. This allows you to automatically create DNS records for your projects. Here’s how to set it up:

  1. Navigate to your Coolify project settings.
  2. Add a wildcard domain in the format *.projects.allaboutpayload.com.
  3. Save the settings to apply the changes.

This configuration ensures that whenever you deploy a new project, it will automatically be assigned a unique subdomain under your wildcard domain.

Creating Your MongoDB Database

With Coolify configured, it's time to create your MongoDB database:

  1. In Coolify, create a new project named payload-mongo.
  2. Select the production environment for deployment.
  3. Choose a separate server for your MongoDB database to maintain good practice.
  4. Start the project, which will SSH into your remote server and initiate the MongoDB database.

After starting the database, you can check its status. If everything is running smoothly, you should see the MongoDB service operational.

coolify-server-configuration-mongodb-setup.jpg

At this point, the database is only accessible internally. If you want to make it publicly available, you'll need to select a public port (e.g., 5432) and check the box to make it publicly accessible. This will generate a public MongoDB URL that can be used to connect from external servers.

Testing the Database Connection

To test the connection, update your environment variables with the new remote URL. Start your development server using:

npm run dev

Once the server is running, navigate to localhost:3000/admin to access the admin panel where you can create your first user.


Using MongoDB Compass for Database Management

To manage your MongoDB database visually, you can use MongoDB Compass. For this, replace the local connection string with the remote URL in Compass. Keep in mind that you may receive a warning indicating that TLS/SSL is disabled, meaning the traffic between your server and the database is not encrypted. While this is a drawback compared to using MongoDB Atlas, it’s manageable for development purposes.

Deploying Your Project on Coolify

With your project and database ready, it’s time to deploy your Payload application on Coolify. Follow these steps:

  1. Make an initial commit to your GitHub repository.
  2. In Coolify, copy the internal connection URL for your MongoDB database for later use.
  3. Create a GitHub app to enable automatic deployments. Go to the sources section in Coolify and add a new source named GitHub.
  4. Authorize the app to access your repositories in the All About Payload organization.

After setting up the GitHub app, you can link your Payload project with the repository.

Configuring Health Checks and Environment Variables

Before finalizing the deployment, adjust the health check settings. The default path may return a 404 error, so ensure you set the health check path to /admin.

Lastly, add your environment variables. Since you can access the database using the local URL, paste it in the relevant environment variable field to enhance security. This way, you can restrict public access to the database while still allowing connections from your application.

Finalizing Your Deployment

After saving all configurations, initiate the deployment. It might take a few minutes, but once completed, you will have a fully operational Payload project with MongoDB hosted on Coolify.

coolify-payload-mongodb-deployment-configuration-screen.jpg


Accessing Your Application

Once the deployment is complete, you can access your application via the automatically generated subdomain under your wildcard domain. If you navigate to the admin panel, you should be able to log in and manage your application seamlessly.

Conclusion

Setting up a Payload NextJS project with MongoDB on Coolify is not only straightforward but also offers numerous benefits, including cost efficiency, data control, and enhanced GDPR compliance. By following the steps outlined in this guide, you can successfully self-host your applications while maintaining full control over your data.

If you have any questions or need further assistance, feel free to reach out in the comments section below or check out our resources at AllAboutPayload.

Frequently Asked Questions (FAQ)

1. What is Payload CMS?

Payload CMS is a headless content management system that allows developers to build flexible and scalable applications using modern technologies.

2. Why should I use MongoDB over Postgres?

MongoDB offers a NoSQL structure, which allows for more flexibility in data storage and schema management, reducing the risk of data loss during schema changes.

3. How much does it cost to self-host on Coolify?

Coolify offers competitive pricing, allowing you to run production workloads for as little as €7 per month, depending on your server configuration.

4. Is my data secure when self-hosting?

Self-hosting offers greater control over your data, and by using private internal URLs, you can enhance security by restricting public access to your database.

5. Can I scale my application easily with this setup?

Yes, with Coolify, you can manage multiple projects and scale your resources as needed without significant overhead.