Quantcast
Viewing latest article 2
Browse Latest Browse All 54

Apache2 and Django installation on Debian

Install Apache2

sudo apt-get install apache2 -y
sudo rm -f /var/www/html/index.html && sudo touch /var/www/html/index.html
sudo a2enmod ssl
sudo a2ensite default-ssl
sudo service apache2 restart

Install Python 3

sudo apt-get install python3 -y
sudo apt-get install python3-pip -y
sudo apt-get install libapache2-mod-wsgi-py3 -y
sudo a2enmod wsgi
sudo service apache2 restart

Basic Configuration

WSGIScriptAlias

sudo nano /etc/apache2/sites-enabled/default-ssl.conf

Add the following lines inside ‘VirtualHost’:

WSGIScriptAlias / /var/www/html/mysite/mysite/wsgi.py


Require all granted


WSGIPythonPath

sudo nano /etc/apache2/apache2.conf

Append the following line:

WSGIPythonPath /var/www/html/mysite
sudo service apache2 restart

Basic Configuration – SSL

WSGIScriptAlias

sudo nano /etc/apache2/sites-enabled/default-ssl.conf

Add the following lines inside ‘VirtualHost’:

WSGIScriptAlias / /var/www/html/mysite/mysite/wsgi.py


Require all granted


sudo service apache2 restart

Django Installation

sudo pip3 install Django

Verifying Django Installation

python3
import django
print(django.get_version())
quit()

or

python3 -m django --version

Create new project

cd /var/www/html
sudo django-admin startproject mysite
sudo python3 manage.py migrate

Allowed hosts

cd /var/www/html/mysite
sudo nano mysite/settings

Change ‘ALLOWED_HOSTS = []’ to:

ALLOWED_HOSTS = ['x.x.x.x', 'localhost', '127.0.0.1']

Run the development server (optional)

The following is not needed if running through apache2 and not standalone module. Just visit http://example.com or https://example.com

cd /var/www/html/mysite
python3 manage.py runserver

or

cd /var/www/html/mysite
python3 manage.py runserver 8080

Serving files

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

Add the following lines inside ‘VirtualHost’:

Alias /robots.txt /var/www/html/mysite/static/robots.txt
Alias /favicon.ico /var/www/html/mysite/static/favicon.ico

Alias /media/ /var/www/html/mysite/media/
Alias /static/ /var/www/html/mysite/static/


Require all granted



Require all granted


sudo service apache2 restart

Serving files – SSL

sudo nano /etc/apache2/sites-enabled/default-ssl.conf

Add the following lines inside ‘VirtualHost ‘:

Alias /robots.txt /var/www/html/mysite/static/robots.txt
Alias /favicon.ico /var/www/html/mysite/static/favicon.ico

Alias /media/ /var/www/html/mysite/media/
Alias /static/ /var/www/html/mysite/static/


Require all granted



Require all granted


sudo service apache2 restart

Database – MySQL – phpMyAdmin

sudo apt-get install mysql-server -y
sudo mysql_install_db
sudo /usr/bin/mysql_secure_installation
sudo apt-get install libmysqlclient-dev
sudo pip3 install mysqlclient
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt php5-mysql php-gettext -y
sudo apt-get phpmyadmin -y
sudo service apache2 restart
cd /var/www/html/mysite
sudo nano mysite/settings
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'db_user',
        'PASSWORD': 'db_pass',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
        }
    }
}

Run migration to apply migrations (create db tables, etc)

python3 manage.py migrate

MySQL Database User permissions

If you plan to use Django’s manage.py migrate command to automatically create database tables for your models:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX ON `db_name`.* TO 'db_user'@'localhost';

If you plan to manually create the tables:

GRANT SELECT, INSERT, UPDATE, DELETE ON `db_name`.* TO 'db_user'@'localhost';

Viewing latest article 2
Browse Latest Browse All 54

Trending Articles