Skip to content

Instantly share code, notes, and snippets.

@suhailvs
Last active September 25, 2017 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suhailvs/fd810e8b59236d80747d to your computer and use it in GitHub Desktop.
Save suhailvs/fd810e8b59236d80747d to your computer and use it in GitHub Desktop.
How to Run Django with mod_wsgi and Apache with a virtualenv

guide

0. ssh into digitalocean

ssh root@104.131.108.244

1. update and install

install apache, git,mod-wsgi,mysql and some python packages such as pip,virtualenv and python-dev

apt-get update
apt-get install apache2 python-pip python-virtualenv git libapache2-mod-wsgi mysql-server libmysqlclient-dev python-dev

start mysql

service mysql start

2. create a venv and clone repo

cd /home
mkdir mysite
cd mysite
virtualenv myenv
source myenv/bin/activate
git clone https://github.com/suhailvs/django_samples www

3. install requirements and runserver

cd www
pip install -r requirements.txt
./manage.py runserver 0.0.0.0:8000

Now visit you server IP address:8000. You must see your django page!

4. pair django with apache server

Create a specific site sampleapp

vi /etc/apache2/sites-available/sampleapp.conf

add below line to that file:

<VirtualHost *:80>
ServerName staging.djangoer.com
DocumentRoot /home/mysite/www

WSGIDaemonProcess sampleapp python-path=/home/mysite/www:/home/mysite/myenv/lib/python2.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias / /home/mysite/www/django_sample/wsgi.py
Alias /static /home/mysite/www/static

ErrorLog /home/mysite/error.log
CustomLog /home/mysite/access.log combined

<Directory /home/mysite/www>
Require all granted
</Directory>

</VirtualHost>

enable it and restart apache

a2ensite sampleapp
service apache2 reload

permission problem fix

# /var/www$ django-admin.py startproject fileupload 
Listen 8001
<VirtualHost *:8001>
    
    WSGIDaemonProcess sampleapp python-path=/var/www/fileupload:/root/djenv/lib/python3.5/site-packages
    WSGIProcessGroup sampleapp
    WSGIScriptAlias / /var/www/fileupload/fileupload/wsgi.py
    
    ErrorLog /root/apache_fileupload.log
    
    <Directory /var/www/fileupload>
        Require all granted
    </Directory>

</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment