Django EC2 deployment with wsgi and Apache - python

I have a micro instance on EC2 with Django installed there. I've also installed mod-WSGI, PostgreSQL, etc following several tutorials.
Finally, I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is the default Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.
my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my .conf file stored in /etc/apache2/httpd.conf:
Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/
<VirtualHost *:80>
DocumentRoot /home/ubuntu/my_project
ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ErrorLog /home/ubuntu/my_project/apache/error_log
CustomLog /home/ubuntu/my_project/access_log_common
WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
Alias /static/ /home/ubuntu/my_project/static/
<Directory /home/ubuntu/my_project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When I'm trying to
/etc/init.d/apache2 stop
and instead:
python manage.py runserver 0.0.0.0:80
It works fine and I'm able to connect to my app from a browser with IP as URL.
It gives me understanding that the setting.py file is fine (am I right?) and the problem is or in wsgi.py or httpd.conf.
When I stop my development server and start Apache again, I get the default Apache page. Can you help me please to find what's wrong with my files?
Permissions for apache:
ubuntu#ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/

You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.
You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.
It is bad security practice to set DocumentRoot to be where your source code for your application is.
Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.
Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.

Related

403 forbidden using a flask app with WSGI and Apache2.4

I'm trying to run a dash app on our website in a sub-directory. I'm getting a 403 forbidden when trying to access to website.
In the /etc/httpd/logs/ssl_error_logs i get the error:
[Tue Jan 03 13:53:42.973046 2023] [autoindex:error] [pid 50576] [client 134.94.71.5:60550] AH01276: Cannot serve directory /var/www/Typesetter/appname/: No matching DirectoryIndex (index.html,index.php,index.php) found, and server-generated directory index forbidden by Options directive
I think i might have something wrong with either the permissions or the virtualhost setup. But i can't figure out what is going wrong.
I want to run the dashboard on: our-group.our-uni.com/appname
The server is running on centos7.
I got a data folder with biochemical information to provide to the users in /var/www/html/appname/data/ from which i can download the data if i go to our-group.our-uni.com/appname/data/datapoint1.pt.
Since this works and i can also put a index.html/index.php page there without issues, i think my virtualhost setup pointing to WSGI is incorrect?
The error says cannot serve directory /var/www/Typesetter/appname/, but this directory is a symbolic link to /var/www/html/appname/.
The virtualhost file appname.conf located in /etc/httpd/conf.d/:
<VirtualHost *:80>
ServerName our-group.our-uni.com
DocumentRoot /var/www/html/appname
Errorlog /var/www/html/appname/appname-error.log
Alias /topenzyme /var/www/html/topenzmye
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appname-env
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks ExecCGI
Require all granted
</Directory>
</VirtualHost>
I also tried changing to <Directory /var/www/typesetter/appname>, but this also did not work.
The contents of testapp.wsgi in /var/www/html/appname/:
import sys, os
sys.path.insert(0, '/var/www/html/appname')
from testapp import app as application
The contents of testapp.py in /var/www/html/appname/:
from flask import Flask
app = Flask(__name__)
#app.route('/topenzyme/')
def main():
return 'Hello world'
if __name__ == '__main__':
app.run()
Permission setup:
-rwxr-xr-x. 1 apache apache 148 Jan 3 13:37 testapp.py
-rwxr-xr-x. 1 apache apache 100 Jan 3 13:38 testapp.wsgi
I also tried 777, but from what i've read 755 permissions should be sufficient?
If i do httpd -M i get wsgi_module (shared).
The wsgi module should be available.
I tried various virtualhost configs from other posts with similar issues.
I'm really unsure why this is not working.
Is there anything wrong with my configuration settings or could it be something else?
Found out the issue. I was miss-configuring my Apache setup.
As we are already hosting a website on the domain, i should not have tried to setup a virtualhost for the sub-directory. From what i understand now this only works for setting up (sub)domains. With this structure of setting it up the appname.conf file gets added the already existing virtualhost config.
Instead the /etc/httpd/conf.d/appname.conf is now:
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appnameenv
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
Options FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
This corrently links the wsgi application to my sub-directory location i wanted (our-group.our-uni.com/appname)

PGAdmin4 virtual host not working with Apache2 + WSGI

I have a problem. I'm creating a server where I will host a Django project and also PGAdmin4. I configured PGAdmin to run through apache2 and WSGI with a Python 3.8 virtual environment. It was working perfectly before I set up the Django project. at the ip/pgadmin. As soon as i manage to host the Django on the virtual host. The pgadmin started not to work.
When trying to enter the "ip/pgadmin" while the django webserver is not at the root at the WSGIScriptAlias i receive this page.
Not Found
The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at 192.168.0.10 Port 80
and the following log error. The pgadmin4 is installed withing the same virtualenv that runs the django project but as they are daemons thats not a problem. So i do not see why is searching under "/var/www/acas_webserver/pgadmin/"
[Tue Mar 16 00:54:22.633470 2021] [core:info] [pid 26119:tid 140348751849216] [client 192.168.0.5:63072] AH00128: File does not exist: /var/www/acas_webserver/pgadmin/
But when i place the django project at the root of the WSGIScriptAlias i reacieve this page but no log info to know what happened.
Not Found
The requested resource was not found on this server.
I'm trying to get both of them work alone and having my django at the root of the directories of the apache2 virtual hosts + wsgi. I cannot fint whats the problem an its been countless hours without figuring it out. where are the virtual hosts configurations
#django project
<VirtualHost *:80>
ServerAdmin lemanuel.colon#upr.edu
ServerName 192.168.0.10
DocumentRoot /var/www/acas_webserver
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel info
Alias /static /var/www/acas_webserver/static
<Directory /var/www/acas_webserver/static>
Require all granted
</Directory>
Alias /media /var/www/acas_webserver/media
<Directory /var/www/acas_webserver/media>
Require all granted
</Directory>
<Directory /var/www/acas_webserver/adapts>
<Files wsgi.py>
Require all granted
</Files>
Require all granted
</Directory>
WSGIDaemonProcess acas_app python-path=/var/www/acas_webserver python-home=/var/www/acas_webserver/my_venv
WSGIProcessGroup acas_app
WSGIScriptAlias / /var/www/acas_webserver/adapts/wsgi.py
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
#Pgadming
<VirtualHost *:80>
ServerName 192.168.0.10
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/pgadmin-error.log
CustomLog ${APACHE_LOG_DIR}/pgadmin-access.log combined
DocumentRoot /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/
LoadModule wsgi_module modules/mod_wsgi.so
WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/var/www/acas_webserver/my_venv
WSGIScriptAlias /pgadmin /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.wsgi
<Directory /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/>
WSGIProcessGroup pgadmin
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
I just want to get them work to continue on with this projects and its been several days of headaches. Can someone be of help here? If anything is need im happy to provide. Plz help.
UPDATE:
The problem is all coming up once I enable the django webserver. As soon as i turn of my django webserver on Apache2, i can access my pgadmin page, but as soon as I turn on my django webserver on Apache2, the pgadmin again is unreachable.
I have looked at your configs ( e.x apache ) and seem fine.
I think, there are many possible ways to solve your problem.
The first way would be using some docker container that fits your needs. if you don't know about it just "GOOGLE: docker Django Postgres apache " and read the top links.
Search for it and there are many results already there. After that, you can run your project probably. I don't want to go deep into docker but surely would solve your problem.
Second, Check and monitor your services, ports, and permissions. If they are not in right place, maybe your server ports are busy or you should restart the services. (as you said worked fine before the Django installation, this idea has a low chance to solve the problem)
Furthermore, You should deploy your Django project in the right folder. the differences between those logs are rational. root folder probably is under a read-only mode.
you have mentioned that you have to work on this project and it's been several days of headaches. My first question is why do you focus on server deployment rather than coding? You can easily run Postgre, WSGI, and Django without any headache ! ( developing environment)
and pgadmin isn't a necessary tool. Also, if it's the problem and you should use it just ignore it and pass it to the final stage which you have done your coding phases. ( seems you stuck in the first place without coding)

