Articles on: Dedicated Servers / VPS

How to run a Discord.js bot on a VPS

How to run a Discord.js bot on a VPS (Linux)



Connect to your VPS via SSH

From this list, select the Node.js version that you would like to run the first 2 commands that are in their respective versions.

For Example, if you want to install Node.js v16. You would run the first two commands in the Terminal.

Note: Depending on what OS you are running, the commands may be different! In this case, we will be using Ubuntu!



Run the commands apt install npm -y and npm i pm2 -g. To install npm for installing node packages and pm2 to run the bots 24/7.



After installing the packages, run the following commands:
pm2 flush
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:rotateInterval '0 */1 * * *'.

Running these commands will make it so that the pm2 logs stop creating very large log files in the long term.

Running the Discord.JS bot(s) using PM2



Connect to your VPS via SFTP. In this case, we will be using FileZilla



Then using SSH, use the “ls” command to see the content of the folders that you are currently on, and use the cd <Folder/Directory> command to navigate through the folders. Also, to go up a folder, run the command cd ../.

For Example: If you want to get from the “root” folder to “Discord Bots > DjsBot”, just run the command cd ‘Discord Bots’/DjsBot.



Now to start the bot, run the command pm2 start <Startup Bot File> or pm2 start <Startup Bot File> --name=<Name of the Process> to give the process a name.



How to Restart the Bot using PM2



To restart the bot you, need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing pm2 ls and find the id corresponding to the process name



Run the command pm2 restart <Process ID or Name> and your bot will restart.



How to Stop the Bot using PM2



To stop the bot, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing pm2 ls and find the id corresponding to the process name



Run the command pm2 stop <Process ID or Name> and your bot will stop.



How to see the logs of your bot using PM2



To view the logs, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing pm2 ls and find the id corresponding to the process name



Run the command pm2 logs <Process ID or Name> or pm2 logs <Process ID or Name> --lines=<Line Amount> and pm2 will show the bot’s console logs.




If you require any further assistance, please create a ticket here.


Created By: Alecz R.

Updated on: 29/02/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!