--- title: Deploy LobeChat with database on Vercel description: >- Learn how to deploy LobeChat with database on Vercel with ease, including: database, authentication and S3 storage service. tags: - Deploy LobeChat - Vercel Deployment - OpenAI API Key - Custom Domain Binding --- # Deploying Server Database Version on Vercel This article will detail how to deploy the server database version of LobeChat on Vercel, including: 1) database configuration; 2) identity authentication service configuration; 3) steps for setting up the S3 storage service. Before proceeding, please make sure of the following: - Export all data, as after deploying the server-side database, existing user data cannot be automatically migrated and can only be manually imported after backup! - The `ACCESS_CODE` in the environment variables is either unset or cleared! - When configuring the environment variables required for the server-side database, make sure to fill in all of them before deployment, otherwise you may encounter database migration issues! ## 1. Configure the Database ### Prepare the Server Database Instance and Obtain the Connection URL Before deployment, make sure you have prepared a Postgres database instance. You can choose one of the following methods: - `A.` Use Serverless Postgres instances like Vercel / Neon; - `B.` Use self-deployed Postgres instances like Docker. The configuration for both methods is slightly different, and will be distinguished in the next step. ### Add Environment Variables in Vercel In Vercel's deployment environment variables, add `DATABASE_URL` and other environment variables, and fill in the Postgres database connection URL prepared in the previous step. The typical format for the database connection URL is `postgres://username:password@host:port/database`. Please confirm the `Postgres` type provided by your vendor. If it is `Node Postgres`, switch to the `Node Postgres` Tab. Variables to be filled for Serverless Postgres are as follows: ```shell # Serverless Postgres DB Url DATABASE_URL= # Specify service mode as server, otherwise it will not enter the server-side database NEXT_PUBLIC_SERVICE_MODE=server ``` An example of filling in Vercel is as follows: {'Add Variables to be filled for Node Postgres are as follows: ```shell # Node Postgres DB Url DATABASE_URL= # Specify Postgres database driver as node DATABASE_DRIVER=node # Specify service mode as server, otherwise it will not enter the server-side database NEXT_PUBLIC_SERVICE_MODE=server ``` An example of filling in Vercel is as follows: {'Add If you wish to enable SSL when connecting to the database, please refer to the [link](https://stackoverflow.com/questions/14021998/using-psql-to-connect-to-postgresql-in-ssl-mode) for setup instructions. ### Add the `KEY_VAULTS_SECRET` Environment Variable After adding the `DATABASE_URL` environment variable for the database, you need to add a `KEY_VAULTS_SECRET` environment variable. This variable is used to encrypt sensitive information such as apikeys stored by users. You can generate a random 32-character string as the key using `openssl rand -base64 32`. ```shell KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk= ``` Make sure to add this to the Vercel environment variables as well. ### Add the `APP_URL` Environment Variable Finally, you need to add the `APP_URL` environment variable, which specifies the URL address of the LobeChat application. ## 2. Configure Authentication Service The server-side database needs to be paired with a user authentication service to function properly. Therefore, the corresponding authentication service needs to be configured. ### Prepare Clerk Authentication Service Go to [Clerk](https://clerk.com?utm_source=lobehub\&utm_medium=docs) to register and create an application to obtain the corresponding Public Key and Secret Key. If you are not familiar with Clerk, you can refer to [Authentication Service-Clerk](/en/docs/self-hosting/advanced/authentication#clerk) for details on using Clerk. ### Add Public and Private Key Environment Variables in Vercel In Vercel's deployment environment variables, add the `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` and `CLERK_SECRET_KEY` environment variables. You can click on "API Keys" in the menu, then copy the corresponding values and paste them into Vercel's environment variables. {'Find The environment variables required for this step are as follows: ```shell NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_xxxxxxxxxxx CLERK_SECRET_KEY=sk_live_xxxxxxxxxxxxxxxxxxxxxx ``` Add the above variables to Vercel: {'Add ### Create and Configure Webhook in Clerk Since we let Clerk fully handle user authentication and management, we need Clerk to notify our application and store data in the database when there are changes in the user's lifecycle (create, update, delete). We achieve this requirement through the Webhook provided by Clerk. We need to add an endpoint in Clerk's Webhooks to inform Clerk to send notifications to this endpoint when a user's information changes. {'Add Fill in the endpoint with the URL of your Vercel project, such as `https://your-project.vercel.app/api/webhooks/clerk`. Then, subscribe to events by checking the three user events (`user.created`, `user.deleted`, `user.updated`), and click create. The `https://` in the URL is essential to maintain the integrity of the URL. {'Configure ### Add Webhook Secret to Vercel Environment Variables After creation, you can find the secret of this Webhook in the bottom right corner: {'View The environment variable corresponding to this secret is `CLERK_WEBHOOK_SECRET`: ```shell CLERK_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxxxxxxxx ``` Add it to Vercel's environment variables: {'Add By completing these steps, you have successfully configured the Clerk authentication service. Next, we will configure the S3 storage service. ## 3. Configure S3 Storage Service In the server-side database, we need to configure the S3 storage service to store files. In this article, S3 refers to a compatible S3 storage solution, which supports object storage systems that comply with the Amazon S3 API. Common examples include Cloudflare R2, Alibaba Cloud OSS, etc., all of which support S3-compatible APIs. ### Configure and Obtain S3 Bucket You need to go to your S3 service provider (such as AWS S3, Cloudflare R2, etc.) and create a new storage bucket. The following steps will use Cloudflare R2 as an example to explain the creation process. The interface of Cloudflare R2 is shown below: {'Cloudflare When creating a storage bucket, specify its name and then click create. {'Create ### Obtain Environment Variables for the Bucket In the settings of the R2 storage bucket, you can view the bucket configuration information: {'View The corresponding environment variables are: ```shell # Storage bucket name S3_BUCKET=lobechat # Storage bucket request endpoint (note that the path in this link includes the bucket name, which must be removed, or use the link provided on the S3 API token application page) S3_ENDPOINT=https://0b33a03b5c993fd2f453379dc36558e5.r2.cloudflarestorage.com # Public access domain for the storage bucket S3_PUBLIC_DOMAIN=https://s3-for-lobechat.your-domain.com ``` `S3_ENDPOINT` must have its path removed, otherwise uploaded files will not be accessible ### Obtain S3 Key Environment Variables You need to obtain the access key for S3 so that the LobeChat server has permission to access the S3 storage service. In R2, you can configure the access key in the account details: {'View Click the button in the upper right corner to create an API token and enter the create API Token page. {'Create Since our server-side database needs to read and write to the S3 storage service, the permission needs to be set to `Object Read and Write`, then click create. {'Configure After creation, you can see the corresponding S3 API token. {'Copy The corresponding environment variables are: ```shell S3_ACCESS_KEY_ID=9998d6757e276cf9f1edbd325b7083a6 S3_SECRET_ACCESS_KEY=55af75d8eb6b99f189f6a35f855336ea62cd9c4751a5cf4337c53c1d3f497ac2 ``` ### Adding Corresponding Environment Variables in Vercel The steps to obtain the required environment variables may vary for different S3 service providers, but the obtained environment variables should be consistent: The `https://` in the URL is essential and must be maintained for the completeness of the URL. ```shell # S3 Keys S3_ACCESS_KEY_ID=9998d6757e276cf9f1edbd325b7083a6 S3_SECRET_ACCESS_KEY=55af75d8eb6b99f189f6a35f855336ea62cd9c4751a5cf4337c53c1d3f497ac2 # Bucket name S3_BUCKET=lobechat # Bucket request endpoint S3_ENDPOINT=https://0b33a03b5c993fd2f453379dc36558e5.r2.cloudflarestorage.com # Public domain for bucket access S3_PUBLIC_DOMAIN=https://s3-dev.your-domain.com # Bucket region, such as us-west-1, generally not required, but some providers may need to configure # S3_REGION=us-west-1 ``` Then, insert the above environment variables into Vercel's environment variables: {'Adding ### Configuring Cross-Origin Resource Sharing (CORS) Since S3 storage services are often on a separate domain, cross-origin access needs to be configured. In R2, you can find the CORS configuration in the bucket settings: {'Cloudflare Add a CORS rule to allow requests from your domain (in this case, `https://your-project.vercel.app`): {'Configuring Example configuration: ```json [ { "AllowedOrigins": ["https://your-project.vercel.app"], "AllowedMethods": ["GET", "PUT", "HEAD", "POST", "DELETE"], "AllowedHeaders": ["*"] } ] ``` After configuring, click save. ## Four, Deployment and Verification After completing the steps above, the configuration of the server-side database should be done. Next, we can deploy LobeChat to Vercel and then visit your Vercel link to verify if the server-side database is working correctly. ### Redeploy the latest commit After configuring the environment variables, you need to redeploy the latest commit and wait for the deployment to complete. {'Redeploy ### Check if the features are working properly If you click on the login button in the top left corner and the login popup appears normally, then you have successfully configured it. Enjoy using it\~ {'User {'Login ## Appendix ### Overview of Server-side Database Environment Variables For easy copying, here is a summary of the environment variables required to configure the server-side database: ```shell APP_URL=https://your-project.com # Specify the service mode as server NEXT_PUBLIC_SERVICE_MODE=server # Postgres database URL DATABASE_URL= KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk= # Clerk related configurations NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_xxxxxxxxxxx CLERK_SECRET_KEY=sk_live_xxxxxxxxxxxxxxxxxxxxxx CLERK_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxxxxxxxx # S3 related configurations # S3 keys S3_ACCESS_KEY_ID=9998d6757e276cf9f1edbd325b7083a6 S3_SECRET_ACCESS_KEY=55af75d8eb6b99f189f6a35f855336ea62cd9c4751a5cf4337c53c1d3f497ac2 # Bucket name S3_BUCKET=lobechat # Bucket request endpoint S3_ENDPOINT=https://0b33a03b5c993fd2f453379dc36558e5.r2.cloudflarestorage.com # Public access domain for the bucket S3_PUBLIC_DOMAIN=https://s3-for-lobechat.your-domain.com # Bucket region, such as us-west-1, generally not needed to add, but some service providers may require configuration # S3_REGION=us-west-1 ```