How to deploy a Web game with Node.js backend to a CentOS server

Cayman Bruce
Aug 26, 2017 · 5 min read

I have built a multiplayer web game with Node.js as its backend. The next step is to put it on the internet so people on the other side of the world can play it.

So I searched the internet and found a few tools for easy deployment. I will be using PM2 and Nginx to deploy my game on a VPS.

Prerequisites for deploying my Web game:

Get Nginx installed on CentOS VPS machine and configure Nginx to be accessed from the internet.Install npm and yarn.Install pm2 deployment tool.

Installing Nginx:

I have found installing Nginx From Nginx Repository is very easy, compared to installing it from source. I just followed the instructions on this page https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/. At the Install From NGINX Repository section it tells us how to install Nginx from its repository.

Installing Node.js and npm:

Follow the guides on the above link to install Node and npm. Change node to the latest version before yum install .

Installing Yarn:

I find it is easier to install yarn from npm using this command:

npm install --global yarn

After we install yarn, we can use yarn to install pm2 with this command:

yarn global add pm2

The above line will add pm2 as a global package so it will not add anything to my project foler.

Now I have all the tools ready. Let’s test if Nginx works correctly.

curl localhost

if there is no response it means the firewall on your Linux system is blocking http accesses.

Here is an article suggesting a few things we need to do when configuring a new CentOS server:

But we can simply use this line to enable it:

firewall-cmd --zone=public --permanent --add-service=http

Now we are able to access Nginx web server. There are a lot of tutorials about how to configure it as a reverse proxy so I am not going to expand it here.

We usually put the configure file into the

/etc/nginx/sites-enabled/

directory. And then we do a symlink to put it into

/etc/nginx/sites-available/

We will need to include all configure files in /etc/nginx/nginx.conf.

nano /etc/nginx/nginx.conf

at the end of the server block, we will use

include /etc/nginx/sites-available/*.conf

Deploying project using pm2:

Before deploying my project to pm2, I need to build my server side to a sub directory of my project while maintaining its file structure. How to move the files to the VPS?

Usually this is done by committing the code to GitHub and then use some automatic scripts to pull the code to the remote server. But I decide to do the files copy manually because I don’t need to update my files very often. This way I will only keep two copies of my files. One on the server and one on my local computer. I just use simple “scp” command to get the job done.

The “scp” command is extremely easy to use. To copy a directory to the remote server’s /srv/site:

scp -r subfolder username@ip-address:/srv/site

Just replace username and “ip-address” with the one on the VPS.

Here is the command to copy multiple files to the remote server:

scp index.html main.js username@ip-address:/var/www/site/public_html

If we have configured out Nginx server to point to /var/www/site/public_html we should be able to see the content in “index.html”. We can check that using:

curl 127.0.0.1

But almost in most cases, we are still unable to access our site even if we have configured Nginx correctly to point to the root folder where the “index.html” file is in. We may get 403 error which is very common in deploying our codes for the first time. By checking the logs of the Nginx server, it is not hard to figure out this error comes from forbidden access of the files. (It will say permission denied in the log)

This is also easy to fix. We need to change the folder to be owned by the Nginx server user, and the folder’s privileges.

chown -R nginx:nginx /var/www/site/public_html

and

chmod 755 /var/www/site/public_html

Usually this will remove the 403 error. If this still doesn’t work, we can check if “index.html” exists or has the correct filer name.

Let’s do a “curl localhost” again. If this shows the content of the “index.html” file, we can proceed to the next step.

Now we have got the client side running correctly. But when we try to play we will get error saying the websocket is closed. We haven’t started the server side yet.

So how do we run our game’s server side on the remote server? It’s easy with pm2. First we go to the server folder we just copied to the remote server. We will need to do a “yarn install” to install all the necessary modules. After that, we create a ecosystem config file in the folder. We do it by using this command:

pm2 ecosystem

This command will create a ecosystem.config.js file in our project folder. We need to modify it to our need. The most important thing to change is the PORT and NODE_ENV environmental variables.

module.exports = { apps : [     {
name : ‘game’,
script : ‘server.js’,
env: {
COMMON_VARIABLE: ‘true’
},
env_production : {
PORT: ‘8999’,
NODE_ENV: ‘production’
}
},
],
}

Node that we have changed the “NODE_ENV” variable to production.

Then a simple command will start our server side:

pm2 start ecosystem.config.js --env production

This will create an app name “game” in pm2’s processes list.

Check the status of the server by using:

pm2 list

and

pm2 logs game

If there is any error showing in the log we can look into our server side code.

But that’s pretty much it. Restart our Nginx server:

service nginx restart

will do.

After all these steps my game is finally up and running. What a beautiful day!

UPDATE:

sometimes the port 80 is blocked on VPS for unknown reason. We can use this command to reopen it:

sudo /sbin/iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT

Then we need to save the rules by using

service iptables save
)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade