https://github.com/guycole/django-lab
Django Experiments
https://github.com/guycole/django-lab
aws-ec2 django-application postgresql python3
Last synced: 25 days ago
JSON representation
Django Experiments
- Host: GitHub
- URL: https://github.com/guycole/django-lab
- Owner: guycole
- Created: 2018-08-25T09:55:54.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-02T07:24:56.000Z (almost 8 years ago)
- Last Synced: 2025-04-06T17:52:15.048Z (about 1 year ago)
- Topics: aws-ec2, django-application, postgresql, python3
- Language: Python
- Size: 2.08 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# django-lab
A simple demonstration of a Django/PostGreSQL/EC2 deloyment. Run time
economy is the primary goal. Most examples focus on a dejango deployment
via Elastic Beanstalk and connected to RDS, nice but managed
services are pricey. Many applications run well enough w/the database
and web server on the same EC2 instance. This project demonstrates a
minimal deployment to AWS LINUX hosted on a single EC2 server.
This is not a beginners tutorial. Not so advanced either, but assumes
you know AWS EC2, UNIX and django.
The django project is named "mvp" and consists of two simple applications
("app1" and "app2") because I wanted to investigate navigating between
multiple applications.
I created an account "django" to hold the django application. httpd
(apache) will use uwsgi as a bridge to django. "django" (the UNIX account)
will need to be a member of the apache (UNIX) group.
## EC2 (deployment environment)
1. Start an EC2 instance. Original MVP development was on a t2.small
instance using a LINUX2 AMI w/a 8GB EBS file system.
1. Install these packages (as root)
1. `yum -y groupinstall 'Development Tools'`
1. `yum -y install python3 python3-devel httpd-devel`
1. `yum -y install postgresql postgresql-server`
1. Configure PostGreSQL
1. `service postgresql initdb` (as root)
1. edit /var/lib/pgsql/data/postgresql.conf
1. enable a listener on localhost by uncommenting
1. listen_addresses = 'localhost'
1. port = 5432
1. edit /var/lib/pgsql/data/pg_hba.conf
1. trust users on the loopback
1. local all all trust
1. host all all 127.0.0.1/32 trust
1. host all all ::1/128 trust
1. start postgresql
1. `service postgresql start` (as root)
1. Verify postgres is working by logging in
1. `psql -d template1 -U postgres`
1. `\q` will exit
1. Start httpd and ensure you can connect to it
1. `service httpd start` (as root)
1. visit your public IP address w/a browser, should see Apache test page
1. Install virtualenv (as root)
1. `pip3 install virtualenv`
1. Create a LINUX user account 'django' to hold the project, and add to apache group
1. `useradd -m django` (as root)
1. `usermod -a -G apache django` (as root)
1. `chown -R django:apache /var/www` (as root)
1. `sudo chmod 2775 /var/www` (as root)
1. su to django
1. pull sources from github
1. `git clone https://github.com/guycole/django-lab.git`
1. create postgresql user and database
1. run postgres/genesis.sh (genesis.sql contains postgresql operations)
1. verify happy postgres account and database creation
1. `psql -U django -d mvp` (there are no tables yet)
1. `\q` will exit
1. establish virtualenv and seed environment
1. `cd` (return to home directory)
1. `virtualenv -p /usr/bin/python3 venv`
1. `source venv/bin/activate`
1. `pip install -r requirements.txt`
1. Check for happy django startup
1. `python manage.py runserver` (Control C to exit)
1. Run migrations and ensure ok database connection
1. `python manage.py migrate`
1. Collect static images (i.e. files)
1. `python manage.py collectstatic`
1. Edit mvp/settings.py and tweak ALLOWED_HOSTS to reflect your EC2 public IP
1. build and install mod_wsgi
1. latest sources are at https://github.com/GrahamDumpleton/mod_wsgi/releases
1. `cd` (return to home directory)
1. `curl https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.6.4 --output mod_wsgi.tgz`
1. `tar -xvzf mod_wsgi.tgz; cd mod_wsgi-4.6.4; ./configure; make`
1. exit (back to UNIX root shell), `cd /home/django/mod_wsgi-4.6.4; make install`
1. configure httpd
1. edit /etc/httpd/conf/httpd.conf (as root)
```
WSGIScriptAlias / /home/django/django-lab/mvp/mvp/wsgi.py
WSGIPythonHome /home/django/venv
WSGIPythonPath /home/django/django-lab/mvp
Require all granted
Alias /static/ /home/django/django-lab/mvp/static/
Require all granted
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
```
1. ensure file permissions are ok
1. `chmod 750 /home/django`
1. `chgrp -R apache /home/django`
1. restart httpd
1. `service httpd restart`
1. ensure mod_wsgi is loaded
1. `httpd -M | grep -i wsgi`
1. Visit your EC2 instance w/a browser and you should see the splash page.
1. URL should be IP address/mvp/app1
1. Visit IP address/mvp/app1/alpha/create/ to create a row in database
1. Note the URL changes after submit
1. Visit IP address/mvp/app1 and note that row is visible (click through row to edit)
1. Inspect urls.py for other features.