Skip to content

Instantly share code, notes, and snippets.

@schesnowitz
Created July 16, 2023 18:48
Show Gist options
  • Save schesnowitz/52c8739b0549cfac91d36b0269c75f2a to your computer and use it in GitHub Desktop.
Save schesnowitz/52c8739b0549cfac91d36b0269c75f2a to your computer and use it in GitHub Desktop.
SC Tech - YouTube Server

Create a Django Server

https://cloud.linode.com/

update and upgrade

ssh root@123.45.6.789
sudo apt update && sudo apt upgrade -y

Custom Domain Setup

point domain to linode https://www.linode.com/community/questions/18941/how-to-point-domain-to-linode

check the propagation status

https://dnschecker.org/

DNS Records for Linode

ns1.linode.com
ns2.linode.com
ns3.linode.com
ns4.linode.com
ns5.linode.com

Apache Server Installation

ssh into ubuntu remote machine

ssh username@123.45.6.789

Install apache

sudo apt install apache2 

Install Python 3 WSGI adapter module for Apache

sudo apt install libapache2-mod-wsgi-py3

start apache

sudo systemctl start apache2

enable apache

sudo systemctl enable apache2

get apache status

sudo systemctl status apache2

apache commands

nano /etc/apache2/sites-available/django.conf
sudo systemctl start apache2 

sudo systemctl stop apache2 

sudo systemctl restart apache2 

sudo systemctl reload apache2 

sudo systemctl status apache2 

sudo systemctl enable apache2

sudo apachectl configtest 

logs found in

/var/log/apache/access. log

Install Django on a Ubuntu Server

View domain from remote locations https://geopeeker.com/

Install venv

sudo apt install python3-venv 

Install pip

sudo apt install python3-pip

install tree

sudo apt-get install tree
exec -l $SHELL  # <= restart shell

sudo mkdir project

Create a virtual environment

<!-- make sure you are in the directory or inlcude the full path -->
sudo python3 -m venv environment
# source /var/www/django/environment/bin/activate

Activate the virtual environment

source /var/www/ubuntu-deployment/environment/bin/activate

install django

sudo pip install django

sudo python3 manage.py runserver 0.0.0.0:8000

update settings.py

import os

STATIC_URL='/static/'
STATIC_ROOT=os.path.join(BASE_DIR, 'static/') 

MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR, 'media/')

migrate DB

./manage.py makemigrations
./manage.py migrate

if you need to kill a process

sudo kill -9 `sudo lsof -t -i:8000`

Apache Configuration for a Django Application

Apache configuration file

<VirtualHost *:80>

        ServerAdmin admin@chainai.online
        ServerName chainai.online
        ServerAlias www.chainai.online

        DocumentRoot /var/www/project/

        ErrorLog ${APACHE_LOG_DIR}/chainai.online_error.log
        CustomLog ${APACHE_LOG_DIR}/chainai.online_access.log combined

        Alias /static /var/www/project/static
        <Directory /var/www/project/static>
                Require all granted
        </Directory>

        Alias /media /var/www/project/media
        <Directory /var/www/project/media>
                Require all granted
         </Directory>

        <Directory /var/www/project/core_project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess dj python-path=/var/www/project python-home=/var/www/project/environment
        WSGIProcessGroup dj
        WSGIScriptAlias / /var/www/project/core_project/wsgi.py

</VirtualHost>

disable conf

sudo a2dissite 000-default.conf 

enable conf

sudo a2ensite django.conf

logs found in

sudo tail -800 /var/log/apache2/access.log

apache commands

nano /etc/apache2/sites-available/django.conf
sudo systemctl start apache2 

sudo systemctl stop apache2 

sudo systemctl restart apache2 

sudo systemctl reload apache2 

sudo systemctl status apache2 

sudo systemctl enable apache2

sudo apachectl configtest 

Django Firewall on Ubuntu

UFW

sudo apt install ufw
sudo ufw enable
sudo ufw status verbose
sudo ufw disable
sudo ufw allow from 123.4.567
sudo ufw app list | grep Apache
sudo ufw allow 80
sudo ufw allow ssh 
sudo ufw allow https
sudo ufw reset
sudo ufw allow 'Apache Full'
sudo ufw delete allow 'Apache'

SSL Django with Certbot

Video 7

install certbot

sudo apt install certbot python3-certbot-apache

get a cert

sudo certbot --apache -d domain.com -d www.domain.com
sudo certbot --apache -d chainai.online -d www.chainai.online

If you are using SQLite as your database, you will need to give some more permissions

sudo chmod 664 /var/www/project/db.sqlite3
sudo chown :www-data /var/www/project/db.sqlite3
sudo chown :www-data /var/www/project 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment