Server isnt recognizing python virtualenv anymore - django

I have a deployed Django app on a Centos 7 server. I ran into problems when git pulling from my repository. It doesn't recognize python-home at the location where my virtualenv is anymore.
When i print(sys.prefix), it shows '/usr', but when i activate my virualenv (source /path/to/virtualenv/bin/activate) it shows the correct path that is assigned to python-home in my httpd .conf file
.conf file
Alias /static /var/www/ek/static
<Directory /var/www/ek/static>
Require all granted
</Directory>
<Directory /var/www/ek/new>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess ek python-path=/var/www/ek python-home= /var/www/kpi_env
WSGIProcessGroup ek
WSGIScriptAlias / /var/www/ek/new/wsgi.py
I'm not exactly sure what the problem is, when im not in my virtualenv, should the prefix be the path to the virtualenv? Should print(sys.prefix) only show var/www/kpi_env when im in the virtualenv?
Whatever it is, it doesn't recognize python-home=/var/www/kpi_env as anything anymore.
When i change it to python-home = /usr it works but the application is giving me errors that i've never run into before on my server or on my workstation. This error being Permission denied is Server running on host 12.0.0.1 and accepting tcp con on port 5432 This is referring to postgresql, and i'm sure that it is running and accepting tcp connection on port 5432. And that my settings.py reflect the right configurations. I havent changed any configurations with changes with git pull and havent touched configs for postgresql at all, so i can only think that its because of the python library it is using, because thats the only thing that has changed.
How do i make my httpd server recognize python-home= /var/www/kpi_envagain ?

This doesn't exactly answer what i asked but this was the workaround i did:
I ran this command to allow my httpd server to accept the connection to my postgresql database,
setsebool -P httpd_can_network_connect_db 1
-P will do this permanently and 1 will set it to on.
And in this, i used python-home = /usr since i couldnt get my virtualenv to work anymore

Related

mod_wsgi: "No module named 'django'"

