Unabled to deploy django + apache on aws Ubuntu server - django

I am trying to deploy a Django Application using Apache on an Ubuntu Server, I am having a weird error (500 internal error server error) when using port :80.
I made some test using port :8000 running the command:
python3 manage.py runserver 0.0.0.0:8000
And the application is working without any problem there, but when I try to access the application using port 80 it does work.
My apache config file looks like this:
<VirtualHost *:80>
Alias /static /home/ubuntu/gh_system/static
<Directory /home/ubuntu/gh_system/static>
Require all granted
</Directory>
<Directory /home/ubuntu/gh_system/HRSYSTEM>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess HRSYSTEM python-home=/home/ubuntu/gh_system/env python-path=/home/ubuntu/gh_system>
WSGIProcessGroup HRSYSTEM
WSGIScriptAlias / /home/ubuntu/gh_system/HRSYSTEM/wsgi.py
I reviewed the apached2 errors logs, and I am getting a: No module named 'django', my wsgi files is:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HRSYSTEM.settings")
application = get_wsgi_application()

After some research, testing and reviewing the apache logs with:
sudo tail -100 /var/log/apache2/error.log
I found that the problem was related to this line:
WSGIDaemonProcess HRSYSTEM ***python-home=/home/ubuntu/gh_system/env*** python-path=/home/ubuntu/gh_system>
Basically, apache was trying to read the packages inside of the virtual environment but I did install them using the command:
sudo pip3 install -r requirements.txt
Apparently, using the "sudo" command was installing the packages outside the virtual environment, it is so weird; but without "sudo" the console logs the error permission denied, so I give chmod 700 permission to the virtual environment and install again the packages without the "sudo", and then it works!
Some of the things I learned about the errors:
Problem: Not module 'encodings' --> this problem occurs when apache can not access your packages, usually for a bad path specified in the config file.
Problem: Not module 'x module, could be django' means you need to install the package using pip3.
Hope it helps someone! and just to point out, something I have been learning lately, is: when you spend too much time trying to solve a problem, and there is not much information about it on internet, it means that something really, really basic is failing in your project; for example, installing the packages outside the virtual environment without even realizing it, just like I did.

Related

Apache, Django and wsgi, No module named 'encodings'

