简体   繁体   中英

How to deploy NextJS application to Linux Server (CentOS 7) - VPS

I've got a question regarding building applications. I'm using simple VPS with node.js support. Now I do not know how to build my next.js application to production.

I want to deploy my application as static files.

I thought that I should use next build && next export then copy out dir to the server but during this process, I faced some issues - when I change route - everything is okay, but if I refresh the page - the page is not found because the server is looking for this file in directories. So how can I deploy my nextjs application in production mode with VPS server and static files?

I tried one thing which is not working fine probably or I did something wrong. I added nodejs express server with

const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';

const app = next({dev});
const router = express.Router();
const handle = app.getRequestHandler();

app.prepare()
    .then(() => {

        const server = express();

        server.get('*', (req, res) => {
            return handle(req, res);
        });

        server.listen(3000, (err) => {
            if (err) throw err;
            console.log('> Ready on http://localhost:3000');
        });
    });



and start server with forever library NODE_ENV=production node server.js and it's working fine, but seems this is working in a wrong way - seems it's normal server like in dev mode - so it shouldn't be like that. (I see thunder icon on the right-bottom corner and I see all files which are same as in dev mode).

I want to deploy everything as static files.

Thank you for your help!

After you build and export you need to serve those files somehow. The reason the Express server works is because you are starting a HTTP server to serve the files.

So you need to serve those files either by using a static hosting provider (ie Vercel or Amazon S3). Otherwise you should start a server on your linux machine using something like serve to serve it at a port, similar to your Express server serving it as localhost:3000 which is then exposed on your VPS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM