Flask + Mod_wsgi + Apache on Windows deployment is successful (ask questions at any time)

Source: Internet
Author: User


Say is preface, pure is spit groove. If you are in a hurry, you can skip this part and I promise not to hide anything useful here.

When people are getting old, they may be running out of momentum, and it took me about two weeks to successfully deploy flask to Windows. Fortunately did not give up, finally found the answer.

But it also shows that I am in the application and configuration of open source software is still very poor ah, when I see To use the VC10 C + + compiler to compile the source code, the head is big. You said that we are usually accustomed to vistual studio compiled, which also know what make command AH. Fortunately there are everything on the Internet, even help you compile all have, therefore, compile this difficult not experienced.

For open-source software like flask, there are always multiple options for deployment, and I'm now throwing up the groove I've tried, but there's no successful experience.

    1. Pyisapie + IIS
    2. CGI + IIS (although configuration is successful, but cannot parse Wsgi)
    3. FastCGI + Nginx
    4. FastCGI + Lighttpd (requires Cygwin for compilation)
Of course, flask official has a deployment document, but it's too simple to write. But I still stick the link out.http://dormousehole.readthedocs.org/en/latest/deploying/index.html

Reference https://claudiosparpaglione.wordpress.com/2013/03/06/how-to-deploy-flask-applications-to-apache-webserver/

I've consulted a lot of links, but this article has led me to the straight path.
Final SolutionFlask + mod_wsgi + ApacheFlask: A python Web framework Mod_wsgi: A python-written Apache fastcgi module Apache: A Web server similar to IIS
!! Attention!!! If this is not done, there is a 99% probability that the whole process will fail. both Apache,mod_wsgi and Python must be generated with the same version of the C + + compiler, which is either 32-bit or 64-bit and cannot be mixed.

Because the Python 3.4 i downloaded from the official Python site is 32-bit and is compiled with VC10. So when you download Apache and MOD_WSGI, you must also select the 32-bit version that was compiled with VC10. I did not notice the problem before, so it has not been installed successfully.
For a detailed explanation of this question, see: Https://github.com/GrahamDumpleton/mod_wsgi/blob/master/win32/README.rst

In addition, the official Python site appears to be a 32-bit version, if you want 64-bit, it is estimated that you need to build it yourself.
compiler under Windows, see: Http://www.microsoft.com/express/vc/

    1. Installing Apache
    2. Installing MOD_WSGI
    3. Install Python
    4. Installing flask
    5. Create a test Web App
    6. Configure the site in Apache
    7. Test
1. Installation of Apacheapache is open source software, for Windows environments, it does not directly provide a compiled version.
You can download the appropriate version of your environment in http://www.apachelounge.com/download/. Based on my own current environment, I chose Win32 VC10 compiled Apache2.4 version
Download the package to this computer, and then copy the Apache24 folder inside the compressed package to C: \.
Of course, you can copy to any location on your system, but Apache's default configuration is C:\Apache24.

If you run IIS on this computer, turn it off. Because IIS and Apache both use port 80 by default. If you want to configure other ports, I'm sure it's not hard. Wait until the flask has been successfully deployed.

Open cmd
>CD c \
>CD apache24\bin\

Then open the browser and enter

If the page appearsIt works!, that means the Apache server is up and running.

2. Install Mod_wsgi This module can not be casually installed, it is the key to start Python.I don't know why the installation of this module is not listed on Flask's official site. Just simply say a bit about the configuration of httpd.confg, it is too irresponsible.
Download Mod_wsgi https://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-win32-ap22py27-3.3.so from the link below
This package contains 32-bit and 64-bit compiled versions, and after the download is complete select mod_wsgi-windows-4.4.12.tar\mod_wsgi-windows-4.4.12\apache24-win32-vc10\modules\mod_wsgi-py34-vc10.so。 Because the python I am currently installing is 32-bit, you must choose Mod_wsgi-py34-vc10.so

Copy the mod_wsgi-py34-vc10.so to C:\Apache24\modules\ and rename it to mod_wsgi.so.

Open c:\Apache24\conf\httpd.conf Add the following configuration
LoadModule Wsgi_module modules/mod_wsgi.so

From the new start httpd, if there is no error, indicating that the MOD_WSGI module in Apache loaded successfully.

3. Install python I am installing Python 3.4 here, which is installed by default in the C:\Python34\ directory.
When you install Python 3.4, it is a good idea to add Python to the system directory. After the installation is complete, you can run the Python program directly, such as the PIP used to install flask below.

4. Installing flask It's ironic that I forgot to install flask while trying to deploy flask applications. Cause I see the error message that Apache has burst on the webpage. At the beginning of the period I also think that is the problem of Apache, after looking at C:\Apache24\logs\error.log only found that the original is flask not installed.
Open cmd directly and run the following command
Pip Install flask

It will automatically load all the two libraries that flask and flask depend on, which is very convenient.

5. Create a web App the code below is copied directly
Create C:\Test_Web\test.py
From flask import flask, Requestapp = Flask (__name__) @app. Route ('/hello ') def hello_world (): name = Request.args.get (' Name ', ') return ' Hello ' + name + '! ' if __name__ = = ' __main__ ': App.run ()

Create C:\Test_Web\test.wsgi
Import Sys#expand Python classes path with your app's Pathsys.path.insert (0, "C:/test_web") from Test import App#put loggin G Code (and imports) here ... #Initialize WSGI app objectapplication = App

Note that application must not be changed into something else. Because MOD_WSGI in parsing this file, only recognize application.
Also, unlike IIS, you do not need to add Network service users access to the C:\Test_Web folder.

6. Configure the site in Apache simply add the following code to the C:\Apache24\conf\httpd.conf file.
<virtualhost *:80 >serveradmin [email protected]documentroot c:\Test_Web <directory "C:\Test_Web" >order Allow,denyallow from all </directory>wsgiscriptalias/flasktest c:\test_web\test.wsgi</virtualhost>

My configuration of Apache is purely layman. My understanding is that a VirtualHost node is equivalent to a Web site node in IIS. If my understanding is wrong, I also hope you pass the Apache configuration master correction.
In my research, I also saw someone put the site's configuration in another conf file, and then include it in the httpd.conf.

7. Test now, open the browser, enter Http://localhost/flasktest/hello?name=CZY

If you see a page that appearsHello czy!It means your site is up and running.

The rest of the job is to start the Python web App development journey with the current work prototype. If you encounter any problems in the process of trying, please give me a message at any time on csdn.

Flask + Mod_wsgi + Apache on Windows deployment is successful (ask questions at any time)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.