2019年3月7日 星期四

Delopy Django project on Ubuntu 1804 with mod_wsgi

1. Install apache2 & postgresql
2. Install mod_wsgi
# sudo apt install libapache2-mod-wsgi-py3
3. Create a Python3 virtualenv
# mkvirtualenv -p $(which python3) django & psycopg2-binary
(django)# pip install django
(django)# pip install psycopg2-binary
4. Enter into project folder. /home/jack/Documents/Projects/myproject
5. Install Requirement python package
(django) # pip install -r requirements.txt
6. Note that the wsgi.py is put on /home/jack/Documents/Projects/myproject/myproject
7. Edit settings.py in ./myproject
Add ALLOWED_HOSTS = ['*',]
(This is risky)
Add STATIC_ROOT = '/home/jack/Documents/Projects/myproject/static'
8. Setup Postgresql database correctly & create a database in it.
9. Edit settings.py to meet the database you want to use.
10. Database Migrate & collectstatic
(django) # python manage.py migrate
(django) # python manage.py collectstatic
11. create a new conf in /etc/apache2/site-available myproject.conf with sudo

    ServerName myproject
    Alias /robots.txt /home/jack/Documents/Projects/myproject/static/static/robots.txt
    Alias /favicon.ico /home/jack/Documents/Projects/myproject/static/favicon.ico
    Alias /static/ /home/jack/Documents/Projects/myproject/static/
   
        Require all granted
   
    WSGIDaemonProcess django python-home=/home/jack/.virtualenvs/django/ python-path=/home/jack/Documents/Projects/myproject/
    WSGIScriptAlias / /home/jack/Documents/Projects/myproject/myproject/wsgi.py
    WSGIProcessGroup django
   
       
        Require all granted
       
   

12. Edit /etc/apache2/ports.conf, Add a line
Listen 4002
13. Enable the site & reload
# sudo a2ensite myproject
# sudo systemctl reload apache2
14. Allow port 4002 in Firewall
# sudo ufw allow 4002


Reference:
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

沒有留言:

張貼留言