Maybe some needed context:
I installed python3.9 into a directory /opt/python39/. I compiled mod_wsgi with this version of python (as per this post) and was able to conduct a test to make sure that it was working correctly. I am not using a virtual environment.
I am trying to move my django project to production, so naturally, I am following the django docs. When I use the default file configurations at the very beginning, i.e.:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /opt/python39/bin/python3.9
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
It works (for the most part - there are resources missing on the page, but thats for another SO post) like a charm.
So I continue through the docs (which is now telling me that I should move it to "Daemon mode"):
“Daemon mode” is the recommended mode for running mod_wsgi (on non-Windows platforms). To create the required daemon process group and delegate the Django instance to run in it, you will need to add appropriate WSGIDaemonProcess and WSGIProcessGroup directives. A further change required to the above configuration if you use daemon mode is that you can’t use WSGIPythonPath; instead you should use the python-path option to WSGIDaemonProcess, for example:
So I change my file to look like:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
#WSGIPythonHome /opt/python39/bin/python3.9
#WSGIPythonPath /path/to/mysite.com
WSGIDaemonProcess mysite.com python-home=/opt/python39/bin/python3.9 python-path=/path/to/mysite.com
WSGIProcessGroup mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
then run sudo apachectl restart, go back to my browser, refresh, shows 500 internal server error, check my logs, and "what do you know?" - ImportError: "No module named 'django'".
I've come across this SO article that seems to be describing my problem, but for virtual environments, and offers a solution, but I give it a go anyway:
WSGIProcessGroup mysite.com
WSGIDaemonProcess mysite.com python-home=/opt/python39 python-path=/path/to/mysite.com
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py process-group=iengravethat.com application-group=%{GLOBAL}
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
I rerun sudo apachectl restart, check my browser - same error, check the logs - same error.
I have tried a multitude of other configurations, but they all boil down to this error. Does anyone have any idea as to how to remedy this problem?
---UPDATE---
SUTerliakov and I opened up a channel for discussion, the end conclusion was thus:
Regarding recompilation - I was absolutely happy with the following installation (from Dockerfile, but it doesn't really matter):
pip install -U --no-cache-dir mod-wsgi
mod_wsgi-express install-module | tee /etc/apache2/mods-available/wsgi_express.load /etc/apache2/mods-available/wsgi_express.conf
And it can be done from venv too.
I really don't understand why did your paths stop working after
switching to daemon mode. I just suggest to try virtualenv instead,
because this method is recommended by maintainer.
You can just drop "tee" part and use standalone mod_wsgi-express install-module. It will print two lines of config, you can add them to your config file manually. It will print WSGIPythonHome, take this value and use for python-home option of WSGIDaemonProcess.
(may require root, AFAIR)
So, basically just create a venv and link it in your .conf file.
Create a virtual environment with the python interpreter you want to use and activate it.
pip install the packages you were using for django.
pip install mod-wsgi.
Grab the version of mod_wsgi you want.
untar it and enter the directory.
./configure --with-python=python3 (This python3 is the same version as the python3 I wanted - not python 3.6 which comes pre-installed on centos7).
make LDFLAGS='-L/path/to/venv/lib -Wl,-rpath,/path/to/venv/lib'.
sudo make install.
run mod_wsgi-express module-config and copy and paste it at the top of your .conf file.
deactivate your environment.
restart apache - sudo apachectl restart
I have the following config:
ServerName ${SERVER_NAME}
ServerAdmin ${SERVER_ADMIN}
<IfModule unixd_module>
User ${APP_USER}
Group ${APP_USER}
</IfModule>
WSGIRestrictEmbedded On
WSGIPassAuthorization On
IncludeOptional /etc/apache2/conf.d/*.conf
Timeout 60
<VirtualHost *:80>
Redirect permanent / https://${SERVER_NAME}/
</VirtualHost>
<VirtualHost *:443>
Alias /media <media folder>
<Directory <media_folder> >
Options -Indexes
Require all granted
</Directory>
Alias /static <static_folder>
<Directory <static_folder> >
Options -Indexes
Require all granted
</Directory>
WSGIDaemonProcess <name> processes=1 threads=5 display-name=%{GROUP} home=<top_folder>
WSGIScriptAlias / <wsgi.py path> process-group=<name> application-group=%{GLOBAL}
... SSL config here ...
</VirtualHost>
It is not a full config: logging, SSL and WebSocket proxies are thrown away.
<name> is any alias
<top_folder> is folder with manage.py file
<wsgi.py path> is <top_folder>/something/wsgi.py
<media_folder> and <static_folder> are paths of MEDIA_ROOT and STATIC_ROOT settings.
It is deployed with docker (system-level package installation as root, no virtual environment).
Final attempt
Probably something gets messed up when using system python that is not the default interpreter. You can set up virtual environment and make httpd use it. To avoid manual recompilation, mod_wsgi pip package can be used:
# With venv activated
pip install mod_wsgi
sudo mod_wsgi-express install-module
This will install mod_wsgi for venv python and print two lines of config: LoadModule ... and WSGIPythonHome <path>. You can add LoadModule to configuration file and use the following daemon config:
WSGIDaemonProcess mysite.com home=/path/to/mysite.com python-home=<venv_root>
Note
You don't need to recompile mod_wsgi manually with this package, it is the most amazing aspect of such installation! Two commands above are sufficient to install mod_wsgi. It will copy module files to required folders for you (see end of this readme), so your steps 4-8 can be omitted.

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.

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.

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.

Configuring Apache 2 with mod_wsgi for Django - no httpd.conf file

I am using Apache 2 on raspbian os. I read somewhere that in the newest versions of Apache there is no httpd.conf file, is this true? [Edit] (This is true)
My apache server is running (I can see the default web page), and mod_wsgi is successfully installed. So I began the instructions here for using django 1.5 with apache. However it tells me I need to add the following lines to the httpd.conf file:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
However, there is no httpd.conf file present in my version of apache. I have checked /etc/httpd/confand /etc/apache2 for the http.conf file. /etc/apache2 is where apache is though.
In short, where should I add the required settings so that apache recognizes mod_wsgi.
Possibly, many distros use
/etc/apache2/apache2.conf
Instead. However you shouldn't really edit that and it affects apache2 globally, i.e. all you sites (virtual-hosts). If you have only the one site better edit the contents of:
/etc/apache2/sites-available/default
The Ubuntu Apache2 configuration documentation is a good getting started guide: https://help.ubuntu.com/14.04/serverguide/httpd.html#http-configuration
Here is tutorial configuring Apache2 with mod_wsgi for Django on Ubuntu 11.04 wihtout httpd.conf: http://blog.madspace.me/configure-python-django-with-apache/, and one on 12.04: http://www.lennu.net/2012/05/14/django-deployement-installation-to-ubuntu-12-dot-04-server/