Skip to content

Instantly share code, notes, and snippets.

@boazcstrike
Last active March 19, 2021 19:02
Show Gist options
  • Save boazcstrike/60db1795f3b3bafccea1649d2ba30261 to your computer and use it in GitHub Desktop.
Save boazcstrike/60db1795f3b3bafccea1649d2ba30261 to your computer and use it in GitHub Desktop.
Python Django API Server Setup Ubuntu 20

Essential Linux Commands

Navigating

go inside a folder

cd foldername
or
cd foldername/foldername

go up a folder

cd ..
Editing a file
nano filename
or
nano foldername/filename
Screening

New Screen

screen

Deattach Screen

screen -d

Reattach Screen

screen -r

Useful screen commands

create
ctr+A c

kill
ctr+A k

split vertical
ctr+A shift+|

split horizontal
ctr+A shift+S

go to next split
ctr+A tab

unsplit
ctr+A shift+Q

rename
ctr+A shift+A

detach
ctr+A ctr+D

Logs should be mostly be downloaded from the server so...

Creating and copying a simple file
touch filename
chmod 600 filename

cat filename

cat whattocopy >> filename

cat filename
whattocopy content
If you're trying to serve a file from the server, try using simple http
python -m SimpleHTTPServer 8000
If you're trying to download a file to the server
wget http://whateveripordns.com:8000/

Next JS Server Deployment

EC2 Instance on Ubuntu

To compile and install native addons from npm you may also need to install build tools:

# use `sudo` on Ubuntu or run this as root on debian
apt-get install -y build-essential

Node.js v12.x:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js v14.x:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

ec2 instance on Amazon Linux 2

For Latest Release:-

sudo yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -

For Stable Release:-

sudo yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash -

Node JS

sudo yum install -y nodejs
node -v 

v14.4.0

npm -v 

6.14.5

Files

npm install
or
npm -g install yarn
yarn install

package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node server.js",
    "build": "next build && next export",
    "start": "NODE_OPTIONS=--max_old_space_size=3450  NODE_ENV=production ENV=production node server.js"
  },
  • All static assets must be moved to the ./public folder
  • testing is npm run dev
  • npm run build - to build and export all public files (production build)
  • run npm start or npm run start

Django Rest Server Deployment

EC2 Instance on Ubuntu 18

sudo apt update
sudo apt upgrade

# production
apt install nginx
sudo passwd root
sudo reboot
su -

# dev
sudo -s

apt update
apt upgrade

Python 3.6.9 default installation (UBUNTU 18)

apt update
apt install python3
apt install python3-pip
apt install python3-venv

# creating virtual environment

python3 -m venv ENV
source ENV/bin/activate

Python 3.8.2 Installation (UBUNTU optional)

https://tecadmin.net/install-python-3-8-ubuntu/

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
    
cd /opt
sudo wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
sudo tar xzf Python-3.8.2.tgz

cd Python-3.8.2
sudo ./configure --enable-optimizations
sudo make altinstall

# check if success
python3.8 -V

cd /opt
sudo rm -f Python-3.8.2.tgz

Python 3.8.2 Installation (AMAZON LINUX optional)

Step 1 – Prerequaities This Python installation required the GCC compiler on your system. Login to your server using ssh or shell access. Now, use the following command to install prerequisites for Python before installing it.

sudo yum install gcc openssl-devel bzip2-devel libffi-devel

Read: How to Use SSH to Connect Remote Linux Server Step 2 – Download Python 3.8 Download the Python from the Python official website. You can also download the latest version in place of specified below.

cd /opt
sudo wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
sudo tar xzf Python-3.8.2.tgz

Step 3 – Install Python 3.8 Use the below set of commands to compile Python 3.8 from the source code and install using the altinstall command.

cd Python-3.8.2
sudo ./configure --enable-optimizations
sudo make altinstall

make altinstall is used to prevent replacing the default python binary file /usr/bin/python.

Now remove downloaded source archive file from your system

sudo rm -f /opt/Python-3.8.2.tgz

Step 4 – Check Python Version The Python 3.8 binary is installed under /usr/local/bin directory. As we have not overwritten the current Python version, you need to run Python 3.8 command as following:

python3.8 -V

Postgres Installation

https://dailyscrawl.com/how-to-install-postgresql-on-amazon-linux-2/

apt install postgresql postgresql-contrib
apt install python3-psycopg2
apt install libpq-dev
# sqitch over to postgres account
sudo -i -u postgres

# access
psql

# change pw
\password

# quit
\q

MYSQL Server Installation

sudo apt-get install libmysqlclient-dev

GUNICORN WSGI

pip3 install gunicorn

gunicorn --bind 0.0.0.0:8000 --workers 2 name.wsgi

--workers 2 --threads 2 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

Setup as a service

nano /etc/systemd/system/name.service

# content

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/sammy/myproject
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 2 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

NGINX

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name domain_name_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/sammy/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
        client_max_body_size 100M;
    }
}

link to sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/name /etc/nginx/sites-enabled

restart nginx

systemctl restart nginx

https://docs.gunicorn.org/en/latest/deploy.html[https://docs.gunicorn.org/en/latest/deploy.html(https://docs.gunicorn.org/en/latest/deploy.html

Check
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn
sudo journalctl -u gunicorn
sudo systemctl daemon-reload
sudo systemctl restart gunicorn

File setups

.env settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
python3 manage.py makemigrations
python3 manage.py migrate

IMPORTANT

  • ALLOWED_HOSTS
  • CORS_ORIGIN_WHITELIST
  • CSRF_TRUSTED_ORIGINS
  • CSRF_COOKIE_SECURE = True
  • SESSION_COOKIE_SECURE = True
  • SECURE_SSL_REDIRECT = True --> this will ruin redirects, do not use

LOGGING

ADMINS = [
    ('bs', 'test@test.com'),
    ('ek', 'test@test.com')
]

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': './logs/errors.log',
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'email_backend': 'django.core.mail.backends.smtp.EmailBackend'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}
Sources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment