It works! In the end I did not have to refer to the link to virtual environments, although I read it anyway for interest. My python-test.wsgi file now looks like this:
import sys sys.path.insert(0,"C:\python_projects\www\python-test") from hello import app as application So I think I can sign off on setup and get down to other Flask issues. Many many thanks for all your patience and help Mike On Friday, August 18, 2017 at 12:05:19 AM UTC+1, Graham Dumpleton wrote: > > > On 18 Aug 2017, at 9:01 am, Mike Heath <[email protected] <javascript:>> > wrote: > > Thanks for that Graham but I'm not quite out of the wood yet. From your > latest advice, my httpd-vhosts.conf file now looks like this: > > # Virtual Hosts > > <VirtualHost *:80> > ServerName localhost > DocumentRoot c:/wamp/www > <Directory "c:/wamp/www/"> > Options +Indexes +Includes +FollowSymLinks +MultiViews > AllowOverride All > Require local > </Directory> > > ServerName localhost > > > You don't need the second ServerName. > > WSGIScriptAlias /tester C:/python_projects/www/wsgi_script/test.wsgi > WSGIScriptAlias /app-test > C:/python_projects/www/python-test/python-test.wsgi > <Directory C:/python_projects/www/> > Order allow,deny > Allow from all > </Directory> > > </VirtualHost> > > The url *localhost/tester* gets me 'Hello world' from the simple wsgi > file cut and pasted from online. > The url *localhost/app-test* gets me a 500 error and the error log is > here: > > [Thu Aug 17 23:45:06.738358 2017] [wsgi:info] [pid 9252:tid 1252] mod_wsgi > (pid=9252): Create interpreter 'localhost|/app-test'. > [Thu Aug 17 23:45:06.754401 2017] [wsgi:info] [pid 9252:tid 1252] [client > 127.0.0.1:54949] mod_wsgi (pid=9252, process='', > application='localhost|/app-test'): Loading WSGI script > 'C:/python_projects/www/python-test/python-test.wsgi'. > [Thu Aug 17 23:45:06.754401 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] mod_wsgi (pid=9252): Target WSGI script > 'C:/python_projects/www/python-test/python-test.wsgi' cannot be loaded as > Python module. > [Thu Aug 17 23:45:06.754401 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] mod_wsgi (pid=9252): Exception occurred processing WSGI > script 'C:/python_projects/www/python-test/python-test.wsgi'. > [Thu Aug 17 23:45:06.755406 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] Traceback (most recent call last):\r > [Thu Aug 17 23:45:06.755406 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] File > "C:/python_projects/www/python-test/python-test.wsgi", line 9, in <module>\r > [Thu Aug 17 23:45:06.755406 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] from hello import app as application\r > [Thu Aug 17 23:45:06.755406 2017] [wsgi:error] [pid 9252:tid 1252] [client > 127.0.0.1:54949] ModuleNotFoundError: No module named 'hello'\r > > > Inside of python-test.wsgi you will need to add at the start: > > import sys > sys.path.insert(0, > '/some/path/to/parent/directory/of/flask/application/code') > > For more details about module search path and setting up things for Python > virtual environments in particular, read: > > > http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html > > Just be aware that since you are on Windows, you don't have daemon mode. > > The directory *python-test* has two files: *hello.py* with this code > > from flask import Flask > > app = Flask(__name__) > > @app.route('/') > def index(): > return 'Hello World' > > if __name__ == "__main__": > app.run() > > > and *python-test.wsgi* with this code > > #! C:/Python36 > > import sys > > sys.path.insert(0,"C:\python_projects\www") > > from hello import app as application > > I got this code from various research online - it's obviously completely > wrong but I'm at a loss as to what it should contain. Can you help me with > that? > > Mike > > > > On Thursday, August 17, 2017 at 11:23:15 PM UTC+1, Graham Dumpleton wrote: >> >> >> On 17 Aug 2017, at 11:14 pm, Mike Heath <[email protected]> wrote: >> >> What a star you are! That worked! It was the virtual host setup that put >> the final piece into the jigsaw. I have also, on your advice, moved all >> python project files to a new directory outside wamp. Once I had changed >> the virtual host details, that worked fine too. However, that's a simple >> case with a special wsgi file. If I have a website project, presumably I >> don't have to use wsgi file extensions throughout? So does each project >> have to have an embedded wsgi file to 'connect' it and what would its code >> look like? >> >> >> Yes, there needs to be some Python code file usable as the WSGI script >> file. Then extension doesn't usually matter, so if has .py extension is >> okay. If using .py just make sure you don't have: >> >> AddHandler cgi-script .py >> >> in your httpd.conf file as that will cause it not to work. >> >> Also, I'm assuming that each project would need its own virtualhost setup. >> >> >> Not a separate VirtualHost definition. If mounting them at different sub >> URL, put it in the same VirtualHost. >> >> A separate VirtualHost can only be used where is for different hostname >> or port. >> >> I tried adding another virtual host within the VirtualHost tags like this: >> >> ServerName localhost >> >> >> You don't need to duplicate ServerName if already appears and likely you >> don't need it at all if only have one VirtualHost as it will default to >> using that anyway. A VirtualHost is strictly only needed when start >> publishing under a real hostname and server needs to handle more than one >> hostname with different applications. >> >> WSGIScriptAlias /app-test C:/python_projects/www/python-test/app.py >> <Directory C:/python_projects/www/python-test> >> Order allow,deny >> Allow from all >> </Directory> >> >> and got a 404 for /app-test, so maybe you can't have two different >> scriptaliases? btw app.py looks like this: >> { >> >> from flask import Flask >> >> app = Flask(__name__) >> >> @app.route('/') >> def index(): >> return 'Hello World' >> >> if __name__ == "__main__": >> app.run() >> } >> >> Mike >> On Thursday, August 17, 2017 at 12:37:14 PM UTC+1, Graham Dumpleton wrote: >>> >>> >>> On 17 Aug 2017, at 9:23 pm, Mike Heath <[email protected]> wrote: >>> >>> Thanks for your response. Taking the points in turn: >>> >>> Can you confirm that you are using the same Apache instance for PHP at >>> the same time as wanting to use it for Python? Yes that is correct >>> Many thanks for your response, Graham. Taking your point in turn: >>> >>> Can you confirm that you are using the same Apache instance for PHP at >>> the same time as wanting to use it for Python? Yes that is correct >>> >>> Post the details of the over VirtualHost. >>> Full content of the *httpd-vhosts.conf* file is as follows: >>> >>> <VirtualHost *:80> >>> ServerName localhost >>> DocumentRoot c:/wamp/www >>> <Directory "c:/wamp/www/"> >>> Options +Indexes +Includes +FollowSymLinks +MultiViews >>> AllowOverride All >>> Require local >>> </Directory> >>> </VirtualHost> >>> # >>> <VirtualHost *:80> >>> ServerName localhost >>> WSGIScriptAlias /test C:/wamp/www/wsgi_script/test.wsgi >>> <Directory C:/wamp/www/wsgi_script> >>> Order allow,deny >>> Allow from all >>> </Directory> >>> </VirtualHost> >>> >>> >>> >>> Change above two VirtualHost definitions to: >>> >>> <VirtualHost *:80> >>> ServerName localhost >>> DocumentRoot c:/wamp/www >>> <Directory "c:/wamp/www/"> >>> Options +Indexes +Includes +FollowSymLinks +MultiViews >>> AllowOverride All >>> Require local >>> </Directory> >>> >>> WSGIScriptAlias /test C:/wamp/www/wsgi_script/test.wsgi >>> <Directory C:/wamp/www/wsgi_script> >>> Order allow,deny >>> Allow from all >>> </Directory> >>> </VirtualHost> >>> >>> In other words, combine them into one. >>> >>> For the same hostname:port, everything must be under the one >>> VirtualHost. Multiple VirtualHost for same hostname:port are not >>> automatically joined in anyway. It was never matching the second >>> VirtualHost and was being process in context of first VirtualHost and why >>> getting 404. >>> >>> Technically since likely only got one site and no public hostname, you >>> could leave out the VirtualHost/ServerName directives and have everything >>> at top level. >>> >>> See how that goes and can then start changing where you place things. >>> You should not put your Python source code under C:/wamp/www like you are >>> as it could expose the source code even if WSGI application mapped properly. >>> >>> For example, I could download source code for that test.wsgi by visiting >>> URL path on server of: >>> >>> /wsgi_script/test.wsgi >>> >>> So better to create separate directory not under DocumentRoot to hold >>> Python web application code. >>> >>> Graham >>> >>> >>> Also, what is DocumentRoot set to in the httpd.conf file? The code line >>> is *DocumentRoot "${INSTALL_DIR}/www"* and earlier there is *Define >>> INSTALL_DIR c:/wamp* which presumably defines the variable. >>> >>> What did step 4 output and what did you add to the httpd.conf file? The >>> following: >>> >>> LoadFile "c:/python36/python36.dll" >>> LoadModule wsgi_module >>> "c:/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd" >>> WSGIPythonHome "c:/python36" >>> >>> Is there any existing LoadModule line for wsgi_module anywhere else in >>> the httpd.conf file. Answer no - confirmed by file search >>> >>> Hope that sheds some light. >>> >>> >>> >>> >>> On Thursday, August 17, 2017 at 10:24:33 AM UTC+1, Graham Dumpleton >>> wrote: >>>> >>>> >>>> On 17 Aug 2017, at 7:15 pm, Mike Heath <[email protected]> wrote: >>>> >>>> I am on a PC running Windows 10. I am in the early stages of learning >>>> python and flask for web development purposes and I want to set up a local >>>> development environment. I have been using wamp for php development and >>>> have a successful installation of wampserver 3.0.6 64bit which runs Apache >>>> 2.4.23. >>>> >>>> >>>> Can you confirm that you are using the same Apache instance for PHP at >>>> the same time as wanting to use it for Python? >>>> >>>> I now want to use that for python/flask and the way to do that appears >>>> to be via mod_wsgi. Thanks to an exchange with Graham Dumpleton in >>>> Stackoverflow I am aware of the documentation for mod_wsgi and have read >>>> that but clearly I have not fully understood it or complied with it >>>> correctly and the system isn't working. >>>> >>>> Incidentally I have managed to get a VirtualEnvironment working which I >>>> think uses an inbuilt python server but I don't think that solves the >>>> problem of connecting to a database. >>>> >>>> Anyway this is what I have done so far: >>>> >>>> 1. Successfully installed Python 3.6 >>>> >>>> 2. Successfully installed flask (evidence for which is that the >>>> VirtualEnvironment works) >>>> >>>> 3. Successfully (I think) installed mod_wsgi via 'pip install mod_wsgi' >>>> >>>> 4. Run command 'mod_wsgi-express module-config' and copied the output >>>> to my apache httpd.conf file. >>>> >>>> 5. Changed the httpd.conf LogLevel to 'info' >>>> >>>> 6. Commented out the line 'AddHandler cgi-script .cgi .py' >>>> >>>> 7. Created simple wsgi application (copy/paste from documentation), >>>> called 'test.wsgi' in a folder C:\wamp\www\wsgi_script >>>> >>>> 8. Set up a virtual host in the httpd-vhosts.conf file using the >>>> following code: >>>> >>>> <VirtualHost *:80> >>>> ServerName localhost >>>> WSGIScriptAlias /test C:/wamp/www/wsgi_script/test.wsgi >>>> <Directory C:/wamp/www/wsgi_script> >>>> Order allow,deny >>>> Allow from all >>>> </Directory> >>>> </VirtualHost> >>>> >>>> >>>> Do you have any other VirtualHost definitions in the httpd.conf file? >>>> >>>> If you do and the other one is set up for PHP this second VirtualHost >>>> would be ignored. Post the details of the over VirtualHost. >>>> >>>> Also, what is DocumentRoot set to in the httpd.conf file? >>>> >>>> which I expect may be wrong but I don't understand it enough to >>>> know what. >>>> >>>> 9. Run a check on the server via >>>> 'C:\wamp\bin\apache\apache2.4.23\bin>httpd -V' >>>> which seemed to give a satisfactory response showing the version as >>>> 2.4.23 and the MPM as WinNT. >>>> >>>> 10. In the browser (Firefox) entered following in url bar ' >>>> http://localhost/wsgi_script/test.wsgi'. The output is merely the raw >>>> code of the file. >>>> >>>> There are things I expected to happen that didn't eg >>>> >>>> After installing mod_wsgi, I expected a module 'mod_wsgi.so' to appear >>>> in the apache modules folder but it did not. >>>> >>>> >>>> When C extensions are compiled by Python they can have a .pyd or .pyo >>>> extension. >>>> >>>> I expected to have to load that module via a LoadModule line in >>>> httpd.conf but didn't see any advice to that effect. >>>> >>>> >>>> Step 4 would have output what to include in the httpd.conf file. That >>>> would have included LoadFile, LoadModule and WSGIPythonHome directives. >>>> The >>>> LoadModule line output at step 4 would use whatever is appropriate >>>> extension, which need not be .so. >>>> >>>> What did step 4 output and what did you add to the httpd.conf file? >>>> >>>> Is there any existing LoadModule line for wsgi_module anywhere else in >>>> the httpd.conf file. >>>> >>>> When I look at the containing fiolder in the browser ie >>>> 'http://localhost/wsgi_script/' >>>> I see the 'index of' page at the bottom of which is >>>> Apache/2.4.23 (Win64) PHP/5.6.25 mod_wsgi/4.5.17 Python/3.6 Server >>>> at localhost Port 80 >>>> which seems to indicate that apache is recognising mod_wsgi >>>> >>>> Having tried to access the wsgi file, the apache error log read: >>>> >>>> [Thu Aug 17 00:07:41.135713 2017] [auth_digest:notice] [pid 5416:tid >>>> 540] AH01757: generating secret for digest authentication ... >>>> [Thu Aug 17 00:07:41.151640 2017] [wsgi:info] [pid 5416:tid 540] >>>> mod_wsgi (pid=5416): Python home c:/python36. >>>> [Thu Aug 17 00:07:41.151640 2017] [wsgi:info] [pid 5416:tid 540] >>>> mod_wsgi (pid=5416): Initializing Python. >>>> [Thu Aug 17 00:07:41.168231 2017] [wsgi:info] [pid 5416:tid 540] >>>> mod_wsgi (pid=5416): Attach interpreter ''. >>>> [Thu Aug 17 00:07:41.182267 2017] [wsgi:info] [pid 5416:tid 540] >>>> mod_wsgi (pid=5416): Imported 'mod_wsgi'. >>>> [Thu Aug 17 00:07:41.184272 2017] [mpm_winnt:notice] [pid 5416:tid 540] >>>> AH00354: Child: Starting 64 worker threads. >>>> [Thu Aug 17 09:54:05.412979 2017] [core:info] [pid 5416:tid 1260] >>>> [client 127.0.0.1:50861] AH00128: File does not exist: C:/wamp/www/test >>>> >>>> I hope this is enough information to pick up my errors and suggest >>>> corrections or further tests. >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "modwsgi" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at https://groups.google.com/group/modwsgi. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>>> >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "modwsgi" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at https://groups.google.com/group/modwsgi. >>> For more options, visit https://groups.google.com/d/optout. >>> >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "modwsgi" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at https://groups.google.com/group/modwsgi. >> For more options, visit https://groups.google.com/d/optout. >> >> >> > -- > You received this message because you are subscribed to the Google Groups > "modwsgi" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] <javascript:>. > To post to this group, send email to [email protected] <javascript:> > . > Visit this group at https://groups.google.com/group/modwsgi. > For more options, visit https://groups.google.com/d/optout. > > > -- You received this message because you are subscribed to the Google Groups "modwsgi" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/modwsgi. For more options, visit https://groups.google.com/d/optout.