Using Django w/ mod_wsgi and Apache

I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. I've referred to several other resources, but much of what is out there seems to be outdated.
I have a host rule set up on my local machine, pointing www.example.com to my EC2 instance's public IP address.
I have a virtualenv set up which lives in /home/django/example.com/ENV. My Django project lives in /home/django/example.com directly. The project name is mysite, and was generated using django-admin startproject mysite, thus it has the default wsgi.py file inside the /home/django/example.com/mysite directory. The contents of wsgi.py look like:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I've tried adding VirtualHost rules such as the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory "/home/django/example.com/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Similarly, I've tried adding:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf and chucking the following:
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com
<Directory /home/django/example.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
into httpd.conf.
In either case, I've restarted the Apache server directly afterwards.
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE".
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this?
After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias /static /home/django/example.com/static
<Directory /home/django/example.com/static>
Require all granted
</Directory>
WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory /home/django/example.com/mysite>
Require all granted
</Directory>
</VirtualHost>
and here are the contents of wsgi.py that I settled on:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os, sys
from django.core.wsgi import get_wsgi_application
path = '/home/django/example.com'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py, and to run python manage.py collectstatic to collect static files from all applications into said STATIC_ROOT.
I hope this helps somebody else, in future!

How to keep python server running

I need a bit of help here. I am new to python and django. For my project i am developing an system which deals with api fetching and insertions. I need this system to be online 24 * 7. so i need to the server to be running all the time.
Using the traditional method of:
python manage.py runserver
runs perfectly.
I even tried :
nohup python manage.py &
but when i access my project with the url. Its showing that it project is not online. How do i fix this?
If you are using Apache then mod_wsgi should be used along with Django for deployment. This is covered here in great detail.
If you want step by step guide then you can have a look at Digital Ocean's Guide.
Your Apache configuration will look like following:
<VirtualHost *:80>
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
You will need to create a wsgi.py file which looks like following(the default one):
"""
WSGI config for myproject project.
It exposes the WSGI callable as a module-level variable named
```application```.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Hope this helps.

Setting up django and apache using the tutorial isn't working

I am trying to set up apache with an existing django project using the tutorial in django site here. My os is Ubuntu, and everything is installed (django apache2 libapache2-mod-wsgi)
My conf file
WSGIPythonPath /home/avlahop/development/django/rhombus2/rhombus/
<VirtualHost *:80>
ServerName myrhobmus.com
ServerAlias www.myrhombus.com
WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/rhombus/wsgi.py
</VirtualHost>
After I created the conf file I ran the a2ensite Ubuntu command for enabling the site.
Putting WSGIPythonPath inside VirtualHost gives me an apache configtest failure
Files inside directive inside directory (as described in the example) gives me the same failure
If I go to www.myrhombus.com I get a Google chrome could not find the specified address message.
What am I doing wrong? Every tutorial on the Internet is using the old file.wsgi while now Django creates this for you but it is a python file with .py extension. How can I serve my django with apache? And If I wanted to go production at my own server where would you put django code and where would you put template files?
EDIT: I am only getting the Index of / page. Is there something I have to do with mysites location in terms of permissions? Could you give me a working example of an django site apache conf file?
I use Django 1.8 and I successfully deployed my project Apache server. I followed basic concept like you and enable Apache this module early.
enable module
You can my project structure this question.Refer this question to understand project structure
--------------this is my virtual host file----------------------------------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin#key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
-----------------wsgi.py---------------------------------------
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I think this will be help to you.

Categories

Resources