• Home
  • Django
  • Server preparation for django in production

Server preparation for django in production

linkaiyi
Follow

Server Preparation for django deployment

Guide is made for Ubuntu 18.04

Workflow

s=>start: Server with Ubuntu 18.04 op1=>operation: Configure the global enviroment condi=>condition: Use virtualenv? op2=>operation: Make virtualenv for each app op3=>operation: Use Certbot to get ssl e=>end: Ready s->op1->condi condi(yes)->op2->op3->e condi(no)->op3->e

Check Server Enviroment

  1. Check Python version

    python --version; pip --version

  2. Check cpuinfo

    cat /proc/cpuinfo

  3. Check Ubuntu version

    lsb_release -a

  4. Install pip apt-get install python3-pip, make symbol link to pip3 under /usr/bin with ln -s pip3 pip or to update pip with pip install -U pip

  5. install virtualenv, sudo pip install virtualenv

  6. update package: pip install Package —upgrade

Create a dir to store your virtualenvs (I use ~/.virtualenvs)

'mkdir ~/.virtualenvs'

At this point you are all set to use virtualenv with the standard commands. However, I prefer to use the extra commands included in virtualenvwrapper. Lets set that up.

Install virtualenvwrapper

'sudo pip install virtualenvwrapper'

Set WORKON_HOME to your virtualenv dir

'export WORKON_HOME=~/.virtualenvs'

Add virtualenvwrapper.sh to .bashrc

Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.

'echo ". /usr/local/bin/virtualenvwrapper.sh" >> .bashrc'

Exit and re-open your shell, or reload .bashrc with the command . .bashrc and you’re ready to go.

Create a new virtualenv

'mkvirtualenv myawesomeproject'

to exit your new virtualenv, use deactivate.

Switch between enviornments with workon

To load or switch between virtualenvs, use the workon command:

'workon myawesomeproject'

6 install nginx

`apt-get install nginx`

7 install postgresql

`apt-get install postgresql`

8 add postgre to service

`$ sudo systemctl disable postgresql` or
`$ sudo systemctl enable postgresql`

9 create database and assign user right

sudo -u postgres psql
create database mysql; # ; is important
create user myuser with encrypted password 'password'
grant all privileges on database mysql to myuser

\password myuser # change password for myuser
\l list all databases
\connect dbname # connect to dbname database

deploy django application

copy the folder structure to server. We can use WinSCP to transfer the files via sFTP.

clean migration files and python cache

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

make migrations
python manage.py makemigrations
python manage.py migrate

migrate data if necessary

python manage.py dumpdata > data.json # dumpdata app_name to migrate just one app data
python manage.py shell # delete contentType 
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()

delete sample.com domain if django site is installed

python manage.py loaddata data.json
python manage.py createsuperuser # in case not in old test system

configure uwsgi

Use uwsgi emperor module

_In case of running one uwsgi instance on the server, use the command uwsgi --ini inifile to start the server and uwsgi --reload pidfile to reload the server. kill -INT pid or kill -INTcat pidfile`` to stop the server

  1. make ini configure file

    [uwsgi]
    #使用nginx连接时使用
    #socketfile
    my_app = my_knowledgebase_uwsgi
    socket = /home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).sock
    chmod-socket = 666
    #socket=127.0.0.1:5050
    #直接做web服务器使用
    #http=127.0.0.1:5000
    #http=81.169.229.121:5000
    #项目目录
    chdir=/home/linkaiyi/my_knowledgebase/
    #项目中wsgi.py文件的目录,相对于项目目录
    wsgi-file=ddk_knowledgebase/wsgi.py
    processes=6
    threads=2
    master=True
    pidfile=/home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).pid
    daemonize=/home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).log
    virtualenv=/home/linkaiyi/.virtualenvs/my_knowledgebase
    disable-logging = True
    harakiri = 20
    max-requests = 5000

    # set https on
    env=HTTPS=on

    # clear environment on exit
    vacuum = true
    use %n refer to current ini filename

2 Setup Emperor enviroment

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid username --gid groupname --daemonize /var/log/uwsgi_emperor.log # without daemonize option, uwsgi will run in foreground
# restart the vassal
touch --no-dereference $INI_FILE

install Certbot and certificate the domains

install the Certbot

sudoapt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx
# first domain name will be used as foldername
# expand the certificate with --expand option
# set certificate name with --cert-name name
sudo certbot --nginx -d www.mydomain.com,mydomain.com,someother.domain.com
or
sudo certbot --nginx certonly --expand --cert-name mycert -d www.mydomain.com,otherdomain.com
Object has 0 attachments

Was this article helpful?

This article is viewed 128 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support