📜 ⬆️ ⬇️

We monitor Django projects in top

In this article I will describe how to configure Django so that on the server in the top you can see the load on the server from each of the Django projects.

1 - Installing Django on FreeBSD from ports


I have FreeBSD 8.0 on my server, all kinds of utilities like MySQL, Python2.6, nginx, etc. are already installed. The easiest way to install Django I chose a simple and banal installation of the ports:
cd /usr/ports/www/py-django
make install clean

when installing, select
[X] POSTGRESQL PostgreSQL support
[X] MYSQL MySQL support
[X] FASTCGI FastCGI support

Django 1.3.1 is installed in /usr/local/lib/python2.6/site-packages/django and is ready to go.

2 - Set the stage for Django work


Pros recommend storing projects in a separate directory (for example, for some user in / data next to / www). We will listen to the pros and populate the projects to the hosting def-user - hostalot will be his name .
Django projects will live along the following path - / home / hostalot / data / django
Create a symlink on Django in this directory and check:
cd /home/hostalot/data/django
ln -s /usr/local/lib/python2.6/site-packages/django django
python
>>> import django
>>> exit()

3 - Create a new project


Let's create a Django project for the hostalot.ru site, we can't use the dots, replace it with an underscore - hostalot_ru
At the same time, the moment is unacceptable and we will create a diro with static, which later will be sent directly to nginx (see below).
python django/bin/django-admin.py startproject hostalot_ru
mkdir /home/hostalot/data/django/hostalot_ru/media

In the config file of our new project (/home/hostalot/data/django/hostalot_ru/settings.py) we will edit the settings on a mandatory basis.

4 - Launch Django Projects


Create the executable / usr / local / bin / hostalot_ru
#!/usr/local/bin/bash
exec -a hostalot_ru /usr/local/bin/python $1 $2 $3 $4 $5 $6 $7

Next, create /usr/local/etc/rc.d/hostalot_ru
#!/usr/local/bin/bash
# Replace these three settings.
. /etc/rc.subr
name=hostalot_ru # your project name
rcvar=`set_rcvar`
load_rc_config $name
PROJDIR="/home/hostalot/data/django/$name"
PIDFILE="$PROJDIR/$name.pid"
HOST="127.0.0.1" # local
PORT="9010" # may be any foreach 9010-9900
METHOD="threaded" # may be "pre fork"
command=/usr/local/bin/$name
command_args=" $PROJDIR/manage.py runfcgi method=$METHOD host=$HOST port=$PORT pidfile=$PIDFILE"
run_rc_command "$1"

Do not forget to give these files permissions to execute:
chmod 555 /usr/local/bin/hostalot_ru
chmod 555 /usr/local/etc/rc.d/hostalot_ru

Add to /etc/rc.conf autorun of our project:
hostalot_ru_enable="YES"

Synchronize the database for Django and start the dedicated daemon running our project:
cd /home/hostalot/data/django/hostalot_ru
python ./manage.py syncdb
/usr/local/etc/rc.d/hostalot_ru restart

Nginx configuration


Create a file with the nginx fastcgi config for Django / usr / local / etc / nginx / django_fastcgi:
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_ruTHOD $request_ruthod;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_SOFTWARE "nginx";
fastcgi_param GATEWAY_INTERFACE "CGI/1.1";

Create a configuration file for the domain /usr/local/etc/nginx/confs/hostalot_ru.conf:
server {
listen 193.106.172.16:80;
server_name hostalot.ru;
set $proot "/home/hostalot/data/django/hostalot_ru/media";
location / {
fastcgi_pass 127.0.0.1:9010;
include django_fastcgi;
client_max_body_size 120m;
access_log main;
error_log main;
root $proot;
}
location ~* ^/.+\.(htm|swf|flv|xml|ico|jpg|jpeg|js|css|png|gif|mpg|avi|mp3|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
root $proot;
limit_rate 2000K;
access_log off;
}
}

Restart nginx:
/usr/local/etc/rc.d/nginx restart


Total: each Django-project has its own demon, which is marked by its suffix in the top, all static is cached by nginx.

')

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


All Articles