Tuesday, March 15, 2022

How To Setup Python Django Project With Apache

The mod_wsgi package provides an Apache module that implements a WSGI compliant interface for hosting Python based web applications on top of the Apache web server.




Pre-requites:

  • Django Should  Already  Run Manually
  • Apache Web Server


Step 1) Install & Enable Apache Modulas

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


Step 2) Make New VirtualHost File

$ sudo vim /etc/apache2/sites-available/myproject.conf

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/myproject
        Servername tech2towards.com

        RewriteEngine on
        RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
        RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
        RewriteRule .* ws://127.0.0.1:4022%{REQUEST_URI} [P,QSA,L]

        Alias /static /var/www/html/myproject/myproject/static
        Alias /media /var/www/html/myproject/myproject/media

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

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

    <Directory /var/www/html/myproject/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myproject python-path=/var/www/html/myproject python-home=/var/www/html/env
    WSGIProcessGroup myproject
    WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py

</VirtualHost>



Step 3) Enable VirtualHost & Restart Apache

$ sudo a2ensite /etc/apache2/sites-available/myproject.conf
$ sudo apachectl -t
$ sudo service apache2 restart

  • Check in your Browser

127.0.0.1  or http://tech2towards.com





Example Error Image 




No comments:

Post a Comment

testing