- [Instructor] In this video, I will deploy our application
to a server using PM2.
PM2 is a very powerful process manager
that also comes with a feature
that lets us deploy a project.
This is a good option for smaller projects.
In larger projects you might have a whole
deployment pipeline that takes care of that.
As server, I'm using an EC2 instance,
but any host on the internet will do.
On this host, I installed Node.js and PM2
and I created the user Node.js
and a directory on Node.js deploy.
I also added the public SSH key
of my development machine to the list of authorized keys.
PM2 tool relies on Git to deploy project from.
For that I pushed it to a GitHub repository.
Additionally, I copied my .env file
into the deploy directory because it's not published
to GitHub for obvious security reasons.
On MongoDB Atlas, I also white listed the IP of my server
so that it can then connect to the MongoDB cluster.
To setup the deployment on all development machine,
we first have to install PM2.
I want to install it globally, so I run
sudo npm install -g pm2.
This asks me for my user password,
and then it will install PM2 globally
on my development machine.
As mentioned, PM2 can handle deployments for us.
For that, it needs
a configuration file called ecosystem config js
that describes this whole deployment.
I have created this configuration file for you already.
You can find it in the exercise files.
It's in the project root.
Let's open it and let's go through it real quick.
First, the file describes which apps need to be deployed,
and we want to deploy only one app, and its name meetup.
We define the start script, and also very important
we define that when we push into production,
NODE_ENV should be set to production.
Starting with line 11, we now describe
the actual deployment.
We define the SSH user to be used,
the host to connect to,
the Git ref, and also the repo to deploy from.
Then we define the deployment path on the server,
and then we have a few post-deploy commands
like copying over the env file
into the current directory, doing npm install,
and then starting the application.
To setup the server for the first deployment,
we have to now run pm2 deploy production setup.
This will create all the directories
and structures on the host.
And next we want to run pm2 deploy production.
And this will now deploy the application to the host.
Let's look into the host real quick.
So I'm in my deploy directory and there.
And we now see that pm2 created a few directories
with a directory current, which points
to the currently running application.
PM2 keeps also copies of all deployment,
so that we can roll back if anything goes wrong.
Now that the application was deployed,
let's see if it's running.
So I'm running pm2 status, and I see that everything
is running as expected.
And now I'm running pm2 save to preserve this setup.
And now I also want that this application starts
whenever the server started, so I'm running pm2 startup.
And this gives us a command to execute
and this command will add our application
to the startup scripts.
Now of course we're curious if we can access the website,
so in my browser opening http
and that's my daemon server URL,
and I'm connecting to port 3000.
On EC2 I have for that open to port 3000 on the firewall.
And we see we have now a website running on the internet.