Simple setup postgres db for django

sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
sudo su – postgres
psql
CREATE DATABASE uweb;
CREATE USER django WITH PASSWORD ‘django’;
ALTER ROLE django SET client_encoding TO ‘utf8’;
ALTER ROLE django SET default_transaction_isolation TO ‘read committed’;
ALTER ROLE django SET timezone TO ‘UTC’;
GRANT ALL PRIVILEGES ON DATABASE uweb TO django;

Render latest git date commit in django

Create file on server:

nano pull_repo.sh

#!/bin/sh

git –work-tree=/home/…web/ –git-dir=/home/…web/.git pull origin dev

git –work-tree=/home/…web/ –git-dir=/home/…web/.git log -1 –format=%cd > /home/…web/last.commit_date

chmod +x pull_repo.sh

Edit crontab:

crontab -e

Add line:

*/5 * * * * /home/…/pull_web.sh

In Django

decorator:

def prepare_commit_date(function):
    def wrap(request, *args, **kwargs):
        try:
            with open(os.path.join(BASE_DIR, "last.commit_date"), "r") as last_commit_date:
                request.commit_date = last_commit_date.readlines()[0].replace('\n', '')
        except Exception as ex:
            request.commit_date = str(ex.message)
        return function(request, *args, **kwargs)
    return wrap

view:

@prepare_commit_date
def index(request):
 return render(request, 'index.html', {})

template:

{{ request.commit_date }}

ubuntu, django, gunicorn, nginx

sudo apt-get install nginx python-pip libpq-dev supervisor

 

export LC_ALL=C

 

sudo pip install django gunicorn psycopg2

Open:

sudo nano /etc/nginx/sites-available/default

Paste:

server {
listen 80;
server_name 192.168.23.109;
access_log /var/log/nginx/access.log;

location /static/ {
alias /home/django/web/static;
}

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

 

For test guincorn:

gunicorn sg_web.wsgi:application

for re-test:

killall gunicorn

 

For autostart after reboot, open:

nano /etc/supervisor/conf.d/sg_web.conf

Paste:

[program:sg_web]

command=gunicorn sg_web.wsgi:application -c /home/django/sg_web/sg_web/gunicorn.conf.py

directory=/home/djangp/sg_web

user=nobody

autorestart=true

redirect_stderr=true

 

sudo systemctl enable supervisor

 

Tail -view logs

For example:

 

tail -f /var/log/upstart/gunicorn.log -n 500

tail -f /var/log/nginx/access.log -n 500

tail -f /var/log/nginx/error.log -n 500

For colored output:

sudo tail -f /var/log/nginx/access.log | sed -e ‘s/\(.* 200 .*\)/\o033[32m\1\o033[39m/’ -e ‘s/\(.* 301 .*\)/\o033[31m\1\o033[10m/’

ubuntu + django + postfix

sudo apt-get install postfix
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mydestination = localhost
myhostname = domain.net

 

Now, let’s test to see if we can send an email to our mailbox via telnet:

telnet localhost 25

Once connected, enter the following line by line:

mail from: whatever@whatever.com
rcpt to: your_real_email_addr@blah.com
data (press enter)
type whatever content you feel like to type
. (put an extra period on the last line and then press enter again)

If everything works out, you sould see a comfirmation message resemables this:

250 2.0.0 Ok: queued as CC732427AE

It is guaranteed that this email will end up in the spam box if you use Gmail. So take a look at your spam inbox to see if you received the test mail (it may take a minute to show up).

If you recevied the test email, then it means postfix is working properly. Now, let’s config Django to send email via postfix.

First, I added the following line to my settings.py file:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'Server <server@whatever.com>'

 

Setup Multiple Django Websites on Django One Click Install Image VPS

How To Use the Django One-Click Install Image

Copy the Nginx configuration for your new site:

cp /etc/nginx/sites-available/django /etc/nginx/sites-available/site2
ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/site2

Then edit its contents to match where you installed your new Django app:

upstream app_server {
    server 127.0.0.1:9500 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name www.mysite2.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/new_project/new_app/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/new_project/new_app/static; 
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

Do the same for the Upstart script used to start gunicorn:

cp /etc/init/gunicorn.conf /etc/init/new_site.conf

And change the port and path for the project:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=new_project \
    --pythonpath=new_project \
    --bind=0.0.0.0:9500 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

You should now be able to start your project with:

service nginx restart
service new_site start

Notice that the way you start the project is determined by the name you give the Upstart script.

Hope this points you in the right direction! Let us know how it goes.