I have installed django website in a new Ubuntu 17.10 server. It is based on python3. It works fine when i run the website in development mode using
python manage.py runserver
But when i tried to configure it via apache2 webserver in port 80 by adding wsgi in apache by editing default config file as shown..
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/usr/mygpsproject/gps_restapi/gps_restapi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gps_restapi python-path=/home/usr/mygpsproject/gps_restapi/ python-home=/home/usr/mygpsproject/venv/bin/
WSGIProcessGroup gps_restapi
WSGIScriptAlias / /home/usr/mygpsproject/gps_restapi/gps_restapi/wsgi.py
</VirtualHost>
but Site is not working. When I checked the apache error log to find this error...
[Sat Apr 14 16:16:09.201739 2018] [core:notice] [pid 5089:tid 140258267696064] AH00051: child pid 7500 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f906c390bc0 (most recent call first):
I am not able to find what is this encodings module. it worked fine in development mode. so python is not missing any libraries for sure.
What could be the problem with my configuration.
Instead of:
python-home=/home/usr/mygpsproject/venv/bin/
you should be using:
python-home=/home/usr/mygpsproject/venv
Review the documentation on using virtual environments. You are supposed to point to the root of the virtual environment as given by sys.prefix, not the bin directory.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Also ensure that the virtual environment is created from the same Python version that mod_wsgi was compiled for.
Finally, depending on the Linux distribution used, you also shouldn't place your code under your personal home directory as the permissions on the home directory can be such that the user that runs your code as under Apache can't access the home directory.
Related
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.
I try to deploy my Django Project on a Debian OS. I use the Anaconda 3 environment for the project that I've made.
Here is the configuration of my Apache2
Alias /static /home/mydef/django_project/static
<Directory /home/mydef/django_project/static>
Require all granted
</Directory>
<Directory /home/mydef/django_project/django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/mydef/django_project/django_project/wsgi.py
WSGIDaemonProcess django_project python-path=/home/mydef/django_project python-home=/home/mydef/venv
WSGIProcessGroup django_project
Then Reload the Apache 2 (already enable my own apache2 conf file)
Then I try to visit it localhost. But it didn't show any response (loading only). Then I check the error log then I see this error.
Current thread 0x00007f4d175f1040 (most recent call first):
[Thu Jan 02 05:58:14.571867 2020] [core:notice] [pid 9737:tid 139969081315392] AH00051: child pid 10145 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings
I also try Searching but still not solved
I made a django project and I tried deploying it with apache and wsgi, however I'm getting the following error in error.log:
Current thread (most recent call first):
[Wed Dec 04 17:14:02. 2019] [core:notice] [pid :tid ] AH00051: child pid 27347 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
I'm using python 3.6.9, I've installed every necessary library using pip3 and I've installed ibapache2-mod-wsgi-py3 using apt.
This is my mysite.conf in /sites-available
WSGIPythonHome /var/www/projectenv/lib/python3.6
WSGIPythonPath /var/www/projectenv/bin/python3
WSGIScriptAlias / /var/www/project/project/project/wsgi.py
<VirtualHost *:80>
# adjust the following line to match your Python path
<directory /var/www/project/site>
<Files wsgi.py>
Require all granted
</Files>
</directory>
</VirtualHost>
Django 2.2.6 is only compatible with python 3, so I'm not sure what to do. I'm not married to the idea of using apache, so if you have any recommendations for other webservers, that will serve as well.
Thanks!
Problem: Followed standard setup for Django web app. I am using Ubuntu 18.04, Django 2.2.1, Python 3.7, Apache 2.4.29, mod-WSGI 4.6.7, and virtualenv to create a virtualenv. When I attempt to access my site (either IP or FQDN) I get a 504 Gateway Timeout Error
I check the Apache2 logs and am getting the following error at 1-second intervals:
Current thread 0x00007f52f8874bc0 (most recent call first):
[Mon Sep 23 02:49:26.540404 2019] [core:notice] [pid 9896:tid 139994333662144] AH00051: child pid 10305 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
python manage.py runserver works fine and I can access through either IP or FQDN via browser from other devices. There are no problems with the database or other issues (although when using FQDN instead of IP # I am getting 404 error on CSS/JS, although rest loads - but this is potentially a separate issue)
I have tried:
* Resetting up virtualenv
* Resetting up Apache2
* Different apache .conf arrangements
* I watched #Graham Dumpleton's excellent video from PyCon Au 2010 "Getting Started with Apache/mod_wsgi." and tried to implement some of his insights
Apache2 Conf
WSGIRestrictEmbedded On
<VirtualHost *:80>
...
...
...
Alias /static /project/static
<Directory /project/static>
Require all granted
</Directory>
Alias /media /project/media
<Directory /project/media>
Require all granted
</Directory>
<Directory /project/main>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django_app python-path=/project python-home=/project/venv
WSGIScriptAlias / /project/main/wsgi.py process-group=django_app
WSGIProcessGroup django_app
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
Apache2 Error File
Current thread 0x00007f52f8874bc0 (most recent call first):
[Mon Sep 23 02:49:26.540404 2019] [core:notice] [pid 9896:tid 139994333662144] AH00051: child pid 10305 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
UFW Status
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
UPDATE: Thanks to Graham's link below I was able to get it working. I believe I had libapache2-mod-wsgi-py3 installed via apt-get install on the system. I had also tried installing mod-wsgi via pip while Python virtual environment was active, but neither worked. I checked the mod-wsgi.so and mod-wsgi.so-python3.6 (or similarly named file indicating Python 3.6) and it looked like the mod-wsgi.so was symlinked to the python 3.6 .so file.
SOLUTION: I purged libapache2-mod-wsgi-py3 from the system and followed the installation instruction guide on https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html per Graham's advice. I made sure to use ./configure --with-python=/path/to/virtualenv/python to make sure the right Python was being used to install mod-wsgi.
This solved the problem. Thank you #Graham Dumpleton.
I am trying to setup a simple Django app using this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
My configuration is:
Ubuntu 14.04, Apache/2.4.7, libapache2-mod-wsgi-py3, Python 3.4, Django 1.86
I keep getting the following error in the apache log whenever I try to access the server using its IP:
ImportError: No module named 'encodings'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
[Sat Nov 07 01:26:56.505373 2015] [core:notice] [pid 20920:tid 140075144152960] AH00051: child pid 21716 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Nov 07 01:26:56.505478 2015] [core:notice] [pid 20920:tid 140075144152960] AH00051: child pid 21717 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
My virtual hosts file looks as follows:
<VirtualHost *:80>
ServerAdmin
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
Alias /static /home/admin/myproject/static
<Directory /home/admin/myproject/static>
Require all granted
</Directory>
<Directory /home/admin/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/admin/myproject:/home/admin/myproject/myprojectenv/lib/python3.4/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/admin/myproject/myproject/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
There were no errors while setting up Django in the virtualenv.
I was finally able to solve this. As I am using Python 3.4, I needed to install libapache2-mod-wsgi-py3 earlier. This installs a precompiled version of mod-wsgi into the Apache modules folder at /usr/lib/apache2/modules.
It also installed the apache module for wsgi-express.
The modules to be loaded by Apache are controlled by configuration files in /etc/apache2/mods-enabled/
Here there are 2 files for each module. A .load and a .conf file. I saw the files wsgi.load & wsgi.conf and also wsgi-express.load & wsgi-express.conf
The .load files mention the .so module to be loaded
Both the .load files had a line beginning with:
LoadModule wsgi_module /usr/lib/apache2/modules/...
That means the wsgi_module was being loaded twice. I used to receive a warning whenever I would restart Apache 2, that mod-wsgi is already loaded so skipping. So the first thing I did was disabled mod-wsgi_express
a2dismod wsgi_express
That left only 1 mod-wsgi to be loaded. This should work.
I actually went a step further and compiled the latest mod-wsgi from source following https://www.digitalocean.com/community/tutorials/installing-mod_wsgi-on-ubuntu-12-04
The latest mod-wsgi can be found here https://github.com/GrahamDumpleton/mod_wsgi/releases
That compilation went smoothly and I got a module named mod_wsgi.so-3.4 in /usr/lib/apache2/modules
In my /etc/apache2/mods-enabled/wsgi.load I put in :
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-3.4
After restarting Apache it worked!