Skip to content

Instantly share code, notes, and snippets.

@wmantly
Last active December 18, 2020 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmantly/f762d673d60612191377cfc5d9b01e27 to your computer and use it in GitHub Desktop.
Save wmantly/f762d673d60612191377cfc5d9b01e27 to your computer and use it in GitHub Desktop.

Seting up Nodejs with Apache on an Ubuntu server

Packages

As always, make sure the server is up to date before we start.

apt update
apt upgrade

Install NodeJS from the default package manager repos. The version may be old, If you need a differnt version check out this link https://github.com/nodesource/distributions

Get your project

Use git to clone your project to /var/www and install any dependance with npm install

Before moving on, make sure you have set everything up for your project and have tested it.

Setting your node app as a service

We will create a system d service to run the node app and keep it running.

[Unit]
Description=web proxy service
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/bin/env node /var/www/proxy/nodejs/bin/www

[Install]
WantedBy=multi-user.target

Edit the Description and ExecStart path to match the current NodeJS app.

Save it to /etc/systemd/system/{{name}}.service. Replace "name" with what you want to name the service. Use only lowercase letters, numbers and dases

Start the service and make sure you can acess it and it works.

systemctl start {{nam}}.service

You can see the stats of a service by running

service {{name}} status

Once you are sure the service is starting the app correctly, enable it(make it start on boot).

systemctl enable proxy.service

Intergrate with apahce

    # socket.io conf
    RewriteEngine On
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
    RewriteCond %{REQUEST_URI}  ^/socket.io/$1/websocket  [NC]
    RewriteRule socket.io/(.*)           ws://localhost:3000/socket.io/$1 [P,L]
    ProxyPass /socket.io http://localhost:3000/socket.io
    ProxyPassReverse /socket.io http://localhost:3000/socket.io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment