Skip to main content

Serving a Django 1.11 Application Through Apache and mod_wsgi

Create a django project "mysite"

In "mysite" project we're going to change it a bit and add an apache directory which contain three files:


In "mysite" add apache folder structure

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
apache/
__init__.py
override.py
wsgi.py
myapp/
models.py
views.py

The empty __init__.py file tells Python to treat the directory as a package.


Add following content in override.py

# override.py

from mysite.settings import *

DEBUG = True
ALLOWED_HOSTS = ['www.mydomain.com', 'mydomain.com']

Add following content in wsgi.py

#wsgi.py
import os, sys
# Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.apache.override'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Change ownership of apache folder

sudo chown www-data:www-data apache/


Configure Apache Settings
----------------------------------


Add static and wsgi path in 000-default.conf

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



Alias /static /home/pranav/Django/projects/mysite2/static
    <Directory /home/pranav/Django/projects/mysite2/static>
        Require all granted
    </Directory>

    <Directory /home/pranav/Django/projects/mysite2/mysite2/apache>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIScriptAlias / /home/pranav/Django/projects/mysite2/mysite2/apache/wsgi.py

Finally, save and close the file and restart Apache to see the changes:

sudo service apache2 restart



Courtsy : https://www.sitepoint.com/

Comments

Popular posts from this blog

How to create multiple services of Odoo(above version 10) on windows

In this post, I will explain how we can create multiple Odoo services in a Windows Server. Step 1 First, you need to get the executable file from the Odoo link mentioned below Or open the already existing executable file.   https://www.odoo.com/page/download   If you are running the executable file for the second time uncheck Postgres installation since it is already done in the first install. Keep a note of your installation path. Step 2 Go to the below path where "Odoo 12.0 Test" is the directory your new installation files belong to: C:\Program Files (x86)\Odoo 12.0 Test\nssm\win64\ Open the command prompt  and type the below command: nssm install odoo-server-TEST   A User Interface similar to the image below will popup, you have to fill the three empty fields with values I mentioned below Step 3 Path : C:\Program Files (x86)\Odoo 12.0 Test\Python\python.exe Startup directory : C:\Program Files (x86)\Odoo 12.0 Test\server Arguments : "C:\Program Files (x86)\Odoo 1...

How to setup apache web server in linux?

APACHE Apache server is the most commonly used web hosting service,  mainly the websites developed in php , python etc. We can host your website to a local network so that people in same network can access. In Linux Apache use split structure for configuring files, in which it will be defining hostname, port ,ssl etc each in different configuration files. Similarly file sharing can be made possible using apache server. Now we shall try to create a sample website hosted in apache server. Step 1: Install  apache2 in to the system $sudo apt-get update //update the repository for latest version of softwares $sudo apt-get install apache2 Step2: create a directory in '/var/www/' folder which is the default folder for storing site datas. (Var folder is known as variable folder since it is used to store growing folder like log files,backups etc) sudo mkdir -p /var/www/sample.com/public_html Step 3: Grant user permission for the folder $sudo chown -R $U...

Odoo Tips

//create a module sudo odoo scaffold <module_name> Run odoo in server from Command line Run under root user permission python3.6 ./odoo-bin --addons=~/odoo/odoo/addons,~/odoo/pranav/<custom module>,~/odoo/accounting_reports --xmlrpc-port=8085 -d <database name> Run under different user with different DB server(Eg:Odoo) First switch to odoo user: sudo su - odoo -s /bin/bash Run the script ./odoo-bin --addons-path=/opt/odoo/addons,/opt/test_addons --db_host=<ip address> --db_port=<port> --db_password=<password> --database=<db name> -u <user name> --db-filter=<db name> --xmlrpc-port=1122 Nohup-to run a service without writing service file :- nohup python3.6 ./odoo-bin --addons=~/odoo/odoo/addons,~/odoo/pranav/<custom module>,~/odoo/accounting_reports --xmlrpc-port=8085 -d <database name> & ODOO QWEB Datetime operation ------------------------------------ <span  t-set...