CHEMSEDDINE HAROUIT
CHEMSEDDINE HAROUIT

Reputation: 352

Deploy Django Application HTTPD

I wanted to deploy my Django Application and tried to achieve that with Whitenoise , but this last one only serves my static files, now I need to serve my media files. I want to use Apache ( HTTPD in Manjaro/Arch) to do that but couldn't figure it out, after trying several tutos.

Here's my config :

httpd.conf:

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias / /home/chemsouh/dev/rci/rci/wsgi.py
WSGIPythonHome /home/chemsouh/dev/rci
WSGIPythonPath /home/chemsouh/dev/rci/rci

<Directory /home/chemsouh/dev/rci/rci>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

settings.py:

MEDIA_URL = '/media-directory/'
MEDIA_ROOT=os.path.join(os.path.dirname(BASE_DIR), "media-serve/")

I also tried this:

in my httpd-vhosts.conf:

<VirtualHost 127.0.0.1:80>
    ServerAdmin RCI
    DocumentRoot "/home/chemsouh/dev/rci/media-serve"
    ServerName rci.co
    ServerAlias www.rci.co    
    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

in my httpd.conf:

DocumentRoot "/home/chemsouh/dev/rci/media-serve"
<Directory "/home/chemsouh/dev/rci/media-serve">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

And I got this My media directory that I want to serve

My media directory that I want to serve

Here's my project Tree: $pwd gave /home/chemsouh/dev/rci

project tree

N.B I use:

Apache 2.4 Django 2.0.6 in settings there is settings.py

Upvotes: 1

Views: 817

Answers (1)

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

Your django project architecture is strange to me and also i'm not using Manjaro. In my answer i'm using Ubuntu 16.04 you can reproduce it using Manjaro.

Besides of this, here is a simple tutorial of how you can deploy a django project using Apache and Python3+.

First of all, you need to install Apache2 and mod_wsgi for Python3:

$> sudo apt update
$> sudo apt install apache2
$> sudo apt install libapache2-mod-wsgi-py3

Then, i suppose that your Django project is under /var/www/html directory and it is similar to this architecutre:

$> tree -I '__pycache__|migrations'
.
└── deploy_django
    ├── deploy_django
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── django_app
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── manage.py
    ├── media
    └── static

Then, you need to configure your Apache virtual host:

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

And add those lines between <virtualHost> and </virtualHost>:

WSGIDaemonProcess django \ # Your Django's project process name
python-home=/usr/local/lib/python3.5/dist-packages/ \ # Python3 dist packages OR even better use your virtualenv's python path
python-path=/var/www/html/deploy_django # Path of your Django project folder
WSGIProcessGroup django # Your django's Process Group
WSGIScriptAlias / /var/www/html/deploy_django/deploy_django/wsgi.py # Where is your WSGI File 
<Directory /var/www/html/deploy_django/deploy_django>
    Require all granted
</Directory>
Alias /media/ /var/www/html/deploy_django/media/ # Media dir
Alias /static/ /var/www/html/deploy_django/static/ # Static dir

<Directory /var/www/html/deploy_django/static>
    Require all granted
</Directory>

<Directory /var/www/html/deploy_django/media>
    Require all granted
</Directory>

And finally:

$> service apache2 restart

And open 127.0.0.1 in your browser and everything should be okay.

Besides of this, if you encounter some errors, you can check Apache's logs in the file /var/log/apache2/error.log or under the directory /var/log/apache2

Upvotes: 2

Related Questions