Recent Notes

Web Application Setup - Flask Setup

FLASK SITE SETUP

  1. Create virutualenv for you application:

    ... cd /var/www/appname
    ... python3 -m virtualenv --python=python3 venv
    
  2. Copy the basic application files to your new app directory:

    From your development machine:

    • scp manage.py wsgi.py adminusr@server:/var/www/appname

    • scp django.settings.ini adminusr@server:/var/www/appname/config

    • scp requirements.txt adminusr@server:/var/www/appname/packages

    • scp appsite-0.1.0.tar.gz adminusr@server:/var/www/appname/packages

    • scp app-plugin-0.1.0.tar.gz adminusr@server:/var/www/appname/packages

    Your directory structure should now look like:

    appname
    ├── config
    │   ├── django.settings.ini
    ├── data
    ├── logs
    ├── manage.py
    ├── packages
    │   ├── appsite-0.1.0.tar.gz
    │   ├── app-plugin-0.1.0.tar.gz
    │   ├── requirements.txt
    ├── static
    └── wsgi.py
    

    We’ll describe these files in more detail later.

Web Application Setup - HTTPS Set Up

set up Let’s Encrypt

  1. Install certbot

    ... sudo apt-get update
    ... sudo apt-get install software-properties-common
    ... sudo add-apt-repository ppa:certbot/certbot
    ... sudo apt-get update
    ... sudo apt-get install python-certbot-nginx 
    
  2. Run certbot

    ... sudo certbot --nginx
    
  3. Automate renewal

    ... sudo certbot renew --dry-run
    
  4. Reset firewall to only allow HTTPS

    ... sudo ufw
    

Prev Step: Nginx Setup

Web Application Setup - Install Software

Install Software

  1. Install Python 3

    Good news, python 3 is already install on Ubuntu 17.10.

  2. Install other top level software packages:

    You may need to run ubuntu upgrade before insalling:

    ... sudo apt-get update
    ... sudo apt-get upgrade
    

    Install the following packages:

    ... sudo apt-get install python3.7
    ... sudo apt-get install python3.7-venv
    ... sudo apt-get install python3-pip
    ... sudo apt-get install uwsgi-core
    ... sudo apt-get install uwsgi-plugin-python3
    ... sudo apt-get install nginx
    

    All other python packages will be installed in the applications virtual environment.

Web Application Setup - Nginx Set Up

NGINX SET UP

  1. Create your apps nginx configuration

    ... touch /var/www/appname/config/nginx-appname.cfg
    
  2. The content of your nginx-appname.cfg files should look like this:

    upstream appname {
        server unix:/var/run/uwsgi/appname.socket;
    }
    
    server {
    
        listen 80;
        server_name SERVER_IP SERVER_DOMAIN;
    
        location = /favicon.ico { access_log off; log_not_found off; }
    
        location /static/ {
            root /var/www/appname/;
        }
    
        location / {
            uwsgi_pass      appname;
            include         /etc/nginx/uwsgi_params;
        }
    
    }
    
  3. Link your nginx config file to nginx’ site config directory:

    ... cd /etc/nginx/sites-enabled
    ... ln -s /var/www/appname/config/appname-nginx.cfg appname
    
  4. You can check your nginx config by running the following:

Web Application Setup - Server Security

Optimizing Ubuntu Server

Services and programs you can likely kill:

This was taken from this site

  1. BASICS

    Killing these didn’t impact the system at all when I tested:

    ... sudo apt remove snapd -y --purge
    ... sudo apt remove lxcfs -y --purge
    ... sudo apt remove policykit-1 -y --purge
    ... 
    ... sudo apt remove lvm2 -y --purge
    ... sudo apt remove at -y --purge
    ... sudo apt remove mdadm -y --purge
    ... sudo apt remove open-iscsi -y --purge
    ... sudo apt remove accountsservice -y --purge
    
  2. EXTREME

Web Application Setup - uWSGI Set Up

Set Up uWSGI To Server Your Application

  1. Create your uWSGI configuration file:

    ... touch /var/www/appname/config/uwsgi-appname.ini
    
  2. Contents of your uwsgi-appname.ini should look like this:

    [uwsgi]
    
    plugins = python3,logfile
    
    chdir = /var/www/appname
    home = /var/www/appname/venv
    wsgi-file = /var/www/appname/wsgi.py
    
    master = True 
    cheap = True
    idle = 600
    die-on-idle = True
    manage-script-name = True
    
  3. Link your config file so uwsgi can find it:

    ... cd /etc/uwsgi/apps-enabled
    ... ln -s /var/www/appname/config/uwsgi-appname.ini appname.ini
    

SYSTEMD UWSGI SET UP

  1. Create systemd socket and server files for your uwsgi app:

Markdown Reference

A quick and greatly truncated reference guide for Markdown.

See Also:

Linking

Formating

This is a paragraph with **bolded** text.

This is another paragraph with *emphasized* text.

This is `inline code` in this paragraph.

This is a paragraph with bolded text.

This is another paragraph with emphasized text.

This is inline code in this paragraph.