Fastest way to deploy your web app on your VPS ubuntu or amazon

A server with Ubuntu Linux ( this guide assumes 18.04 but should work for other versions a well) SSH access to the server with sudo permissions

· 7 min read
Fastest way to deploy your web app on your VPS ubuntu or amazon
Dumup Community
dumup, solving problems and help your productivity by answering each other’s answers

What you will need:

  • A server with Ubuntu Linux ( this guide assumes 18.04 but should work for other versions a well)
  • SSH access to the server with sudo permissions
  • A lucky app with an awesome name. Replace <yourapp> in the following instructions with that name.
  • A domain name. While lucky apps can be deployed in a “subdirectory” of a domain, they work best when installed at the root of the domain. Replace <yourdomain> in the following instructions with that domain name.

#Install required dependencies

How to create an SSH-Key (mac/linux/windows with git-bash)

  • cd into the hidden ssh folder cd ~/.ssh
  • run the ssh-key generator ssh-keygen
  • accept all the defaults (only don't use the defaults if your creating a second key or know what you are doing)
  • this should create two files
  • id_rsa this is your private key, this should only be on your computer and never shared.
  • id_rsa.pub this is your public key, this is what your share with servers you want to SSH into (like github or your virtual server)
  • copy the text of your public key by printing cat id_rsa.pub then copying and pasting into the service you are generating your server with

How to Login to your server

  • in terminal to SSH into your server use the following command ssh username@ipaddress
  • So if my username was alex and ip address 111.111.111.1 I'd use command ssh [email protected]
  • The server will then present the public key you provided it and if the matching private key is on your computer the login will be successful

If you are logging in as the root user, you should create a different user to avoid security issue that can arise from deploying your app with root privileges.

creating a non-root user

  • create a new user adduser testuser
  • add user to sudo group usermod -aG sudo testuser
  • switch to that user su - testuser

pointing a domain to your server

  • In the DNS records of your domain, create an "a" record the points to the ip address of your virtual private server (VPS)
  • The host part of the DNS record specifies a subdomain that is being pointed.

So if I wanted to point subdomain.domain.com to my server running at 111.111.111.11 I'd create...

  • DNS record type: a, host: subdomain, points to: 111.111.111.11

Step 2 - Setting Up The Server

  • Installing any software your app needs to run (language, like python or ruby).
  • git clone the repo with your app to the server
  • run your app development server

For example, if I was deploying a python flask app. I would:

  • Install python3
  • create an SSH key on my server then add it to my github account
  • clone the repo of my app in the home folder
  • install dependencies as I normally would after cloning
  • run the devserver and test it out in my browser

So if the app runs the dev server on port 5000 I'd then put http://111.111.111.11:5000 in my browser, make sure to use http as we have not yet configured https.

NOTE by default the dev server on most langauge's web frameworks default to hosting on 127.0.0.1 or "localhost" which is not publically accessible, you need to change the host 0.0.0.0, read your frameworks documentation on how to change the host.

If you see the right output then the app is running on your server correctly. Next we need to...

  • create a service for our app
  • use nginx to point requests to our subdomain to that service
  • use letsencrypt to add SSL to our site.

Step 3 - Create a Service for Our App

Essentially a service allows use to define a process that runs our app that doesn't run neccessarily on a IP port. Instead our Nginx Web Server will forward requests to that service.

create a fill in the following location, the name of the service file is the name of the service. To create the file use the command sudo nano /etc/systemd/system/app.service

The contents should be, use the comments to know what the contents need to be...

[Unit]
#  specifies metadata and dependencies
Description=Service to run my app
After=network.target

# tells the init system to only start this after the networking target has been reached
# We will give our regular user account ownership of the process since it owns all of the relevant files
[Service]

# Service specify the user and group under which our process will run.
User=testuser

# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.
Group=www-data

# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where to run the command>
WorkingDirectory=/home/testuser/mygithubrepo

# Define inline ENV VARIABLES here
Environment="ENVIRONMENT=PRODUCTION"
Envrionment="DATABASE_URL=postgres://..."

# We'll then specify the commanded to start the service
ExecStart=node server.js

# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular m>

[Install]
WantedBy=multi-user.target
  • in nano to quit and save first hit ctrl+x then hit y and enter

NOTE Make sure you look up how to generate a sock file with your particular language/framework, for example deploying a flask python app, I'd likely use gunicorn to act as a web app so the ExecStart command would look like...

# We'll then specify the commanded to start the service
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:app.sock --access-logfile /home/testuser/mygitrepo/accesslog --error-logfile /home/testuser/mygitrepo/errorlog -m 007 wsgi:app

This gunicorn command create an app.sock file and this will allow Nginx to communicate with the service.

Start the Service

  • then run sudo systemctl start app
  • then run sudo systemctl enable app

**NOTE: double check the service is running with the command sudo systemctl status app, if you need to do any trouble shooting to restart the server do a sudo systemctl daemon-reload and then start the service again.

Step 4 - Configuring Nginx

  • Install Nginx sudo apt-get install nginx
  • create configuration file sudo nano /etc/nginx/sites-available/app
server {
        listen 80;
        server_name subdomain.domain.com;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/testuser/mygitrepo/app.sock;
        }
}

