Ubuntu, Apache2, python3, django, mod wsgi

Дусал нэвтэрхий толь-с

[Ubuntu18.04 + Apache2.4 + Django2.1 + Python3.6 + wsgi + virtualenv]


Requirement package install

sudo apt-get update
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
sudo pip3 install virtualenv virtualenvwrapper
sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev
pip3 install setuptools --upgrade


Configure a Python virtual enviroiment

cd /var/www/domain.com/web

# upload project or create new:
python3 manage.py startproject firstproject
cd firstproject
python3 -m venv firstprojenv
source firstprojenv/bin/activate
pip3 install django
cd firstproject

# If creating new:
python3 ./manage.py migrate
python3 ./manage.py createsuperuser
python3 ./manage.py collectstatic


configure wsgi.py

Заавал хийхгүй байсан ч болох байх.

import os
import site
import sys

site.addsitedir('/var/www/domain.com/web/firstprojenv/lib/python3.6/site-packages')
sys.path.append('/var/www/domain.com/web/firstproject')

Configure Apache

deactivate
vim /etc/apache2/sites-enabled/firstproject.conf
a2ensite firstproject
<VirtualHost *:80>
  ServerName domain.com

  WSGIDaemonProcess username user=username group=usergroup python-home=/usr python-path=/var/www/domain.com/web

  WSGIProcessGroup username
  WSGIApplicationGroup %{GLOBAL}
  WSGIScriptAlias / /var/www/domain.com/web/firstproject/wsgi.py process-group=username


    Alias /static /var/www/domain.com/web/firstprocject/static
    <Directory /var/www/domain.com/web/fistproject/static>
        Require all granted
    </Directory>

    <Directory /var/www/domain.com/web/firstproject/>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>


Change Owner

sudo chown -R username:usergroup /var/www/domain.com/web/firstproject


Apache restart

sudo service apache2 restart


Зарим хэрэгтэй хаяг

  1. Django Docs
  2. [1]
  3. [2]