Skip to content

Instantly share code, notes, and snippets.

@wmantly
Last active December 18, 2020 00:06
Show Gist options
  • Save wmantly/f7cbbcdfefbc1c96289ffcc402e0b882 to your computer and use it in GitHub Desktop.
Save wmantly/f7cbbcdfefbc1c96289ffcc402e0b882 to your computer and use it in GitHub Desktop.

Setting up a production/ pre-production server

Update the OS

First, make sure the OS is fully up to date:

apt update
apt upgrade

System Dependences

Now install the system dependences:

apt install apache2 apache2-dev libapache2-mod-wsgi-py3 libpq-dev postgresql postgresql-contrib git python3 python3-dev python3-pip

python3, python3-dev, python3-pip and libpq-dev are required to install Django and other python dependences.

apache2 is the web server and apache2-dev libapache2-mod-wsgi-py3 are used to connect apache to the Django app.

postgresql is the database and postgresql-contrib is a supporting package.

We have to install some supporting packages:

pip3 install virtualenv

virtualenv is a python package allows the for the creation of self contained python environments and needs to be installed globally.

Data Base

The production project uses a Postgres database for its main data store. Lets set up the data base.

Lets get into the postres command line:

sudo -u postgres psql

Now that we are in postgres we need to make a user and a database. Replace <DB PASSWORD> with a string we will use for the password, and keep this for latter it needs to be in the local_setting.py file. A random string 30-40 characters long is recommended.

CREATE DATABASE bytedev;
CREATE USER bytedev WITH PASSWORD '<DB PASSWORD>';
GRANT ALL PRIVILEGES ON DATABASE bytedev TO bytedev;
\q

Now that we have the all the system packages we need, lets get the project and configure it.

Get the project

First, lets move to the /var/www/ directory where the project will live.

cd /var/www

Now, lets clone the project and move into its directory.

git clone [email protected]:ByteAcademyCo/gitwrapper.git
cd gitwrapper

And set up project. The setup.sh script will create the virtual environment, download any git sub-modules and install python and NodeJS packages.

source scripts/setup.sh

If you get an error during the python packages install, run the following:

pip3 install setuptools==45

Configure local settings

The project does not come with the file project/settings/local_settings.py. You will need to create this file and give it the correct settings.

First, lets open the file in a text editor:

nano project/settings/local_settings.py

Here is a sample for production, please replace:

  • <GITHUB_SECRET> with the OAUTH client secret token for production
  • <GITHUB_INVITE_TOKEN> with a github user token that can be used to invite user to organizations.
  • <DJNAGO_KEY> with a long random string (50 characters)
  • <DB_PASSORD> with the password you used to create the postgres user
  • <SMPT2GO_PASSWORD> with the password for the SMTP2GO user
GITHUB = dict(
    CLIENT_ID='3e6868531eec10b37d73',
    CLIENT_SECRET='<GITHUB_SECRET>',
    invite_token='<GITHUB_INVITE_TOKEN>'
    TEST_DISTRIBUTOR_ORG = "ByteTesting",
    DISTRIBUTOR_ORG = "ByteExercises",
    SOURCE_ORG = "ByteAcademyCo",
)

DATADUMP_URL = "http://blah.staging.bytedev.co:3000/master_dump"

API_KEYS = 'apisNeedKeys'

SECRET_KEY = '<DJNAGO_KEY>'

## DATABASES['default'] is overridden in dev.py settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'bytedev',
        'USER': 'bytedev',
        'PASSWORD': '<DB_PASSWORD>',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.smtp2go.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '<SMPT2GO_PASSWORD>'
EMAIL_USE_SSL = True
EMAIL_FEEDBACK_ADDRESS = ['[email protected]', '[email protected]', '[email protected]']

NODEPORT='3000'

Now we can set up the migrate the project. Django will setup the database schema.

./manage.py migrate

Apache

We will use the Apache we server a proxy to Django and NodeJS and to server the static files.

Lets enable the Apache modules need for the web socket proxy

a2enmod expires

Now lets configure apache. First remove the default config:

rm /etc/apache2/sites-enabled/000-default.conf

Lets add the down message config:

nano /etc/apache2/sites-available/000-down-bytedev.co.conf

Contents

<VirtualHost *:80>
    ServerName bytedev.co
    DocumentRoot /var/www/gitwrapper/static/error_pages
</VirtualHost>

Lets add out apache config file, open your text editor:

nano /etc/apache2/sites-available/000-bytedev.co.conf

Here is the apache config file, no editing should be needed:

<VirtualHost *:80>
    ServerName www.bytedev.co
    Redirect permanent / http://bytedev.co/
</VirtualHost>


<VirtualHost *:80>
    # ServerAdmin [email protected]
    ServerName bytedev.co

    <IfModule mod_expires.c>
        <FilesMatch "\.(jpe?g|png|gif|js|css)$">
            ExpiresActive On
            ExpiresDefault "access plus 1 week"
        </FilesMatch>
    </IfModule>

    ErrorLog ${APACHE_LOG_DIR}/bytedev.co-error.log
    CustomLog ${APACHE_LOG_DIR}/bytedev.co-access.log combined

    Alias /static /var/www/gitwrapper/staticfiles

    WSGIDaemonProcess gitwrapper python-path=/var/www/gitwrapper python-home=/var/www/gitwrapper/env
    WSGIProcessGroup gitwrapper
    WSGIScriptAlias / /var/www/gitwrapper/project/wsgi.py


</VirtualHost>

Nows lets enable the new config

ln -s /etc/apache2/sites-available/000-bytedev.co.conf /etc/apache2/sites-enabled/.
service apache2 reload

All set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment