Server error with Apache, Gunicorn, and Django - django

I am getting a server error with trying to reverse proxy to Gunicorn.
My virtual host file looks like this:
<virtualhost *:80>
<Location /myApp>
ProxyPreserveHost On
ProxyPass "http://127.0.0.1:8090/"
ProxyPassReverse "http://127.0.0.1:8090/"
</Location>
</virtualhost>
And I'm running Gunicorn like this from within my Django project directory:
gunicorn -b 127.0.0.1:8090 -w 3 myApp.wsgi
Basically I'm wanting to reverse proxy requests to Gunicorn because I can't use mod_wsgi as it can't be compiled for the version of python I am running on my distro of Linux.

I had to do ProxyPass "/server" "http://127.0.0.1:8005/" to get this kind of thing to work it didn't like passing on the root /

Related

How to configure apache2 on ubuntu for django restful services

I have django restful services on my ubuntu server which are running on port 84.
When the request is send, it come through the apache2 server which is running on port 80.
Now let say my server ip is "xx.xx.xx.10" and when i call with this url http://xx.xx.xx.10/user where user is the rest service running on django rest framework on port 84. Then the request should go to my django rest service through apache2 and return the desired output.
I tried by using the below configuration in the apache2 :-
<VirtualHost *:80>
WSGIScriptAlias / /myproject/myapp/test/wsgi.py
<Directory "/myproject/myapp/test/">
<Files "wsgi.py">
Require all granted
</Files>
</Directory>
</VirtualHost>
but this is giving internal error saying that test.settings does not exits.
is this the right way to configure the restful services running on different port on apache server.
Did you specify a python path? If you don't, import mysite will not work.
WSGIPythonPath /path/to/mysite.com
For more info see: Django docs modWSGI
I have solved this issued by using the below configurations :-
<VirtualHost *:80>
ServerName mydomain
ServerAlias *.mydomain
ProxyRequests off
ProxyPass / http://localhost:84/
ProxyPassReverse / http://localhost:84/
</VirtualHost>

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.
Installing mod-wsgi and apache2:
sudo apt-get install libapache2-mod-wsgi && sudo apt-get update && sudo apt-get install apache2
Edit the apache2 port to 8083, instead of 80 by altering file "/etc/apache2/ports.conf": Listen 8083
Add the following line into "/etc/hosts" file: 160.75.133.175 160.75.133.175
Edit the following code in the "/etc/apache2/apache2.conf" file:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Create a file inside "/etc/apache2/sites-available/" dir with name "sql_api.conf":
<VirtualHost *:8083>
ServerAdmin zia#gmail.com
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>
Run the following commands being in "/etc/apache2/sites-available" dir: sudo a2enmod wsgi && sudo a2ensite sql_api.conf && sudo service apache2 restart
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 zia#gmail.com 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.
Thanks to everyone. Finally found a working procedure. Follow the following steps in order:
Installing mod-wsgi and apache2:
sudo apt-get install libapache2-mod-wsgi && sudo apt-get update && sudo apt-get install apache2
Edit the apache2 port to 8083, instead of 80 by altering file "/etc/apache2/ports.conf": Listen 8083
Add the following line into "/etc/hosts" file: 160.75.133.175 160.75.133.175
Edit the following code in the "/etc/apache2/apache2.conf" file:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
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 zia#gmail.com
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>
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')
Run the following commands being in "/etc/apache2/sites-available" dir: sudo a2enmod wsgi && sudo a2ensite sql_api.conf && sudo service apache2 restart
Open http://160.75.133.175:8083/
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 your_admin_email#gmail.com
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.

Configureing mod_wsgi on Apache2 for Django project

