Zia
Zia

Reputation: 277

Deploying Django on Apache and WSGI

Although I have found a bunch of tech support to deploy Django over Apache using WSGI but infact they all have confused me unfortunately, and I couldn't get the Django running. I hope this to be real easy job but being a new comer I am facing difficulties.

I have two Django projects namely website1 and website2 inside my /home/zia/Documents/Codes/Django/website1 and ..../website2 dir, respectively. The folder containing settings.py file is root/ inside the /website1 and /website2 dir.

Apache, mod_wsgi everything is installed as required. How to edit apache2.conf and wsgi.py file to keep these two projects running over port 8080 and 8081?

I am struggling with this issue for past few days and have tried all the following websites.

link1,link2,link3,link4


UPDATE1:

I have followed the following approach right from the beginning to make things going well but found myself in some new issues. Kindly guide me where I am wrong.

  1. Installing mod-wsgi and apache2: sudo apt-get install libapache2-mod-wsgi && sudo apt-get update && sudo apt-get install apache2
  2. Edit the apache2 port to 8083, instead of 80 by altering file "/etc/apache2/ports.conf": Listen 8083
  3. Add the following line into "/etc/hosts" file: 160.75.133.175 160.75.133.175
  4. Edit the following code in the "/etc/apache2/apache2.conf" file:

<Directory /> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

  1. Create a file inside "/etc/apache2/sites-available/" dir with name "sql_api.conf":

<VirtualHost *:8083> ServerAdmin [email protected] ServerName 160.75.133.175 ServerAlias http://160.75.133.175 <Directory /home/zia/Documents/Codes/Django/sql_api/ > Order deny,allow Allow from all </Directory> WSGIScriptAlias / /home/zia/Documents/Codes/Django/sql_api/root/wsgi.py WSGIDaemonProcess 160.75.133.175 user=www-data group=www-data threads=25 python-path=/home/zia/Documents/Codes/Django/sql_api/root/:/usr WSGIProcessGroup 160.75.133.175 ErrorLog /home/zia/Documents/Codes/Django/sql_api/root/error.log </VirtualHost>

  1. Run the following commands being in "/etc/apache2/sites-available" dir: sudo a2enmod wsgi && sudo a2ensite sql_api.conf && sudo service apache2 restart
  2. Open http://160.75.133.175:8083/ but getting the following error: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Apache/2.4.7 (Ubuntu) Server at 160.75.133.175 Port 8082

NOTE: When I am making a Django project in /var/www/ dir and then doing the same approach then working just fine! I think because I am trying to access /home/zia/.... dir, there is this issue. Anyways, this is just a guess. I would appreciate your help.

Upvotes: 4

Views: 3383

Answers (2)

Zia
Zia

Reputation: 277

Thanks to everyone. Finally found a working procedure. Follow the following steps in order:

  1. Installing mod-wsgi and apache2: sudo apt-get install libapache2-mod-wsgi && sudo apt-get update && sudo apt-get install apache2
  2. Edit the apache2 port to 8083, instead of 80 by altering file "/etc/apache2/ports.conf": Listen 8083
  3. Add the following line into "/etc/hosts" file: 160.75.133.175 160.75.133.175
  4. Edit the following code in the "/etc/apache2/apache2.conf" file:

<Directory /> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

  1. Create a file inside "/etc/apache2/sites-available/" dir with name "sql_api.conf" (make as many .conf files you want with different names, each serving different website):

<VirtualHost *:8083> ServerAdmin [email protected] ServerName 160.75.133.175 ServerAlias http://160.75.133.175 <Directory /home/zia/Documents/Codes/Django/sql_api/ > Order deny,allow Allow from all </Directory> WSGIScriptAlias / /home/zia/Documents/Codes/Django/sql_api/root/wsgi.py WSGIDaemonProcess 160.75.133.175 user=www-data group=www-data threads=25 python-path=/home/zia/Documents/Codes/Django/sql_api/root/:/usr WSGIProcessGroup 160.75.133.175 ErrorLog /home/zia/Documents/Codes/Django/sql_api/root/error.log </VirtualHost>

  1. Add the following lines in the wsgi.py file inside "/home/zia/Documents/Codes/Django/sql_api/root/": sys.path.append('/home/zia/Documents/Codes/Django/sql_api/root') sys.path.append('/home/zia/Documents/Codes/Django/sql_api')
  2. Run the following commands being in "/etc/apache2/sites-available" dir: sudo a2enmod wsgi && sudo a2ensite sql_api.conf && sudo service apache2 restart
  3. Open http://160.75.133.175:8083/

Upvotes: 3

Chris Hawkes
Chris Hawkes

Reputation: 12410

you should probably just start over if you made a bunch of changes to your Apache config. I'm most familiar with setups under Ubuntu.

What you need to look to do is setup both sites under apache as a virtual host. After installing apache there is a folder called sites-available and sites-enabled they should contain the virtual host files with the names of your website projects. Each virtual host will point to whereever your .wsgi file is located. these virtual hosts typically listen under the same port number (as Daniel mentioned above) but serve whichever app is requested based on the domain name. noobmovies.com google.com ect...

how to setup a virtual host with apache is pretty well explained here. this assumes you're using ubuntu though.

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

your virtual host (the file should be named after your domain exp... noobmovies.com) and will look something like this...

**<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName  www.yourdomain.com
    ServerAlias yourdomain.com

    <Directory /home/path/your/project/ >
            Order deny,allow
            Allow from all
    </Directory>

    WSGIScriptAlias / /home/path/your/project/app/wsgi.py
    WSGIDaemonProcess  yourdomain.com user=www-data group=www-data threads=25 python-path=/path/to/your/project/app/:/path/to/python/virtual/host/site-packages
    WSGIProcessGroup yourdomain.com
    ErrorLog /path/to/your/app/error.log
</VirtualHost>**

keep in mind the WSGIDaemonProcess is only if you're running your app using virtualenv (which you should). this tells apache where python is that should be used to read the wsgi app/run django app.

So if you're using ubuntu or linux you may just want to uninstall apache and reinstall then just follow the digital ocean instructions to get setup.

Upvotes: 2

Related Questions