Gaurav
Gaurav

Reputation: 69

Django rest API deploy on apache

I have created a django application and deployed it on the server.I have run the application through :- python manage.py runserver 8000 &

and handle the requests on the apache server through proxy

ProxyPass "/" "http://www.example.com/" ProxyPassReverse "/" "http://www.example.com/".

But there is a issue that I am facing while testing the api through JMeter, when i am running a test case for 10 users my python service over the server gets killed automatically. What i am doing wrong or what i have to do more to resolve the above test scenario,please suggest?

Upvotes: 1

Views: 4928

Answers (2)

R.A.Munna
R.A.Munna

Reputation: 1709

First of all you need to deploy it on other server like apache. Below I am sharing how as usual configuration of apache2 to deploy a python django project.

Apache installation: You need to install apache if not installed yet. Here I am showing the apache2 installation and some other installation which we need.

sudo apt-get update
sudo apt-get install apache2
sudo a2enmod wsgi
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

configuration of 000-default.conf file : Generally the apache2 is located on linux m at path /etc/apache2/sites-available/000-default.conf. And the configuration file may like that.

<VirtualHost *:80>
     ServerName www.something.com

    # this is the document root of your project
    DocumentRoot /path/to/my-project
    # The directory which you can access with base path EX: www.something.com/static/images/image.png
    Alias /static /path/to/my-project/static

    <Directory /path/to/my-project/static>
        Require all granted
    </Directory>

    <Directory /path/to/my-project/my-project>
        <Files wsgi.py>
        Header set Access-Control-Allow-Origin "*"
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess my-project python-home=/path/to/my_env python-path=/path/to/my-project
    WSGIProcessGroup my-project
    WSGIScriptAlias / /path/to/my-project/my-project/wsgi.py

    ErrorLog /path/to/my-project/logs/error.log
    CustomLog /path/to/my-project/logs/access.log combined3
</VirtualHost>

Django project wsgi.py : The django project you created there have a wsgy.py and this file may look like.

python_home = '/path/to/my_env'

import sys
import site

sys.path.append('/path/to/my-project')
sys.path.append('/path/to/my_env/bin')
sys.path.append('/path/to/my_env/lib/python3.6/site-packages')

# Calculate path to site-packages directory.

python_version = '.'.join(map(str, sys.version_info[:2]))
site_packages = python_home + '/lib/python%s/site-packages' % python_version

# Add the site-packages directory.

site.addsitedir(site_packages)

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my-project.settings")

application = get_wsgi_application()

After that you can start you apache2 sever sudo service apache2 start

Also you need to give the permission of you project directory sudo chmod -R 777 /path/to/my-project

This is the basic configuration of apache2 with python django project. Hope this will help to configure any linux machine with your python django project

Upvotes: 1

MaximeK
MaximeK

Reputation: 2061

You cannot using python manage.py runserver 8000 on a server, it's for development ONLY.

You can see the documentation here https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/

A basic conf for Apache would be :

<VirtualHost *:80>
    ServerName yoursite.com
    ServerAdmin [email protected]

    Alias /media/ /path/to/django-app/media/

    <Directory /path/to/django-app/media/>
        Require all granted
    </Directory>

    WSGIScriptAlias / /path/to/django-app/conf/wsgi.py

    <Directory /path/to/django-app/conf/>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

You need to adapt this for your project.

If you need to install mod_wsgi : https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html

For me i would use https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/uwsgi/ it's more convenient (pip install uwsgi)

Upvotes: 0

Related Questions