Deploy a Next.js App
Next.js is a React framework for building full-stack web applications. It handles server-side rendering, static generation, API routes, and routing.
This guide covers how to deploy a Next.js app to Railway in four ways:
Create a Next.js App
Note: If you already have a Next.js app locally or on GitHub, you can skip this step and go straight to Deploy the Next.js App to Railway.
To create a new Next.js app, ensure that you have Node installed on your machine.
Run the following command in your terminal to create a new Next.js app:
Follow the prompts to configure your app. The defaults work well for most projects.
Run the Next.js App locally
cd into the directory and start the development server:
Open your browser and go to http://localhost:3000 to see your app.
Deploy the Next.js App to Railway
Railway offers multiple ways to deploy your Next.js app, depending on your setup and preference.
One-Click Deploy from a Template
If you're looking for the fastest way to get started, the one-click deploy option is ideal.
Click the button below to begin:
We highly recommend that you eject from the template after deployment to create a copy of the repo on your GitHub account.
Note: You can also choose from a variety of Next.js app templates created by the community.
Deploy from the CLI
-
Install the Railway CLI:
- Install the CLI and authenticate it using your Railway account.
-
Initialize a Railway Project:
- Run the command below in your Next.js app directory.
- Follow the prompts to name your project.
- After the project is created, click the provided link to view it in your browser.
- Run the command below in your Next.js app directory.
-
Configure for Standalone Output:
Next.js needs to produce a standalone build for self-hosted deployment. Add the
outputoption to yournext.config.ts(ornext.config.js) file:next.config.ts
Then update the start script in
package.jsonto serve the standalone server:package.json
Note: Railway uses Railpack (or Nixpacks) to build and deploy your code with zero configuration. The Node provider will pick up the start script in
package.jsonand use it to serve the app. -
Deploy the Application:
- Use the command below to deploy your app:
- This command will scan, compress and upload your app's files to Railway. You'll see real-time deployment logs in your terminal.
- Once the deployment completes, go to View logs to check if the service is running successfully.
- Use the command below to deploy your app:
-
Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
Deploy from a GitHub Repo
To deploy a Next.js app to Railway directly from GitHub, follow the steps below:
- Create a New Project on Railway:
- Go to Railway to create a new project.
- Configure for Standalone Output:
- Follow step 3 mentioned in the CLI guide to set
output: "standalone"and update the start script.
- Follow step 3 mentioned in the CLI guide to set
- Deploy from GitHub:
- Select Deploy from GitHub repo and choose your repository.
- If your Railway account isn't linked to GitHub yet, you'll be prompted to do so.
- Select Deploy from GitHub repo and choose your repository.
- Deploy the App:
- Click Deploy to start the deployment process.
- Once deployed, a Railway service will be created for your app, but it won't be publicly accessible by default.
- Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully.
- Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
Use a Dockerfile
-
Create a
Dockerfilein your Next.js app's root directory. -
Add the content below to the
Dockerfile:Note: This Dockerfile requires
output: "standalone"in yournext.config.tsas described in step 3 of the CLI guide. -
Either deploy via the CLI or from GitHub.
Railway automatically detects the Dockerfile, and uses it to build and deploy the app.
Note: Railway also supports deployment from public and private Docker images.
This guide covers the main deployment options on Railway. Choose the approach that suits your setup, and start deploying your Next.js apps seamlessly!
Next Steps
Explore these resources to learn how you can maximize your experience with Railway: