To connect to mysql, Django depends on mysqlmysqldb lib provided by mysqlclient.
Why django doesn't use PyMySQL? Refer to What's the difference between PyMySQL and mysqlclient-python?
- For windows, download wheel from Unoffical PYPI and install it.
- For Ubuntu, execute the following commands:
sudo apt-get install python-dev libmysqlclient-dev
pip install mysqlclient
Modify project setting file setting.py
:
DATABASES = {
'default': {
# 'ENGINE': 'mysql.connector.django',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'I321761',
'USER': 'root',
'PASSWORD': '******',
'HOST': '10.128.184.199',
'PORT': '3306',
},
}
We use uwsgi to run the application, here we use uWSGI as uwsgi server. For a real productive deployment, we also need to use nginx as a HTTP reverse proxy.
- Copy project archive to you ubuntu host.
- Install python libraries that application depends
Go to project folder, and execute:
pip install -r requirements.txt
For Ubuntu:
- Install c header compiler dependency:
apt-get install build-essential python-dev
- Install uswgi via pip:
pip install uwsgi
- Optionally, you can install uwsgitop which is used to monitor uwsgi:
pip install uwsgitop
- Optionally, you can install uwsgitop which is used to monitor uwsgi:
Go to django-learning directory and execute one of below commands:
uwsgi --http :8080 --wsgi-file djangoproject/wsgi.py
uwsgi --http :9090 -w djangoproject.wsgi
uwsgi -s :9090 -w djangoproject.wsgi -M --pidfile /tmp/uwsgi.pid -p 4 -d uwsgi.log
or
uwsgi --socket :9090 -wsgi djangoproject.wsgi --master --pidfile /tmp/uwsgi.pid --processes --daemonize
- -M/--master: Manager worker
- --pidfile: pid file, with it we can easily to stop uwsgi via:
uwsgi --stop /tmp/uwsgi.pid
- -p/--processes: Worker process count
- -d/--daemonize: Daemonize uWSGI
- -w/--wsgi: Load a wsgi module
uwsgi uwsgi.ini
-- start uWSGI via configuration file.
Refer to nginx offical guide.
-
Download PGP key for nginx repository signature:
wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
-
Add repo Modify
/et/apt/source.list
, add below 2 lines at the end of file:- For Ubuntu 16.04:
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx
- For Ubuntu 14.04:
deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx
- For Ubuntu 16.04:
-
Execute following commands:
apt-get update
apt-get install nginx
-
Modify nginx configuration
By default, it locates at/etc/nginx/
, main configuration is/etc/nginx/nginx.conf
, and sub-configuration is/etc/nginx/conf.d/default.conf
.
Open default.conf and modifylocation
like:location / { include /etc/nginx/uwsgi_params; uwsgi_pass 127.0.0.1:9090; }
Additionally, modify
server_name
to IP, like10.128.184.199
, but notlocalhost
. -
Start nginx
- Start:
nginx
- Shutdown:
nginx -s stop
ornginx -s quit
- Reloading configuration:
nginx -s reload
- Start:
-
Start uWSGI
Go to django-learning directory and execute:
uwsgi -s :9090 -w djangoproject.wsgi
-
Prerequisites
SetSTATIC_ROOT
insetting.py
, and after application is deployed, execute:python manage.py collectstatic
Then all static files that applications uses will be copied toSTATIC_ROOT
. -
Use uWSGI
uWSGI serving static file refers to here.-
Option 1: Add
--static-check
or--check-static
option when running uWSGI, i.e.:
uwsgi -s :9090 -w djangoproject.wsgi --static-check /wxc/workspace/
With this option, when server received a http request"http://hostname:9090/static/admin/favorate.icon"
, uWSGI will checkstatic/admin/favorate.icon
under/wxc/workspace/
first. -
Option 2: Add
--static-map
in running command:
uwsgi -s :9090 -w djangoproject.wsg --static-map /static=/wxc/workspace/static
With this option, when server received a http request"http://hostname:9090/static/favorate.icon"
, uWSGI will returnfavorate.icon
under/wxc/workspace/static
.
-
-
Install
virutualenv
pip install virutalenv
-
Deploy application
- Create virtual python environment
virualenv .env
-- ".env" folder is created, which contains a isolated python. - Activate virtual environment
source .env/bin/activate
(If you want to quit the virtual env, executedeactivate
) - Install dependency libraries
cd django-learning
pip install -r requirements.txt
- Collect static files
python manage.py collectstatic
- Start uwsgi
uwsgi uwsgi.ini
- Create virtual python environment
You can use model tool of MySQL workbench to create the model file and import to your database.
Refer to MySQL modeling.
Use manage.py
to manage database model.
-
Before first run application, we need map django models to database table.
python manage.py migrate [app]
-
After django model changes
- Create migrations
python manage.py makemigrations [app]
- View one migration
python manage.py sqlmigrate [app] [000x]
- Implement all or one migration
python manage.py migrate [app] [000x]
- Create super user
python manage.py createsuperuser
- Create migrations
-
Make application's model administrated in
"django.contrib.admin"
Register model in app'sadmin.py
, like:
from django.contrib import admin
from .models import Country, User
admin.site.register((Country, User))
-
Install
django-redis-cache
pip install django-redis-cache
-
Django setting
Modifysettings.py
:
CACHES: {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': [
'10.128.184.199:6379',
],
'OPTIONS': {
'DB': 1,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 50,
'timeout': 20,
},
'MAX_CONNECTIONS': 1000,
'PICKLE_VERSION': -1,
},
}
}
- Read & Write to Redis cache
from django.core.cache import cache
from .models import User
def get_user(req):
id = req.GET.get('id')
key = 'myapp.user.%d'%id
user = cache.get(key)
if not user:
user = User.objects.get(id=id)
cache.set(key, user)
return HttpResponse(user)
Refer to Docker for Ubuntu.
Here we use Ubuntu Xenial 16.04 (LTS).
-
If you have old version and you want to remove it
$ sudo apt-get remove docker docker-engine docker.io
-
Setup the repository
- Update
apt
package index:$ sudo apt-get update
- Allow
apt
to use a repository over HTTPS:
$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
-
Add docker official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-
Setup the
stable
repository
$ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
- Update
-
Install Docker CE
- Update
apt
package index$ sudo apt-get update
- Install the latest version of Docker CE
$ sudo apt-get install docker-ce
OR
$ sudo apt-get install docker-ce=<VERSION>
- Verify installation by running
hello-world
image
$ sudo docker run hello-world
- Update
-
Configure Docker to start on boot
$ sudo systemctl enbale docker
-
Start docker daemon
$ sudo systemctl start docker
OR
$ sudo service docker start
-
Set HTTP/HTTPS proxy
$ mkdir -p /etc/systemd/system/docker.service.d
- Create file
/etc/systemd/system/docker.service.d/http-proxy.conf
and add:
[Service] Environment="HTTP_PROXY=http://proxy.example.com:80/" "HTTPS_PROXY=https://proxy.example.com:443/"
- Flush changes and restart docker
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
`$ docker build -t mydjango:0.1 . `