use pwd to make sure you have the right file path to the where the directory with run.py is on your machine. Also make sure to replace the domain above with a domain that is pointing to the server.

  • run command sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled
  • restart nginx sudo systemctl restart nginx
  • set firewall for nginx sudo ufw allow 'Nginx Full'

NOTE If you don't want to a service and instead just want to run the application locally and forward traffic to it. Imagine I have a express web server running on port 5000 my conf file may look like this instead:

server {
        listen 80;
        server_name subdomain.domain.com;

        location / {
                include proxy_params;
                proxy_pass http://127.0.0.1:5000;
        }
}

In this case, it's ok your apps server is hosted on 127.0.0.1:5000 cause it's not being accessed externally. Instead external requests will go through port 80 (http) or port 443 (https) to the Nginx server who then redirects it your app based on your conf file.

Deploying a web application to your ArcGIS Server Linux site on Amazon

Deploying a web application to your ArcGIS Server Linux site on Amazon Web Services (AWS) requires some preparation. Follow these steps to expose your application in a secure and stable manner. You will need to complete some of these steps in the AWS Management Console or a third-party client similar to Amazon Web Services.

  1. Create a site with ArcGIS Server Cloud Builder on Amazon Web Services.
  2. Add rules to your site's Amazon security group to allow the following:
  • SSH access (usually via port 22) for your IP address. This allows you to connect and configure your instance.
  • HTTP access (usually via port 80) for the IP range of your choice.
  1. Using SSH, connect to the site server instance. This is the instance that contains the configuration location. It is referred to as SITEHOST when you view your instance list in the AWS Management Console. Unless otherwise specified, you will complete the rest of these steps while remaining connected to this instance.
Dumup Community
dumup, solving problems and help your productivity by answering each other’s answers

To connect, enter the following command:

ssh -i <your key pair file> ubuntu@<Public DNS name of your AWS instance>

  1. Launch Apache using the following command:

sudo service apache2 start

  1. Configure Apache to start as soon as the operating system starts. Use the following command:

sudo update-rc.d apache2 defaults

  1. Using the AWS Management Console or another AWS-like client, assign an Elastic IP address and associate it with the site server instance in your site (remember that this is the instance you are connected to).

The Elastic IP address is necessary because Amazon Web Service changes the machine name as soon as you stop or start a site. The Elastic IP address gives you a constant address that allows you to access your ArcGIS Server.

  1. Deploy your web application by copying it to the Apache root folder, /var/www/.
  2. You can also contact your network administrator to register a domain name for your site.

In most cases, it will be better to create a domain name, such as mescartes.maville.gov instead of directing users through your elastic IP address. Provide your Elastic IP address to your network administrator, who will associate a domain name that forwards traffic to your site.

  1. Refresh the URLs in your Web application code.
  • JavaScript apps: URLs must use your new domain name. For example, http://mymaps.mycity.gov/arcgis/rest/services/MyService/MapServer.
  • Flex and Silverlight applications: Use URLs containing the elastic load balancer (ELB) addresses that Cloud Builder placed on your site. For example, http://<ELB address>/arcgis/rest/services/MyService/MapServer. Flex and Silverlight can access Web services through the ELB address because the client access policy files have been automatically added to the site.