I am having some issues in configuring the various elements of a mod_wsgi setup. This is my fist time using mod_wsgi so I have been following several tutorials, the main one being a YouTube video Install Django on Apache with mod_wsgi on Linux . From what I understand after following the below steps I should at least see the Django 'It Works' page.
Setup
Ubunto 12.04
Apache 2.2.22
Python 2.7.3
Django 1.6
I have created a WSGI script file firstweb.wsgi in my home directory
/home/firstweb.wsgi
Its contents are
import os
import sys
sys.path = [‘/var/www/firstweb’] + sys.path
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘firstweb.settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I then created a host file for the project located at
/etc/apache2/sites-available/firstweb.conf
Its contents are
<VirtualHost *:80>
ServerAdmin webmaster#firstweb.com
ServerName www.firstweb.com
ServerAlias firstweb.com
WSGIScriptAlias / /home/firstweb.wsgi
Alias /static /var/www/firstweb/static/
<Directory /var/www/firstweb/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I then enabled the configuration file with the command
a2ensite firstweb.conf
I then started my django project.
I went to /var/www/ and used the Django start project command
django-admin.py startproject firstweb
I then restarted my server
sudo service apache2 restart
Lastly I then reconfigured my Apache hosts file to point the domain firstweb.com to my new, local, Django project
/etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
134.226.38.147 firstweb.com
So with all of this done when I visit www.firstweb.com I should see the Django "It Works!" page
Can anyone tell me what I'm doing wrong?
You haven't listed 'www.firstweb.com' in your /etc/hosts file. Both hosts listed under ServerName and ServerAlias must have a resolvable IP address.

How to enable SSL support using Apache + Fastcgi + Django?

I have written a Django App and I want to enable SSL(Https) support in my deployment. Currently my web application is properly served on port80. This is my setup
I am using Apache as my web server
FastCGI as reverse proxy to serve my web app
I launch my Django app using the following command
/usr/bin/python manage.py runfcgi daemonize=false method=threaded host=127.0.0.1 port=8080
I edited the Apache httpd.conf file accordingly
<VirtualHost _default_:80>
Alias /static /opt/tms/web3/static
#For every request that *doesn't* start with "static", send it
#to Django via fastcgi. The address and port must match the
#arguments that are later passed to Django.
<LocationMatch "^/(?!static)">
ProxyPass fcgi://127.0.0.1:8080/
</LocationMatch>
</VirtualHost>
<VirtualHost _default_:443>
Alias /static /opt/tms/web3/static
#For every request that *doesn't* start with "static", send it
#to Django via fastcgi. The address and port must match the
#arguments that are later passed to Django.
<LocationMatch "^/(?!static)">
ProxyPass fcgi://127.0.0.1:8080/
</LocationMatch>
LogLevel warn
ErrorLog /var/log/web_error_log
LogFormat combined
CustomLog /var/log/web_access_log combined
SSLEngine on
SSLCertificateFile /var/opt/tms/web/conf/webserver.cert.pem
SSLCertificateKeyFile /var/opt/tms/web/conf/webserver.priv.pem
SSLCertificateChainFile /var/opt/tms/web/conf/webserver_chain.cert.pem
SSLOptions +StdEnvVars
SSLProtocol -all +TLSv1 +TLSv1.1 +TLSv1.2
SSLCipherSuite HIGH:-aNULL:-kKRB5:-MD5
FileETag None
</VirtualHost>
and I get this error when I access with https
Unable to make a secure connection to the server. This may be a problem with
the server, or it may be requiring a client authentication certificate that you don't have.
Error code: ERR_SSL_PROTOCOL_ERROR

uwsgi + apache + django

I am running apache (mod_uwsgi) with uwsgi. in settings i have unix socket pointing to /var/uwsgi/ . I am also pointing to this socket from apache vhost. If i try to access this vhost, i got error (in apache log):
apache log:
uwsgi: unable to connect to uWSGI server: Permission denied
uwsgi config:
<uwsgi>
<pythonpath>/home/user/django_projects/project/</pythonpath>
<master/>
<no-orphans/>
<processes>1</processes>
<optimize>0</optimize>
<home>/home/user/Envs/project/</home>
<limit-as>128</limit-as>
<chmod-socket>664</chmod-socket>
<gid>www-data</gid>
<pidfile>/var/uwsgi/project.pid</pidfile>
<socket>/var/uwsgi/project.sock</socket>
<wsgi-file>/home/user/django_projects/project/deploy/wsgi-sites/production.py</wsgi-file>
<daemonize>/var/uwsgi/project.log</daemonize>
<chdir>/home/user/django_projects/project/</chdir>
</uwsgi>
and apache config:
<Location />
Options FollowSymLinks Indexes
SetHandler uwsgi-handler
uWSGISocket /var/uwsgi/project.sock
</Location>
what am i missing? I also tried changing chmod socket to 777..no success..
I run mod_wsgi with django, but consider adding a Directory block to the Apache config.
<Directory /var/uwsgi/>
Options All
</Directory>
And if that works, make the "All" statement more specific to your needs.