Upload existing Node.js (with Express js) app to CentOS 7 server

February 2, 2015 2.2k views

I have an existing Express.js project that I would like to upload and run on my CentOS 7 server. Does anyone know how to do that? I have completed all the CentOS setup steps, installed Node.js, Express.js, the project is on Git, I have my domain pointed to the Digital Ocean name servers, I just need to upload the app and tell it to run (forever).

1 Answer

As you are already maintaining your code in Git, using Git is probably the best to handle deployment as well. You can set up a repository on the server and a post-receive hook to restart the app or any other steps you need to run on the server. This way you can simply git push your app to the server. This article should get you pointed in the right direction:

This one will give you a bit more background on using Git hooks:

by Caio Vaccaro
This article will teach you how to use Git when you want to deploy your application. While there are many ways to use Git to deploy our application, we'll focus on the one that is most straightforward.
  • Thanks, those both look great! Though, one thing I don't understand is that each of those tutorials are creating git repositories. I already have one created and can push changes to it. Is there a way to connect that repository to my server or do I need to create a new one by following the steps in the tutorial?

  • @tech1002985 I'd create a second repository on your server. You can then add it as a "remote" in your existing repo. Then you'd use something like git push origin to push to your normal repo and git push production to push to your live site.

  • @asb Every time I try to commit something I just get a "nothing to commit" message. And when I try to push I get a connection refused message. I am not sure what I am missing. I have a local repository and have committed an entire application to it. I also have created a repo on my server. but cannot figure out how to get the all from my local repo to my servers repo.

  • @asb Do you have any info for me on the "nothing to commit" and "connection refused" thing? When I do a 'git push live master' from my local git directory it says "could not read from remote repository". When I added the remote directory I added it in my local git directory with 'ssh://user@mydomain.com/var/repo/site.git'. To clarify, what is the user@mydomain supposed to be? Is 'user' the user I am logged into my server with and mydomain the domain I want to publish to? Can you clarify on that?

  • @tech1002985 The user should be a user on the server with write access to the directory hosting the git repositories. mydomain.com could also be the ip address. It's generally good practice to set up a specific git user. It's simply the location of the server.

    I just ran through a basic setting up of a remote git repo. On the remote server, I ran:

    apt-get install git
    adduser git
    mkdir /var/repos/
    cd /var/repos/
    git init --bare my-project.git
    chown -R git:git /var/repos/

    Then locally, I ran:

    cd /path/to/my/repo
    git remote set-url remote-name git@my.ip.address:/var/repos/my-project.git
    git push remote-name
  • @asb Okay, I did that and tried a several different permutations of that and it still does not work. The exact error is:

    fatal: 'zhunt@' does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

    Does the user zhunt not have access rights to his own repository? If not, how do I set them. Isn't that what chown does? I ran that, it appeared to have worked - I didn't get any errors - but still nothing.

    What could I be missing?

  • @tech1002985 I just realized there was a typo in that!

    git remote set-url remote-name git@my.ip.address:/var/repos/my-project.git

    I've updated it above, but it was missing a colon between the ip address and the file path. Sorry!

  • @asb Still not working... It returned this:

    ssh: connect to host port 22: Connection refused
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

    Is the port incorrect?

    I've tried this: git remote set-url live zhunt@

    and this: git remote set-url live zhunt@

    and neither work. This is killing me!

Have another answer? Share your knowledge.