backend/ubuntu

ubuntu 14.04 uwsgi, nginx with django

seul chan 2018. 3. 29. 18:53

Install virtualenv

Install pip, virtualenv, virtualenvwrapper

sudo apt-get install -y python-pip python-virtualenv virtualenvwrapper  
sudo apt-get install python-dev

Make virtualenv dirs

mkdir ~/virtualenvs

Add configure to .bashrc

export WORKON_HOME=~/virtualenvs  
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh  
export PIP_VIRTUALENV_BASE=~/virtualenvs  

Create virtualenv

mkvirtualenv mysite
workon mysite

Install nginx

sudo apt-get install nginx

Before add custom nginx file, just comment out default nginx (/etc/nginx/site-avaliable/default)

add nginx conf

In /etc/nginx/site-avaliable , add your site nginx conf. (ex. mysite)

server {
        listen 80;
        server_name your_server_name;
        access_log /var/log/nginx/mysite_access.log;
        error_log /var/log/nginx/mysite_error.log;

        location / {
                uwsgi_pass 0.0.0.0:3031;
                include uwsgi_params;
        }

        location /media/ {
                alias /home/username/mysite/media/;
        }

        location /static/ {
                alias /home/username/mystie/static/;
        }
}

ls tn /etc/nginx/site-enable

sudo ln -s /etc/nginx/site-avaliable/mysite /etc/nginx/site-enable

Uwsgi settings

pip install uwsgi

If needed, install uwsgi-plugin-python

sudo apt-get install uwsgi-plugin-python

need more information, check SO answer

Make project uwsgi file in /etc/uwsgi/apps-avaliable

example of mysite.ini

[uwsgi]
vhost = true
plugins = python
socket = 0.0.0.0:3031
master = true
enable-threads = true
processes = 4
wsgi-file = /home/username/project/path_to/wsgi.py
virtualenv = /home/username/virtualenvs/project
chdir = /home/username/project
touch-reload = /home/username/project/reload

ls to /etc/uwsgi/apps-enable

sudo ln -s /etc/uwsgi/apps-avaliable/mysite.ini /etc/uwsgi/apps-enable

Starting nginx and uwsgi

sudo service nginx start
sudo service uwsgi start