Django app cannot be accesed remotely with Apache+mod_wsgi - django

I have the following problem:
I cannot remotely access my django app with Apache+mod_wsgi.
But I can remotely access my django app with the django development server with manage.py runserver 0.0.0.0:8000. And I can locally access my django app with Apache+mod_wsgi on my local computer trough port 80.
So I would like to know why I cannot access remotely trough Apache.
This is my httpd.conf (I have just posted what I modified, everything else is as default.)
Listen *:80
LoadModule wsgi_module modules/mod_wsgi.so
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
WSGIScriptAlias / C:/Users/Ricardo/Dropbox/django_scada/django_scada/apache/wsgi.py
WSGIPythonPath C:/Users/Ricardo/Dropbox/django_scada
Alias /static/ C:/Users/Ricardo/Dropbox/static/
<Directory C:/Users/Ricardo/Dropbox/static/>
Order deny,allow
Allow from all
</Directory>
<Directory C:/Users/Ricardo/Dropbox/django_scada/django_scada/apache>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I have never configured an Apache server so I am not sure what the problem is. I think the Apache+mod_wsgi integreation works fine because I do not have any problem with the local access, so it must be something about the configuration to remotely acccess Apache. When I try to access from another computer, nothing appears on the access log, however it does when locally.
Which steps should I follow to solve my problems?
Thanks in advance!

Did you check the firewall settings? That port 80 is open to the world?

So actually the problem was related with my router and ISP. I realized this when I set up my network on my University instead of my home.
Hope this helps everyone getting stucked.

Related

Host Django Rest API on AWS ec2 using Apache

I have built a very basic API using the Django Rest Framework following this tutorial: https://scotch.io/tutorials/build-a-rest-api-with-django-a-test-driven-approach-part-1
I have confirmed that this works on my local machine by following the instructions of the tutorial. Now I need to host this API on the internet. The easiest way to do this seems to be with AWS using Apache2 on an ec2, which I have attempted to do, but I can't seem to figure out how to make this work. I understand that I need to configure Apache2 for my application, which I have done following this tutorial: http://www.nickpolet.com/blog/deploying-django-on-aws/koAayBejnjdNwYfc6 modifying values as needed. My Apache2 configuration file is located
here: /etc/apache2/sites-enabled/djangorest.conf
and contains the following:
WSGIScriptAlias / /home/ubuntu/API/djangorest/djangorest/wsgi.py
WSGIPythonPath /home/ubuntu/API/djangorest
<Directory /home/ubuntu/API/djangorest/djangorest>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Alias /media/ /home/ubuntu/API/djangorest/media/
Alias /static/ /home/ubuntu/API/djangorest/static/
<Directory /home/ubuntu/API/djangorest/static>
Require all granted
</Directory>
<Directory /home/ubuntu/API/djangorest/media>
Require all granted
</Directory>
My project is housed in the home directory of the ec2 instance like so: ~/API/djangorest/...
From here, shouldn't it be a simple matter of configuring Apache? I've waded through documentation for hours to no avail. When I type the public dns into a browser, I get the following message: "Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request."
This is basically the first project I've ever done that isn't a console application, so It's likely that I'm missing something obvious. In short, I just need to know how to host this API so that I can make calls to it and modify my database. If there's an easier approach than what I'm taking, or if there's a better tutorial to follow, I would appreciate any help.

How to configure Apache to work with Django

I've followed the instructions on Django website for configuring Apache with my Django app on a CentOS 7 server. This included building mod_wsgi from sources to work with the installed python3.4.
Apache restarts without errors but when I hit my app with the URL
http://example.com/myapp/
I get a 503 error like:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.2.15 (CentOS) Server at example.com Port 80
I'm not sure how I troubleshoot what's wrong here. Can anyone help?
Details of the config:
My django app lives at /mnt/net/django/myapp
I've added the file wsgi.conf to my apache conf.d directory and it looks like this:
#LoadModule wsgi_module modules/mod_wsgi.so
# use python34 pip installes mod_wsgi
LoadModule wsgi_module "/usr/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
#WSGIPythonHome "/usr"
Alias /robots.txt /mnt/net/django/myapp/static/robots.txt
Alias /favicon.ico /mnt/net/django/myapp/static/favicon.ico
Alias /media /mnt/net/django/myapp/media/
Alias /static/ /mnt/net/django/myapp/static/
<Directory /mnt/net/django/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /mnt/net/django/myapp/media>
Order deny,allow
Allow from all
</Directory>
# Allows URLs like example.com/myapp to forward to django
WSGIScriptAlias /myapp /mnt/net/django/myapp/myappsite/wsgi.py process-group=example.com
# Use the virtual env for the myapp site
#WSGIPythonHome /mnt/net/django/myapp/env-myapp-py3-4
# Need to use WSGIDaemon
WSGIDaemonProcess example.com python-home=/mnt/net/django/myapp/env-myapp-py3-4 python-path=/mnt/net/django/myapp
#WSGIPythonPath /mnt/net/django/myapp
<Directory /mnt/net/django/myapp/myappsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
How to use Django with Apache and mod_wsgi -- follow this: I have done it myself many times, it is very straight forward.
Tip: Create a document and record everything you do whilst setting this up, this way if it doesn't work then you can retrace your steps, but if it does work... Great, you have your very own guide to setting up an Apache server for Django.
Solved my problem (mostly)
The problem is that mod_wsgi with a daemon process tries to write a socket file into the apache logs directory and permissions are denied.
Solution is to tell apache another place to write the socket like this:
WSGISocketPrefix /var/run/wsgi

AH01630: client denied by server configuration WAMP