I've looked through many stack overflow questions with the same issues, but none of them solved my issue. I'm trying to deploy Django on AWS - Ubuntu 18.04 using Apache and WSGI.
When running my website with Django's servers on port 8000, everything works fine. Then when I attempt to run on apache I get the following error in my logs.
Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
Command line: '/usr/sbin/apache2'
(2)No such file or directory: mod_wsgi: Unable to stat Python home /var/www/html/projects/venv
Python interpreter may not be able to be initialized correctly. Verify the supplied path
and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
As it says clearly mod_wsgi and Python 3 are configured. I am using a virtual environment. My project is located at:
/var/www/html/project/venv/mysite
My Apache conf file is configured as:
<Directory /var/www/html/projects/venv/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/var/www/html/projects/venv/mysite python-home =/var/www/html/projects/venv
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/html/projects/venv/mysite/mysite/wsgi.py
Alias /static /var/www/html/projects/venv/mysite/polls/static
<Directory /var/www/html/projects/venv/mysite/polls/static>
Require all granted
</Directory>
I'm not sure where to go from here. I don't fully understand what the error means. I have Python installed in my Venv. There is a file pyenv.conf which reads
home = /usr
implementation = CPython
version_info = 3.6.9.final.0
virtualenv = 20.0.20
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3
I'm wondering if this base-executable path being different than my python path in the apache config file is causing problems, but I'm not sure exactly what I need to do from here (or if this is even part of the problem. I believe permissions should not be the issue since I added all of my files within the var/www/html folder.
Update: I added the recommended permissions to all files in the chain and that didn't change anything.
Any advice is appreciated.

Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'

I'm trying to serve a little django project with the following Apache configuration :
Apache virtualhost configuration :
<VirtualHost *>
ServerName servername
[...]
<Directory "/path/to/project/project">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess project python-path=/path/to/project:/path/to/Envs/venv/lib/python3.5/site-packages
WSGIScriptAlias / /path/to/project/project/wsgi.py
</VirtualHost>
I also have the following wsgi.py :
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
application = get_wsgi_application()
I have no problem to serve STATIC files and MEDIA files.
I also checked permissions and tried to recursively use 755, then 777 to my virtualenv's site-package directory. It didn't work.
But when trying to reach the root of my site I get the following :
from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django'
I guessed that it was a Python path related problem since django is installed in my virtualenv. But I added the relevant python paths to the WSGIDaemonProcess's python-path attribute so I don't get why it doesn't work.
I also guess I could add the relevant directory to my Python path in my wsgi.py by using the site module, but I'd like to understand why the Apache configuration I tried isn't enough. Did I miss something?
You are missing a WSGIProcessGroup directive or equivalent option on WSGIScriptAlias, so your application isn't actually being run in that daemon process group where you have set the virtual environment.
See Using mod_wsgi daemon mode
I would also recommend ensuring application group is set to '%{GLOBAL}' if that is the only application you are running in the daemon process group.
Thus use:
WSGIScriptAlias / /path/to/project/project/wsgi.py \
process-group=project application-group=%{GLOBAL}
Also better to use python-home for the virtual environment.
WSGIDaemonProcess project python-path=/path/to/project \
python-home=/path/to/Envs/venv
See:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
My rep is not over 50 so I cannot comment, but I'd like to share my discovery.
In WSGIDaemonProcess, if you are using Python 3.5, you need to set exactly as #graham-dumpleton say, with
python-home=/path/to/Envs/venv
set explicitly.
However, if you are using Python 3.4 (or some older version Python like 2.7 as far as I know), you'll have to configure it as
python-path=/path/to/project:/path/to/Envs/venv/lib/python3.4/site-packages
just like what the asker did.
Really weird.
For me the problem was I had mod wsgi installed for python2. I had to reinstall it for python3:
sudo apt-get install libapache2-mod-wsgi-py3
The issue for me was that I was running libapache2-mod-wsgi-py3 on python version 3.9. When I rolled back the python version to 3.7, there was no issue.

Django - Apache2 - Postgresql: error with _psycopg

Trying to deploy a django project on apache (2.2) on Debian 7.
Using Django 1.8 and a virtual environment with python3.4.
Getting the following error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named _psycop
I have tried the suggestions pointed out here, and here.
Here is my apache conf.
(following lines are nested within VirtualHost scope)
#WSGI configuration for ds_monitor django project
Alias /static /home/pkaramol/Applications/timeds/ds_monitor/static_root
<Directory /home/pkaramol/Applications/timeds/ds_monitor/static_root>
Allow from all
</Directory>
<Directory /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
WSGIDaemonProcess ds_monitor python-path=/home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
WSGIProcessGroup ds_monitor
WSGIScriptAlias /ds_monitor /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor/wsgi.py
and this outside VirtualHost scope (to enable production - time virtual environment)
WSGIPythonPath /home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
Another issue is the following line present in apache's log stack trace
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 821, in _gcd_import
loader.load_module(name)
Why is it using system's python 3.2?
Have I done s.th wrong in terms of pointing the virtualenv path correctly in wsgi confs?
In any case, the psycopg2 module is also installed in the system (not only in virtualenv)
Seems very weird but the whole issue most likely had to do with the python version that mod_wsgi was compiled against.
I had downloaded / configured / installed version 4.5.2 of mod_wsgi from here but forgot to configure with the appropriate python version.
So I run through the whole process again (starting from unzipping the original tarball) and configuring as follows:
./configure --with-python=/usr/bin/python3.5
Restarted apache and problem solved.

How to Deploy Django project on amazon AWS or other server

I am digging arround from some days and i can not figure out how to deploy a Django Project.
I have a Amazon AWS EC2 machine running.
I started the project on my laptop, then i configured and installed Virtualenv on AWS EC2 and all the requeriments.
This machine has Apache2 and libapache2-mod-wsgi and mysql installed.
When i do the command python manage.py runserver there are NO errors! I did the syncdb and everything is ok.
But a can not see the page in my browser.
Apache config:
File: /etc/apache2/sites-enabled/test_site.com.conf
WSGIScriptAlias / /home/ubuntu/server/workspace/proj/proj/wsgi.py
WSGIPythonPath /home/ubuntu/server/workspace/proj/
<Directory /home/ubuntu/server/workspace/proj/proj>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/server/workspace/proj/static/
<Directory /home/ubuntu/server/workspace/proj/static>
Require all granted
</Directory>
How can i Deploy my Django?
Thanks,
Ricardo
I found a way to solve my problem.
For new comers, with a similar problem, take a look at this page Deploy django in Apacha2 server and mod wsgi
You can use this script that I wrote:
Safely deploy your Django app in less 1 minute!
Instructions
Installing the DeployDjango script
$ wget https://raw.githubusercontent.com/yask123/DeployDjango/master/deploydjango.sh && chmod +x deploydjango.sh
From your Django App’s root directory (Where manage.py file exists).
$ sudo ./deploydjango.sh project_name
Done!
Visit http://ip-address-of-your-instance to see your web app live!

Django (1.6) + Apache2

First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.