Set up Django with apache
1. Create Project [ for project structure]
mkdir django_project
cd django_project
virtualenv env
source env/bin/activate
pip3 install django
// Set up static file & media file
// setting.py
//import os
//at the end of file
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
// collect static file
python3 manage.py collectstatic
2. Create site for django
sudo nano /etc/apache2/sites-available/djangoproject.conf
//Add this in conf file with your domain and directory
<VirtualHost *:80>
ServerAdmin admin@djangoproject.localhost
ServerName djangoproject.localhost
ServerAlias www.djangoproject.localhost
DocumentRoot /home/user/django_project
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/user/django_project/static
<Directory /home/user/django_project/static>
Require all granted
</Directory>
Alias /static /home/user/django_project/media
<Directory /home/user/django_project/media>
Require all granted
</Directory>
<Directory /home/user/django_project/my_django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django_project python-path=/home/user/django_project python-home=/home/user/django_project/env
WSGIProcessGroup django_project
WSGIScriptAlias / /home/user/django_project/my_django_project/wsgi.py
</VirtualHost>
3. Do this
cd /etc/apache2/sites-available
//Enable conf
sudo a2ensite djangoproject.conf
// add to domain to host
sudo nano /etc/hosts
// allow firewall
sudo ufw allow 'Apache Full'
//change permission
sudo chmod 664 /home/user/django_project/db.sqlite3
sudo chown :www-data /home/user/django_project/db.sqlite3
sudo chown :www-data /home/user/django_project
//Check apache config
sudo apache2ctl configtest
//restart apache2
sudo service apache2 restart
Ref: https://studygyaan.com/django/how-to-setup-django-applications-with-apache-and-mod-wsgi-on-ubuntu
Comments
Post a Comment