📜 ⬆️ ⬇️

Setting up a server for django projects from scratch

I want to share the experience of setting up a server for django-projects. It turned out that I often have to configure a VPS server from scratch to run django sites on them. Somehow, I had the idea to write down the setup process step by step. It turned out that “on a piece of paper” to perform these routine operations is much easier and faster - all the nuances are written down, it is difficult to miss something. Further more - I turned the instruction into an independent shell script - I started it and the server is ready. I think some python developers, especially beginners, will find it helpful to familiarize themselves with the contents of the script. With some modifications, you may want to use it in your practice.

So let's say you just bought a VPS server and put a stable Debian release there. First of all, you copy your RSA key to the server:

ssh-copy-id root@server

Now we go to the server, download the script and run it:
')
wget -O ~ / install.sh dumpz.org/57292/nixtext
sh ~ / install.sh

Everything :) If you run the script as I wrote it, then you now have nginx, postgres, supervisor, uwsgi; Created a web user who can work with postgres installed all popular DVCS; various python libraries have been installed and a lot of little things have been done. All you have to do is to clone your project to the / web / project_name directory, further configure the nginx config for your site (a template has been created in sites-enabled), register a couple of lines in the supervisor config, and launch the site.

My django projects are usually also configured by a single script that creates virtualenv, installs dependencies into it, creates a database, creates syncdb, performs migrations, creates initial data - but this is a topic for another article.

As a result, launching a django site on a bare server turns from a routine into a quick and almost painless procedure (well, anyway, any thread bugs will come out).

The current version of the script is here: dumpz.org/57292

I also decided to post the text of the script here with detailed comments in Russian.

#!/bin/sh

# vim -
# : , ssh
# habrahabr
wget -O ~/.vimrc http://dumpz.org/25712/nixtext/

# vim -
# , , crontab,
# nano
update-alternatives --set editor /usr/bin/vim.basic

# netselect-apt .
#
# debian
apt-get install netselect-apt -y
rm /tmp/sources.list
netselect-apt -o /tmp/sources.list -n stable

# /tmp/sources.list
#
# :
# debian-: ,
# (sid) , nginx-
# uwsgi
echo "deb http://security.debian.org/ squeeze/updates main contrib non-free" > /etc/apt/sources.list
LINE = $( cat /tmp/sources.list | grep '^deb' | head -1 )
echo $LINE /etc/apt/sources.list >> /etc/apt/sources.list
echo $LINE | sed 's/stable/testing/' >> /etc/apt/sources.list
echo $LINE | sed 's/stable/sid/' >> /etc/apt/sources.list

# -,
# , -,
cat > /etc/apt/apt.conf.d/07custom << EOF
APT::Install-Recommends "false";
APT::Default-Release "stable";
EOF


#
# sources.list
aptitude update

# : en_US -.
# ru_RU
aptitude install locales -y
echo en_US.UTF-8 > /etc/default/locale
cat > /etc/locale.gen << EOF
en_US.UTF-8 UTF-8
ru_RU.UTF-8 UTF-8
EOF

locale-gen

#
# .. ,
aptitude upgrade -y

#
# psmisc pkill
# screen sudo
aptitude install psmisc screen sudo -y

# exim4,
# ,
# dpkg-reconfigure
aptitude install exim4 -y
#dpkg-reconfigure exim4-config

# nginx sid , uwsgi
# ngin
# -
aptitude install nginx -t sid -y
update-rc.d nginx defaults
/etc/init.d/nginx start

# web
# - django-
# RSA , ssh
# vim
useradd -m web -s /bin/bash
cp ~/.vimrc /home/web
mkdir /home/web/.ssh
cp ~/.ssh/authorized_keys /home/web/.ssh
chown -R web:web /home/web

# /web, -
mkdir /web /web/run /web/log
chown -R web:web /web

#
aptitude install mercurial subversion git-core -y

# mercurial
#
#
cat > /home/web/.hgrc << EOF
[ui]
username = Name Name
EOF

chown web:web /home/web/.hgrc

# python :)
# python-dev - python,
# python-, C
# python-setuptools easy_install
aptitude install python python-setuptools python-dev -y

# pip virtualenv
easy_install -U pip
easy_install -U virtualenv

# gcc python-
aptitude install gcc -y

# python- lxml
# ,
#
aptitude install python-lxml -y

# Fabric -
pip install -U fabric

# PIL
# ,
# , PIL JPEG :)
aptitude install libjpeg62-dev libfreetype6-dev -y
pip install -U PIL

# uwsgi
#
aptitude install libxml2-dev -y
pip install -U http://projects.unbit.it/downloads/uwsgi-latest.tar.gz

# postgres
# , - web
#
aptitude install postgresql python-psycopg2 -y
su postgres -c "cd /; createuser -s web"

# uwsgi-
#
# , ""
aptitude install supervisor -y

# ,
cat > /etc/supervisor/conf.d/inet.conf << EOF
[inet_http_server]]
port=666
#username=
#password=
EOF


# ,
# django- supervisor-
#
# uwsgi , uwsgi uwsgi.xml
#
cat > /etc/supervisor/conf.d/web.conf << EOF
#[program:PROJECT]
#directory=/web/PROJECT
#command=uwsgi -x uwsgi.xml
#user=web
EOF


# supervisor,
/etc/init.d/supervisor restop

# , web
# supervisorctl sudo
# -,
echo "web ALL = NOPASSWD: /usr/bin/supervisorctl" >> /etc/sudoers

# nginx
#
# VIM HOST PROJECT

cat > /etc/nginx/sites-enabled/template << EOF
server {
server_name .HOST.com;

error_log /web/log/HOST.com-error.log warn;
access_log /web/log/HOST.com-access.log;

location /static/admin-media {
alias /web/PROJECT/var/.env/lib/python2.6/site-packages/django/contrib/admin/media;
}

location /static/ {
root /web/PROJECT;
}

location /robots.txt {
root /web/PROJECT/static;
}

location /favicon.ico {
root /web/PROJECT/static;
}

location / {
include uwsgi_params;
uwsgi_pass unix:/web/run/PROJECT.sock;
}
}
EOF



Thank you for scrolling to this place, I hope, the wheel of your mouse is not broken. I wish this material to be useful to someone.

UPDATE : This is not a solution for deployment, although there are some hints there. This is just a script that installs and configures the necessary software for comfortable work with django-projects.

UPDATE2 : Created a project on the bitbeat: bitbucket.org/lorien/django-server Forkyte on health.

Source: https://habr.com/ru/post/120363/


All Articles