Deploy Nuxt App in Ubuntu Server
For this, first you need to have a running Ubuntu server and a Nuxt app to build.
Build the Nuxt App
Navigate to your project directory and build the app using the following command
bash
npm run buildbash
┌ Building Nuxt for production...
│
● Nuxt 4.4.2 (with Nitro 2.13.3, Vite 7.3.1 and Vue 3.5.32)
✔ Processed 2 collections and 0 files in 69.58ms (0 cached, 0 parsed) @nuxt/content 2:46:56 PM
ℹ Nuxt Icon server bundle mode is set to local 2:46:57 PM
│
● Nitro preset: node-server
ℹ Building client... The build folder will be .output. If you look into the folder, you can see a lot of build files. Inside the server folder, you can see the node_modules as well. This is when you build the app. Nuxt bundles everything your application needs into a standalone package, so you don't have to run
npm install it on your server.Transferring those files to the remote server will take much time, so we need zip them. Use the following command to zip the folder
bash
tar -czf build.tar.gz .outputNow, transfer the file to our Ubuntu server.
bash
scp build.tar.gz user@your-server-ip:~Now SSH into the remote Ubuntu server
bash
ssh user@your-server-ipProvide the password if required. Here, user is your Ubuntu server username that might be root or ubuntu
Inside the Ubuntu server, untar the file
bash
tar -xzf build.tar.gzInstall NodeJS on the Server
We need to install the node inside the server. We are using nvm node manager to do so, it's due to get rid of version conflict between the node used to develop locally and the server. First, install the nvm
bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bashNow install NodeJS
bash
nvm install v22.17.1Make sure to use your own node version.
Verify the node is installed
bash
node -vInstall and Set Up Process Manager(PM2)
Install PM2
We are going to manage the process using pm2, which helps to run the application in the background.
bash
npm install -g pm2Let's create a configuration file for pm2 to manage. Create the file in the same directory where our .output folder is located.
bash
touch ecosystem.config.cjsSet up the config file
javascript
module.exports = {
apps: [
{
name: 'csbyte',
port: '3000',
exec_mode: 'cluster',
instances: 'max',
script: './.output/server/index.mjs'
}
]
}Here, use your own name. You can see the instances are set to max, which will create multiple instances as the user requests increase. When you use Cluster Mode, PM2 acts as a load balancer. Instead of running one single process, it will utilize all available CPU cores on your Ubuntu server.
If your server have 2 core cpu, setting instances max will create one instance per core, resulting in 2 instances.
Start pm2
Simply, start the pm2 using the following command
bash
pm2 start ecosystem.config.cjs
The output looks something like the above, for instance, set to 1.
Now you can open the IP address with port 3000 it will load the app.
http://your-server-ip-address:3000/Run Node APP in the background
Run the following command to run your node app in the background
bash
pm2 save
pm2 startupUseful command for PM2
bash
pm2 delete app-nameThis will delete the app use app-name as your configured name in ecosystem.config.cjs
bash
pm2 logsThis will list the logs of all the running processes if you have multiple nuxt app setup.
bash
pm2 logs app-nameThis will log for a specific Nuxt app.
bash
pm2 logs app-name --lines 100This will show the older logs, the last 100 lines
bash
pm2 monitThis will show a more visual way to monitor logs alongside CPU and RAM usage
If your logs are getting too cluttered and you want to wipe them to start fresh
bash
pm2 flushAll these logs are located inside these locations.
Output console logs:
bash
~/.pm2/logs/app-name-out.logErrors logs:
bash
~/.pm2/logs/app-name-error.logThis is the process to deploy our Nuxt application.
