"ERR_CONNECTION_REFUSED" after adding "LoadModule wsgi_module modules/mod_wsgi.so" - python

I am trying to run python on wampserver, I am following How to install Python with Wampserver as guide.
Setup Configuration:
Python 2.7.6(64-bit); wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64bit;
Windows 7 (64 bit)
For mod_wsgi: I tried all three 64 bit binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi
But as I add "LoadModule wsgi_module modules/mod_wsgi.so" line to load wsgi module in httpd.conf, the server fails with error "ERR_CONNECTION_REFUSED"
Any help on what am I missing?

Related

Deploying django with apache + mod-wsgi _ Error 500

I'm trying to deploy my first django app in windows 10 (x64), so installed apache 2.4(x86), installed and build mod_wsgi (4.9.1) with python 3.8(x86) (I have microsoft visual studio 2019 installed) and configured Apache httpd.conf and httpd-vhosts.conf files.
My app runs fine, but the annoying error 500 keeps showing every now and then, and goes away by refreshing the browser every time.
There seems to be a bug with python 3.8, so decided to upgrade to python 3.10 (X86),
Reinstalled mod-wsgi and other packages using pip in new virtual environment in the same directory, and reconfigured httpd.conf accordingly. now apache service fails to start with error: "windows could not start the Apache2.4 on local computer....".
If I change httpd.conf configuration to load and use the python3.8 , Apache starts with no error (but again Error 500 keeps showing).
by the way I tried the same with python3.9 (x64) and Apache 2.4 (X64), and apache fails to start again.
Any help would be greatly appreciated.
In httpd.conf:
This configuration fails:
LoadFile "C:/Program Files (x86)/Python310-32/python310.dll"
LoadModule wsgi_module "d:/django project/unemployment project/.venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win32.pyd"
WSGIPythonHome "d:/django project/unemployment project/.venv"
This configuration works:
LoadFile "C:/Program Files (x86)/Python38-32/python38.dll"
LoadModule wsgi_module "d:/django project/unemployment project/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win32.pyd"
WSGIPythonHome "d:/django project/unemployment project/venv"

Syntax Error when deploying django on apache2

I am trying to host a django application on a server using apache2 and mod_wsgi.
The project itself is located at the path /usr/share/django-projects/mysite
The virtual environment is located at the path /home/zakhar/django_test/env
It's part of apache2 (2.4 version) conf file. In venv python version is 3.8.13
WSGIDaemonProcess mysite user=www-data group=www-data python-path=/usr/share/django-projects/mysite:/home/zakhar/django_test/env/lib/python3.8/site-packages python-home=/home/zakhar/django_test/env
It's last error from apache arror.log
File "/home/zakhar/django_test/env/lib/python3.8/site-packages/django/db/models/aggregates.py", in line 84
if (default := c.default) is None:
^
SyntaxError: invalid syntax
I think apache use python 3.6.9 in global env, but not in venv
I would be very grateful if someone could help me
I installed mod_wsgi in my venv and edited /etc/apache2/mods-avaible/wsgi.load
LoadModule wsgi_module "/home/zakhar/django_test/env/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIPythonHome "/home/zakhar/django_test/env"

How to run Flask + Apache app with Anaconda Python version?

