Skip to content

Commit

Permalink
Merge pull request #19 from Falldog/example-django
Browse files Browse the repository at this point in the history
(example) add vagrant environment for django example
  • Loading branch information
Falldog authored Oct 6, 2017
2 parents 9b0a538 + a4adb90 commit c30cc08
Show file tree
Hide file tree
Showing 22 changed files with 494 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ $ TEST_PYE_PERFORMANCE_COUNT=1 ./pyconcrete-admin.py test
```


Example
--------------
[Django with pyconcrete](example/django)


Building on Windows
--------------
#### Python 2.7 - Visual Studio 2008
Expand Down
31 changes: 31 additions & 0 deletions example/django/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pyconcrete example for Django
==============


Environment setup
--------------
* install vagrant && VirtualBox
```bash
$ cd pyconcrete/example/django && vagrant up
```
* access `http://127.0.0.1:5151` by browser


Environment
--------------
* Ubuntu 14.04
* Python 2.7
* Nginx + uwsgi
* Django 1.11


How the example working
--------------
* Django in VM listen port 5151
* install `pyconcrete` in system
* source code path in VM: `/home/pye_web_src`
* encrypted code path in VM: `/home/pye_web`
* leave these two files as `.py`, and need add `ipmort pyconcrete` at beginning
* pye_web/pye_web/wsgi.py
* pye_web/manage.py

19 changes: 19 additions & 0 deletions example/django/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.box = "ubuntu/xenial64"
config.vm.hostname = "pyconcrete-example-django-server"

config.vm.provider "virtualbox" do |v|
v.name = "pyconcrete_example_django_server_sandbox"
end

config.vm.synced_folder "../../", "/home/pyconcrete"
config.vm.synced_folder "./pye_web/", "/home/pye_web_src"

config.vm.network :forwarded_port, guest:5151, host:5151, auto_correct: false # Nginx

config.vm.provision :shell, path: "bootstrap.sh", args: $args, privileged: false

end
76 changes: 76 additions & 0 deletions example/django/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash

USER=ubuntu

PYE_HOME=/home/pye_web # encrypted
PYE_HOME_SRC=/home/pye_web_src # source
PYE_LOG_DIR=/var/log/pye_web
PYCONCRETE_HOME=/home/pyconcrete


setup_python()
{
sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:fkrull/deadsnakes
sudo apt-get update

sudo apt-get install -y python2.7 python2.7-dev
#sudo apt-get install -y python3.3 python3.3-dev
#sudo apt-get install -y python3.4 python3.4-dev
#sudo apt-get install -y python3.5 python3.5-dev
#sudo apt-get install -y python3.6 python3.6-dev
}

setup_nginx()
{
sudo apt-get install -y nginx
sudo apt-get install -y uwsgi
sudo apt-get install -y supervisor

# uwsgi + supervisor
sudo mkdir -p /etc/uwsgi
sudo cp /vagrant/config/uwsgi/pye-web-uwsgi.ini /etc/uwsgi

sudo cp /vagrant/config/supervisor/uwsgi.conf /etc/supervisor/conf.d
sudo supervisorctl reload

# nginx
sudo cp /vagrant/config/uwsgi/uwsgi_param /etc/nginx
sudo cp /vagrant/config/nginx/nginx.conf /etc/nginx
sudo cp /vagrant/config/nginx/pye-web-uwsgi.conf /etc/nginx/sites-enabled

sudo service nginx reload
}

setup_pyconcrete()
{
cd $PYCONCRETE_HOME
sudo python setup.py install <<STDIN
pyconcrete
pyconcrete
STDIN

}

setup_pye_web()
{
sudo mkdir -p $PYE_HOME
sudo chown $USER:$USER $PYE_HOME
cp -r $PYE_HOME_SRC/* $PYE_HOME
pyconcrete-admin.py compile --source=$PYE_HOME --pye --remove-py --remove-pyc -i wsgi.py manage.py

sudo supervisorctl restart uwsgi
}

sudo apt-get update
sudo apt-get install -y python-pip

sudo pip install -r $PYE_HOME_SRC/requirements.txt
sudo mkdir -p $PYE_LOG_DIR
sudo chown $USER:$USER $PYE_LOG_DIR

setup_python
setup_nginx
setup_pyconcrete
setup_pye_web

75 changes: 75 additions & 0 deletions example/django/config/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
user ubuntu;
worker_processes 2;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile off;

tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

32 changes: 32 additions & 0 deletions example/django/config/nginx/pye-web-uwsgi.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/pye-web-uwsgi.sock; # for a file socket
}

# configuration of the server
server {
# the port your site will be served on
listen 5151;

# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;

# max upload size
client_max_body_size 75M; # adjust to taste

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

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_param; # the uwsgi_params file you installed
}

location /favicon.ico {
alias /home/pye_web/static/favicon.ico;
}
}
5 changes: 5 additions & 0 deletions example/django/config/supervisor/uwsgi.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[program:uwsgi]
command = /usr/local/bin/uwsgi --ini /etc/uwsgi/pye-web-uwsgi.ini

user=ubuntu
group=ubuntu
20 changes: 20 additions & 0 deletions example/django/config/uwsgi/pye-web-uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[uwsgi]

# ======= Django-related settings =======
# the base directory (full path)
chdir = /home/pye_web
# Django's wsgi file
wsgi-file = pye_web/wsgi.py

# process-related settings
master = true
processes = 1
socket = /tmp/pye-web-uwsgi.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664

# clear environment on exit
vacuum = true

logto = /var/log/pye_web/uwsgi.log
touch-reload = /home/pye_web/pye_web/wsgi.py
15 changes: 15 additions & 0 deletions example/django/config/uwsgi/uwsgi_param
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;

uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
Empty file.
6 changes: 6 additions & 0 deletions example/django/pye_web/app_pye/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

# Register your models here.
8 changes: 8 additions & 0 deletions example/django/pye_web/app_pye/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


class PyeConfig(AppConfig):
name = 'pye'
Empty file.
6 changes: 6 additions & 0 deletions example/django/pye_web/app_pye/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.
6 changes: 6 additions & 0 deletions example/django/pye_web/app_pye/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase

# Create your tests here.
6 changes: 6 additions & 0 deletions example/django/pye_web/app_pye/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
23 changes: 23 additions & 0 deletions example/django/pye_web/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import pyconcrete
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pye_web.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
Empty file.
Loading

0 comments on commit c30cc08

Please sign in to comment.