Linux | Deploy with Pycharm

Django Deployment:

  • ⇒ uwsgi + nginx

The web client <-> The web server(nginx) <-> The socket <-> uwsgi <-> Django App

  • ⇒ supervisor manage process

  • ⇒ Pycharm upload to Remote server

1). uwsgi

Install

sudo apt-get install uwsgi / pip install uwsgi

uwsgi.ini

For connect with nginx

uwsgi.ini

[uwsgi]
socket       = /tmp/app.sock
chdir        = /home/app
# virtualenv = /path/to/env
binary-path  = /usr/local/bin/uwsgi
wsgi-file    = /home/app/wsgi.py
module       = app.wsgi:application
touch-reload = /home/app/reload

plugins      = python34
processes    = 2
threads      = 4

chmod-socket = 664
chown-socket = app:www-data

2). supervisor

To auto start / restart app

sudo apt-get install supervisor

sudo vi /etc/supervisor/supervisord.conf

; supervisord.conf

[program:lroolle]
command                 = /usr/local/bin/uwsgi --ini /home/app/uwsgi.ini
directory               = /home/app
startsecs               = 0
stopwaitsecs            = 0

redirect_stderr         = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 10
stdout_logfile          = /home/app_supervisor.log
autostart               = true
autorestart             = true

sudo supervisord -c /etc/supervisord.conf

sudo supervisorctl -c /etc/supervisord.conf restart app

sudo supervisorctl -c /etc/supervisord.conf restart all

3). nginx

nginx HTTP server, listen 80 port

sudo apt-get install nginx

sudo vi /etc/nginx/sites-available/app.conf

# app.conf

server {
    listen      80;
    server_name lroolle.com;
    charset     utf-8;

# access_log /home/app/log/access.log;
# error_log /home/app/log/error.log;

client_max_body_size 75M;

location /media  {
    alias /home/app/media;
    }

location /static {
    alias /home/app/static;
    }

location /download {
    alias /home/app/download;
    }   

location / {
    uwsgi_pass  unix:///tmp/app.sock;
    include     /etc/nginx/uwsgi_params;
    }
}

sudo ln -s /etc/nginx/sites-available/zqxt.conf /etc/nginx/sites-enabled/zqxt.conf

sudo service nginx configtest

sudo service nginx reload / sudo service nginx restart

4). Pycharm Deployment

Setup FTP server in ubuntu server with vsftpd

sudo apt-get install vsftpd

sudo vi /etc/vsftpd.conf

anonymous_enable=NO
local_enable=YES
write_enable=YES
connect_from_port_20=YES

seccomp_sandbox=NO
allow_writeable_chroot=YES
pasv_enable=Yes
pasv_min_port=30000
pasv_max_port=30100
port_enable=YES

If create new user:

useradd –d /home/ftp virtual

service vsftpd restart

Connect to FTP server

In pycharm

Tools -> Deployment -> Configuration

Choose SFTP!

Not FTP

FTP run out with 500 illegal port

Why? remain to be solved…

NOTE: Make sure to configure the right directory / root path of your project

5). Several Problems

1. uwsgi run with python3

BIG PROBLEM!!

Reason: the apt-get install uwsgi is build uwsgi with the default versio on ubuntu, the python 2.7. (Why not python3 for default !!?? Hum, see what happens when python 4 release…)

Work it out!

  • ⇒ 1st try: pip install uwsgi >>> failed; then pip3 ... >>> failed
  • ⇒ 2nd try: virtualenv -p /usr/bin/python3 env and configure the virtualenv path in uwsgi.ini >>> failed
  • ⇒ 3rd try: plugins = python3 >>> failed, can not find plugins_python3…
  • ⇒ n-th try: apt-get remove uwsgi and pip/pip3 uninstall uwsgi, then whereis uwsgi rm all uwsgi, then build a uwsgi and also python34_plugin.so “hand by hand”! (See References 2)

Finally it works !!

2. uwsgi no sites package

Pay attention to the packages installed on your python2 or python3

3. supervisord

sudo supervisord -c /etc/supervisor/supervisord.conf

Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.

See References 6

4. PyCharm FTP

NO FTP ! USE SFTP!

5. nginx log file

Why this doesn’t work!??

access_log /home/app/log/access.log;
error_log /home/app/log/error.log;

access_log /home/app/log/access_log;
error_log  /home/app/log/error_log;

Are you kidding me !@_@
…To be solved…

References:

  1. http://www.jianshu.com/p/e6ff4a28ab5a
  2. http://bradenmacdonald.com/blog/2015/uwsgi-emperor-multiple-python
  3. http://stackoverflow.com/questions/23872801/running-multiple-uwsgi-python-versions
  4. http://askubuntu.com/questions/286699/uwsgi-with-python3-how-to-get-the-pluginnot-from-apt
  5. http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#installing-uwsgi-with-python-support
  6. http://serverfault.com/questions/114477/supervisor-http-server-port-issue
  7. http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014323392805925d5b69ddad514511bf0391fe2a0df2b0000#0
  8. http://www.ziqiangxuetang.com/django/django-nginx-deploy.html
  9. http://os.51cto.com/art/201008/222036.htm
  10. http://askubuntu.com/questions/358603/vsftpd-illegal-port-command
  11. http://note.mingmings.org/note/2015/pycharm%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/
  12. http://www.ziqiangxuetang.com/django/django-nginx-deploy.html