I have in my server two Python versions: the Python default, already present in Ubuntu Server 16.04 and Anaconda Python.
When I run the Flask application with Apache, because this is running in user www-data, the application runs in Python default, but I have to run in Anaconda installation.
How to configure to use the Anaconda Python version and how to install new modules in this version?
The path of Anaconda installed is /home/wandre/anaconda2
This is one way that worked for me to let Apache run the python module in Anaconda environment so that Flask app can run.
First is installation. Install Apache with the command sudo apt-get install apache2 apache2-bin apache2-dev. Then, install Anaconda under the directory /opt as sudo. After having Anaconda installed on /opt/anaconda2, install conda packages as below:
sudo su # Login as superuser
export PATH=/opt/anaconda2/bin:$PATH # Add conda to your path
conda install -c anaconda flask
pip install mod_wsgi
mod_wsgi-express install-module # the outputs to this is used for Apache configuration in the next step
Now you need to update the Apache configurations. This involves updating three files:
wsgi.load
Open the file /etc/apache2/mods-available/wsgi.load and add the following (this is the output from mod_wsgi-express install-module so yours could be different)
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py27.so
If the LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so was originally included in this file, comment it out. Otherwise (at least from what I checked), the apache server will run the default python instead of the one in Anaconda. Next, enable the wsgi mod:
sudo a2enmod wsgi
wsgi.conf
Open the file /etc/apache2/mods-available/wsgi.conf and add the following
<IfModule mod_wsgi.c>
WSGIPythonHome /opt/anaconda2
</IfModule>
000-default.conf (your filename may be different)
Open the file /etc/apache2/sites-available/000-default.conf and add the following
WSGIDaemonProcess flaskapp python-home=/opt/anaconda2 python-path=/var/www/flask
WSGIScriptAlias / /var/www/flask/flaskapp.wsgi
<Directory /var/www/flask>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
You are all set.
Please note that I was not able to find tutorial for setting 'Anaconda + Flask + Apache' so I referred to a post on 'Anaconda + Django + Apache'.

uWSGI will not run in mixed Python environment in order to operate correctly with nginx and run Django app

Environment: Ubuntu 16.04 (with system Python at 2.7.12) running in Vagrant/Virtualbox on Windows 10 host
Python Setup: System python verified by doing python -V with no virtualenv's activated. Python 3.5 is also installed, and I've done pipenv --three to create the virtualenv for this project. Doing python -V within the activated virtualenv (pipenv shell to activate) shows Python 3.5.2.
Additional Background: I'm developing a Wagtail 2 app. Wagtail 2 requires Django 2 which, of course, requires Python 3. I have other Django apps on this machine that were developed in Django 1.11/Python 2.7 and are served by Apache. We are moving to Django 2/Python 3 for development going forward and are moving to nginx/uWSGI for serving the apps.
I have gone through many tutorials/many iterations. All Vagrant port mapping is set up fine with nginx serving media/static files and passing requests upstream to the Django app on a unix socket, but this is giving a 502 Gateway not found error because uWSGI will not run correctly. Therefore, right now I'm simply running the following from the command line to try to get uWSGI to run: uwsgi --ini /etc/uwsgi/sites/my_site.com.ini. This file contains:
[uwsgi]
uid = www-data
gid = www-data
plugin = python35
# Django-related settings
# the base directory (full path)
chdir=/var/sites/my_site
# Django's wsgi file
wsgi-file = my_site.wsgi
# the virtualenv (full path)
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
# process-related settings
# master
master = True
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe)
socket = /tmp/my_site.sock
# clear environment on exit
vacuum = True
I've tried installing uWSGI in the following ways:
system-wide with pip install uwsgi as well as pip3 install uwsgi
using apt-get install uwsgi uwsgi-plugin-python3
I've ensured that only one install is in place at a time by uninstalling any previous uwsgi installs. The latter install method places uwsgi-core in usr/bin and also places in usr/bin shortcuts to uwsgi, uwsgi_python3, and uwsgi_python35.
In the .ini file I've also tried plugin = python3. I've also tried from the command line:
uwsgi_python3 --ini /etc/uwsgi/sites/my_site.com.ini
uwsgi_python35 --ini /etc/uwsgi/sites/my_site.com.ini
I've tried executing the uwsgi ... .ini commands from both within the activated virtual environment and with the virtualenv deactivated. Each of the three command line uwsgi ... .ini executions (uwsgi ..., uwsgi_python3 ... and uwsgi_python35 ...) DO cause the .ini file to be executed, but each time I'm getting the following error (the last two lines of the following statements):
[uwsgi] implicit plugin requested python35
[uWSGI] getting INI configuration from /etc/uwsgi/sites/my_site.com.ini
*** Starting uWSGI 2.0.12-debian (64bit) on [Wed Mar 7 03:54:44 2018] ***
compiled with version: 5.4.0 20160609 on 31 August 2017 21:02:04
os: Linux-4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018
nodename: vagrant-ubuntu-trusty-64
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /vagrant/my_site
detected binary path: /usr/bin/uwsgi-core
setgid() to 33
setuid() to 33
chdir() to /var/sites/my_site
your processes number limit is 7743
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/my_site.sock fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
Set PythonHome to /root/.local/share/virtualenvs/my_site-gmmiTMID
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
If I go into the Python command line within the activated virtualenv and do import encodings, it imports fine (no message - just comes back to command line). A search for this particular error has turned up nothing of use. Any idea why the ImportError: No module named 'encodings' is coming up?
UPDATE - PROBLEM STILL OCCURRING
I'm using pipenv, and it stores individual virtualenvs in the /home/username/.local/share/virtualenvs folder. Though I was able to start uWSGI from the command line by executing the uWSGI config file as the vagrant user (see comment below), I have still not been able start the service with /home/vagrant/.local/share/virtualenvs/my_venv in the uWSGI config file. I tried adding the vagrant user to the www-data group and adding the www-data user to the vagrant group. I also put both read and execute permission for the world on the whole path (including the individual venv), but the uWSGI service will still not start.
A HACKISH WORKAROUND
I did finally get the uWSGI service to start by copying the venv to /opt/virtualenvs/my_venv. I was then able to start the service with sudo service uwsgi start. Ownership of that whole path is root:root.
STILL LOOKING FOR A SOLUTION...
This solution is not optimal since I am now executing from a virtualenv that will have to be updated when the default virtualenv is updated since this location is not the default for pipenv, so I'm still looking for answers. Perhaps it is a Ubuntu permissions error, but I just can't find the problem.
It might be the problem with your virtual environment. Try the following
rm -rf /root/.local/share/virtualenvs/my_site-gmmiTMID
virtualenv -p python3 /root/.local/share/virtualenvs/my_site-gmmiTMID
source /root/.local/share/virtualenvs/my_site-gmmiTMID/bin/activate
pip install -r requirements.txt
and in uwsgi conf try to change
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
to
home=/root/.local/share/virtualenvs/my_site-gmmiTMID
Reference:
http://uwsgi-docs.readthedocs.io/en/latest/Options.html#virtualenv