I have seen alot alot of guides how to fox the AH01630 error and some httpd corrections and tweaks.
Surely some will be annoyed me asking for help on this specific topic but I was trying to figure out for hours how to fix my issue.
What did I do?
I have port-forwarded the required ports for Apache and MySQL
I installed WAMP on my computer
I tried to config. httpd
I read about 50 topics according to make my server public accessible
-
I tried to set up the whole thing on a fresh virtual machine
I reinstalled WAMP a few times
So what doesn't work?
The usual access refuse message: Forbidden You don't have permission to access / on this server.
-I found this in the error log: AH01630: client denied by server configuration
I have tried to fix the issue by my own and couldn't proceed further after hours, maybe I forgot a little detail or just something I have to know but I don't because I am uneducated in this specific area
Thanks in advance
Here are the logs and config, if you don't mind and know how to fix the problem send me the finished file
httpd.conf
apache_error.log http:// pastebin. com/YSZDc0tp
access.log http:// pastebin. com/xfwv5ebB
In WAMPServer 3+ there is a Virtual Host defined for localhost by default, and that is where you should make the access amendments and not in httpd.conf
So in httpd.conf replace
# onlineoffline tag - don't remove
Require all granted
</Directory>
With
# onlineoffline tag - don't remove
Require local
</Directory>
Edit \wamp\bin\apache\apache\apache2.4.18\conf\extra\httpd-vhosts.conf and replace Require local with Require all granted
EG
#
# Virtual Hosts
#
<VirtualHost *:80>
ServerAdmin webmaster#homemail.net
ServerName localhost
DocumentRoot E:/wamp/www
<Directory "E:/wamp/www/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Django project doesn't show up with Apache and mod_wsgi

I've installed Apache and mod_wsgi on windows xp service pack 3 and added these line to my httpd.conf :
WSGIScriptAlias / "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/wsgi.py"
WSGIPythonPath "C:/Documents and Settings/X/My Documents/Downloads/Foo"
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
but when I open localhost on my firefox, it shows Apache's It Works! message, what should I do to run my project on localhost ?
EDIT :
I checked and recognized that my project's path is not included in PYTHONPATH. Isn't the line WSGIPythonPath ... expected to add the address to PYTHONPATH ?
Alright, so my setup is in linux so this is not tested on windows, but:
I did not see your LoadModule statement
File: httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
modwsgi wont work without that.
Also: the your grant statement seems a bit suspicious.
In the wsgi configuration guide suggests using a Directory directive for allowing this access to your mod_wsgi application.
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/">
Order allow,deny
Allow from all
</Directory>
Finally:
Make your life easy down the road.
configure apache in worker mode
configure mod_wsgi in daemon mode.
profit
Might I suggest watching this PyCon talk Making Apache suck less for hosting Python web applications from 'the-man' Graham. I wish I knew all of that stuff years ago.
Note: To figure out if you have apache in mpm worker mode.
httpd.exe -V
look for the "Server MPM" value of worker.
Django runs on port 8000 so you'll want to do two things. First, you need to run the server by entering into your console python manage.py runserver. Second, you need to direct your browser to localhost:8000.
As an aside, you don't need Apache to run a simple, local development environment. Django has its own server built in that you can leverage.

how to serve website with apache over the internet?

I have somehow managed to serve both my project app and its static files on the apache. But only I can see my webpage, by typing localhost and by my IPv4 address. And I can't see my webpage from the other's computer. In my http.conf, it is Listen 80. I don't know much about this. I even registered on a free dynamic DNS provider, but even from that url I can only see It works message. I really suck at these things. Please guide me here. Thank you.
snippet of http.conf:
WSGIScriptAlias / C:/Users/robin/web/etc/etc/etc/wsgi.py
WSGIPythonPath C:/Users/robin/web/etc/etc
<Directory C:/Users/robin/web/etc/etc>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
#Alias /robots.txt /path/to/mysite.com/static/robots.txt
#Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
AliasMatch ^/([^/]*\.css) C:/Users/robin/web/etc/etc/static/styles/$1
#Alias /media/ /path/to/mysite.com/media/
Alias /static/ C:/Users/robin/web/etc/etc/static/
<Directory C:/Users/robin/web/etc/etc/static>
Order deny,allow
Allow from all
</Directory>
#<Directory /path/to/mysite.com/media>
#Order deny,allow
#Allow from all
#</Directory>
WSGIScriptAlias / C:/Users/robin/web/etc/etc/etc/wsgi.py
<Directory C:/Users/robin/web/etc/etc/etc>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
You need to do the following:
1. Ensure that the server is publicly accessible
To do this you need to ensure
Port 80 is routed through your router to the servers internal IP address. This is called NAT. If you are using a Cisco router you can see the instructions here.
Any firewalls are configured not to block the traffic on port 80. If you are using Linux you would most likely need to configure iptables. If you are using Windows you can do this on the windows firewall. If you are using a third party firewall you need to search google to find out how to do it.
You need to ensure you are using the public ip address to connect. You can find this by using a website such as http://whatismyipaddress.com/
2. Setup the virtual hosts on apache
The reason you are only seeing the "It Works" message is because Apache is pointing to the default web root on the server for that domain name. To fix this, you need to setup a virtualhost for the domain name, and point it to the root directory of your application. Instruction for this can be found here.
Under for virtual hosts in your apache config, you could add something like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin [email]#[address].com
DocumentRoot [directory root]
ServerName [registered domain name without www eg: something.com]
ServerAlias *.[registered domain name without www]
</VirtualHost>
The "NameVirtualHost *:80" might already exist, so search the config first. Usually it is in there and commented out by default, so you might just need to remove the #.
Note: ALWAYS backup the config by copying the contents to another file. Also restart apache after you make the adjustment.