If you are starting out with web development or if this is the first time it should be a little confusing to get better with nginx,
That's why I am here for you guys, you could ask me anything and I will surely help you with it and this is one of the requested ones.
If you are using nginx in a server or mac os or linux based system here's what you should do.
sudo systemctl status nginx
If that doesn't support it might be because your linux is a little lower version, so try out this one.
sudo systemd status nginx
This should fix your problem if you are using linux or macOs.
In the case that you are using it in windows, There is only one way to make sure that nginx is running in the background, Open any browser that you have and type localhost
If you want to setup the whole nginx then here is how you should do it.
Installing Nginx
Let me help you from the beginning of installing nginx to deploying your node app.
If you are using ubuntu or any debian based operating system.
It is really simple to install.
sudo apt-get update && sudo apt-get install nginx -y
This should update your repositories and install nginx and run it in the background and you can check it by going in to the chrome and type the url of the server or raspberry pi if you use like me.
It should show something like welcome to nginx.
Running nginx in the background
So if you have just installed now, then this step is unnecessary but if you have already installed nginx and you have either rebooted your device or you have switched off the device accidentally.
Then you have to start the nginx in order for it to work on the background.
It is also simple to do.
sudo systemctl start nginx
If you are using really way older version of linux then use this command.
sudo systemd start nginx
Now that's out of our way.
You have to also check whether it is running then check that the device ip is showing welcome to nginx.
Or you can run this command.
sudo systemctl status nginx
It should show active just like in this image.
Cloning Our Node App
So before we continue you have to push all your code to nginx and it is really simple to do.
If you have never used git and github before in your like,
Then watch this video for learning about git
youtube.com/watch?v=USjZcfj8yxE
Now after watching this video you have to watch this video about github
youtube.com/watch?v=nhNq2kIvi9s
If you saw both these videos then great we can continue.
You should have already pushed your code to github now you have to clone it in your server.
git clone
Now that you have cloned your repo you have to install node and also install pm2.
curl -sL raw.githubusercontent.com/nvm-sh/nvm/v0.35... -o install_nvm.sh && bash install_nvm.sh && source ~/.bash_profile && nvm install --lts
This will install node and npm in your server.
Now you have to install pm2
npm install pm2 --globally
Now cd into your cloned repository and type this command out.
pm2 start --name "node-app"
Example like
pm2 start app.js --name "node-app"
This should start the node app in the background and keep it running.
If you want to stop then use this command
pm2 stop node-app
If you want to restart then type
pm2 stop node-app
If you want to see the console logs of your app type this.
pm2 logs node-app
And Finally type
pm2 save
Configuring Nginx Configuration File
This is step differs for everyone and it depends on the software that you are trying to deploy.
If you don't know how to use nginx at all then follow my series on nginx in this blog. Click Here.
Right now I am going to enable reverse pass to port 3000 and reload the nginx.
Type this command for editting the file
sudo nano /etc/nginx/nginx.conf
Delete everything and then we have to copy paste the below code for reverse proxying it.
Instead of 3000 write any port number that you have used like, port 5000 or something.
events { worker_process auto; } http { server { listen 80; location / { proxy_pass localhost:3000; } } }
If you don't understand this at all then go through the series. I have made a lot of posts on nginx. Click Here.
That's all for the nginx setup.
Conclusion
I hope you have understood how you should view the nginx status and also complete setting up of nginx,
If you are new to this blog, I am Uzham (Full Stack Dev / Mobile App Dev) It would be lovely if you join our discord server.
By the way, I am almost completed with the app that I challenge to do in a week I will share it soon about it guys
Most probably in the next post.
Till then Happy Hacking guys