Python web script doesn't run on apache

I am trying getting starting with python web programming but I have a problem. I want to work with CGI
I installed Ampps web-server on pc (Linux). According to this instruction Apache Tutorial: Dynamic Content with CGI. I put my file index.pl in the folder '/usr/local/ampps/www/cgi-bin' and tried to accesses it
http://localhost/cgi-bin/index.py
Here is code of my file:
#!/usr/local/ampps/python/bin/python
print "Content-type: text/html\n\n"
print "Hello World from PythonStandard Hello World from a Python"
But it gives me mistake Sorry, the page you are looking for is currently unavailable.
Here is error log of Apache:
[Tue Jul 22 20:05:45.019799 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] AH01215: (13)Permission denied: exec of '/usr/local/ampps/www/cgi-bin/index.py' failed
[Tue Jul 22 20:05:45.020262 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] End of script output before headers: index.py
I corrected conf of Apache
AddHandler cgi-script .cgi .pl .py
it looked ok too
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "{$path}/www/cgi-bin/"
What can be wrong else?
Your script needs to have the executable bit set (using chmod) and the Apache user or group (usually named 'Apache' or 'www-data' depending on your distro - you can check /etc/passwd to find out) must have the 'execute' permission on it.
I was getting similar errors. It seemed that the AMPPS installation of Python was missing one of the DLLs. I downloaded the 2.7 .msi package from ActiveState and installed it.
A couple of things before you start: the location, and the permissions.
I may be wrong on this, but Apache will be looking for the Python executable in the directory that was created for it in the AMPPS installation -- c:\whatever\whatever\ampps\python. I tried installing it in c:\whatever\whatever\ampps\python2.7, but it didn't work. I suppose I could have edited the httpd.conf file, but since the original installation wasn't working anyway, I overwrote the c:\w\w\ampps\python files.
I also ran the .msi as an administrator. This was tricky, because my company-issued machine doesn't let me simply right-click on an .msi and select "run as administrator." I finally went into the task manager (option 8 here: http://www.eightforums.com/tutorials/9564-run-administrator-windows-8-a.html), and put the full path to the downloaded file in the entry. And voila, "hello world."

Categories