Thursday, March 7, 2019

Prepare Apache server for django deploy




Go inside default apache www directory ( ie     /var/www/html/   )
cd /var/www/html/
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

sudo pip3 install virtualenv

mkdir ~/myproject
cd ~/myproject

virtualenv myprojectenv
source myprojectenv/bin/activate

Note
Regardless of whether you are using Python 2 or Python 3, when the virtual environment is activated, we should use the pip command (not pip3).
later when work with this project you should activate the virtual env using command "source myprojectenv/bin/activate", virtualenv  keep the dependencies for each project separate.

pip install django

django-admin.py startproject myproject .


nano myproject/settings.py

. . .
ALLOWED_HOSTS = ["server_domain_or_IP"]
. . .

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser

Configure Apache

sudo nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    . . .

    Alias /static /var/www/html/myproject/static
    <Directory /var/www/html/myproject/static>
        Require all granted
    </Directory>

    <Directory /home/sammy/myproject/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myproject 
                                  python-home= /var/www/html/myproject/myprojectenv 
                                  python-path= /var/www/html/myproject/
    WSGIProcessGroup myproject
WSGIScriptAlias / /home/sammy/myproject/myproject/wsgi.py

</VirtualHost>



After update apache configuration validate it using the next command
sudo apache2ctl configtest

sudo systemctl restart apache2










No comments: