Skip to content

codingforentrepreneurs/Hello-Linux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hello Linux

This series is all about deploying a Python web application on Ubuntu 18.04 LTS.

You can spin up a virtual machine with Ubuntu at a variety of places:

We'll be using Digital Ocean since it's really easy to get started. What you'll learn here can also be applied to deploying Python projects on any Ubuntu server. In some cases, this setup will work with other Linux distributions too albeit different methods of installation (ie not using apt-get).

Here's the plan:

  • Launch a virtual machine (droplet) with Digital Ocean
  • Access our droplet via SSH
  • Install Updates & Dependancies via bash script which is here
  • Configure & Implement:
    • Git (for push, build, & deploy) [docs]
    • Supervisor (process manager) [docs]
    • Nginx (web server / load balancer) [docs]
    • Redis (task queue datastore / message broker) [docs]
    • Celery (worker / task queues) [docs]
    • Gunicorn (WSGI web server for python) [docs]
    • PostgreSQL (database) [docs]
    • Django (Python web framework) [docs]
    • Let's Encrypt for HTTPs Certificates

1. Automatic Virtual Machine Setup

After you run the below command, you'll see an endpoint to add to your local git remote.

wget https://raw.githubusercontent.com/codingforentrepreneurs/Hello-Linux/master/setup.sh
chmod +x setup.sh
./setup.sh

2. Production-Ready Django Project

We'll be using a bare bones Django project that's mostly ready for production. It's just an example but an important one to get this working.

Go to this guide to get started.

3. Create PostgreSQL Database for Django

To create a PostgreSQL database, it's recommended to use setup.sh on Server.

Another option is to run:

# enable current logged in user as a default user for postgres
sudo -u postgres createuser $USER

sudo -u postgres psql --command="CREATE DATABASE ${projectDB};"

sudo -u postgres psql --command="CREATE USER ${projectDBuser} WITH PASSWORD '${newPassword}';"

sudo -u postgres psql --command="ALTER ROLE ${projectDBuser} SET client_encoding TO 'utf8';"

sudo -u postgres psql --command="ALTER ROLE ${projectDBuser} SET default_transaction_isolation TO 'read committed';"

sudo -u postgres psql --command="ALTER ROLE ${projectDBuser} SET timezone TO 'UTC';"

sudo -u postgres psql --command="GRANT ALL PRIVILEGES ON DATABASE ${projectDB} TO ${projectDBuser};"

Be sure to replace ${projectDB}, ${projectDBuser}, and ${newPassword} to the values you want to use. The setup script does this automatically.

Update Django Production Settings (src/cfehome/settings/production.py)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '${projectDB}',
        'USER': '${projectDBuser}',
        'PASSWORD': '{newPassword}',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Activate Virtual Environment & Migrate Django

$ cd path/to/django/proj
$ pipenv shell
(venv) $ pipenv install psycopg2-binary # you might need this
(venv) $ python manage.py migrate

Our example

$ cd /var/www/hello_linux/src/
$ pipenv shell
(src) $ python manage.py migrate