Skip to main content

How to deploy NodeJS app on server with Apache2 and acess it with Server IP addresss - Node JS deploy project Live.



    hello friens , This is Vasu Birla , in previous post
we have seen the deployement of NodeJS app on AWS instance server instance
but AWS server is expensive than other servers .
SO today we will use simple Ubuntu server for making Live out Project using Apache2 .


At the end you will be abe to access your NodeJS app using
server IP address from anywhere  ..



1.  Login to your server using SSH terminal .
(in AWS part i already explained how to do this )

    -> Open your putty in hostname put your server IP -
     login with ssh username ->root and password
    (if you dont have root username and pass ask your
     provider or reset it from cpanel or hosting panel )



2. after login - on terminal you can Put your project anywhere.
    There are two ways to put your project folder on server location

    (i) - > using Github - (very popular and easy to track your everyday code
changes to your project folder)

            you can push your entire project folder to git-hub and and then clone
            it using http link and you can clone your project to local computer or
             server and changes and again push and pull as per your requirements
   
    so push your code to git-hub and clone it your server root location ..


    (ii)  if your code is not on git-hub  then on your local machine use any FTP server
     like fileZilla or WinSCP open WinSCP and login with server IP (hostname)
      and password (or ..ppk or .pem file )

     once you connect your server using FTP you can directly upload your Project folder to
    Server location .  

3. after cloning your project open your folder on server using CD command
    run neccessery commands like npm install (for node-module dependencies  )

4. then run your script in my case it is npm run start  or
        in some cases most npm run dev app.js

5. if you are facing problem with database configure it  using nano command

    run ->  nano config.js  //database file
            and do changes as per server (for MongoDB paste mongo cloud cluster  link )
                for mysql you have to access mysql and import your database.sql file

6.  run mysql -u root -p  to enter in mysql

    then run Show Databases;  
        if your database is not there then create it

            -> CREATE DATABASE your_database_name;

            -> USE your_database_name;  
           
            -> SHOW TABLES;

            ->  USE your_database_name;

            -> SOURCE /root/nodeProject/database.sql (if your folder is at root
                     location you have to use /root/)

        now you have successfully imported your mysql database tables
        you can check it by running command SHOW TABLES;

7.  now for reverse proxy (inwhich your project run on localhost(127.0.0.1) but
    when you access it using Sever IP publicly you can acess it )  
        We will use apache2 for it aleternative option is (nginx server )

        for appche2 configurtion run following commands

      ->   sudo apt update

      -> sudo apt install apache2

      -> sudo a2enmod proxy
   
        ->  sudo a2enmod proxy_http

      ->  sudo nano /etc/apache2/sites-available/kilvish.conf
            # here kilvish.conf is your appname and it can be anything.
   

    #paste following code in kilvish.conf or in your app.conf

<VirtualHost *:80>
ServerName 195.35.23.27 DocumentRoot /homemapp # your server's main projectFolder path ..
ProxyPreserveHost On ProxyPass / http://127.0.0.1:3000/ #use your port my is 3000 ProxyPassReverse / http://127.0.0.1:3000/ </VirtualHost>




    -> sudo ufw status

    -> sudo ufw allow 80

    -> sudo a2ensite your-app-name.conf
   
    -> sudo service apache2 reload





    #now appache2 configuration is done to see details  run
    -> sudo apache2ctl -S


//run following command if you want another projects on different port

sudo ufw allow 3006 // user your project port sudo ufw reload


-------------------------
Now it is completed on your terminal you can run your node app
npm run start
or
npm run dev app.js  or whatever your script is  

after successfully started

go your browser and acess it your ServerIP ..  https://0.0.0.0/  (user your IP )
and you are able to see your project later you can use domain name in  .conf file above

-----------------------------------------------------
make terminal Forever ALive ...

to run your project forever evenif you close terminal you have to install PM2 pkg

go to your project folder -  and run

    -> npm install pm2 -g

    -> pm2 start app.js --name "kilvishAPP"

    # now it is started  to see it  run

    -> pm2 ls

    # to Stop it  
    -. pm2 stop 0  or  pm2 stop appName  

    #to restart it
    -> pm2 restart 0  or pm2 restart appname  

   # if in anycase your server restart - you can set PM2 on startup by
   # using following command (for server username root )

 -> sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u root --hp /root
   
        # if any doubt you can contact me on linkedin

    https://in.linkedin.com/in/vasubirla

    ------------------------------------------- completed ---




Comments

Popular posts from this blog

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  ...

GitHub Repo Collaboration Work on single project

 =============================================== To collaborate effectively with your friend on the same project, you should use Git branches to manage different lines of development. Here's a step-by-step procedure you can follow to streamline collaboration: 1. Create Separate Branches for Each Developer Create a New Branch for Your Friend: On your local repository, create a new branch for your friend. For example if your friend name is kilvish , if you want to create a branch named kilvish , you would run: make sure you would be on main branch already   command->  git checkout -b kilvish git push origin kilvish 2. Set Up Your Friend’s Environment( On your Friend's System )  at kilvish side  run ->  Clone the Repository (if not already done): If your friend hasn’t cloned the repository yet, they should do so: command ->  git clone https://github.com/Vasu-Birla/your-repo.git   // your your main clone line  Fetch All Bran...