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

Source: Internet
Author: User
Tags win32

Preface

Say is preface, pure is spit groove.

Suppose you are in a hurry. Completely able to skip this part, I promise not to hide it here no matter what practical content.

People in the elderly. Probably not enough momentum, 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 VC10 C + + compiler to compile the source code, the head is big. You say that we are accustomed to vistual studio compiled. What do you know about make orders?

Fortunately there are everything on the Internet, even help you compile all have, therefore, compile this difficult not experienced.

For flask This open source software, there are always multiple options for deployment. I'm now throwing up the groove I tried, but there was no successful experience.

    1. Pyisapie + IIS
    2. CGI + IIS (although the configuration is successful, it cannot parse Wsgi)
    3. FastCGI + Nginx
    4. FastCGI + Lighttpd (requires Cygwin to compile)
Of course, flask official has a document on deployment. But it's too easy to write. But I still stick the link out.

http://dormousehole.readthedocs.org/en/latest/deploying/index.html


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

I have been involved in a lot of links, but it is this article that leads me to the good.


Finally the solutionFlask + mod_wsgi + ApacheFlask: A python Web framework Mod_wsgi: An Apache fastcgi module written for Python: An IIS-like webserver
! Attention!

!!

Suppose the following is not done. The entire process has a 99% chance of failure.

both the Apache,mod_wsgi and Python must be generated with the same version number of A/C + + compiler, which is either 32-bit or 64-bit and cannot be mixed.

Because the Python 3.4 i downloaded from the Python official website is 32-bit. And it's compiled with VC10. So when you download Apache and Mod_wsgi. You must also select a 32-bit version number that is compiled with VC10.

I did not notice the problem before, so it has not been successfully installed.


The explanation of this question, see: Https://github.com/GrahamDumpleton/mod_wsgi/blob/master/win32/README.rst

In addition, the Python official website seems to offer a 32-bit version number. Assuming 64-bit, it is expected that you will need to compile it yourself.
The compiler under Windows. See: Http://www.microsoft.com/express/vc/



Steps
    1. Installing Apache
    2. Installing MOD_WSGI
    3. Install Python
    4. Installing flask
    5. Create a test Web App
    6. Configure the Web site in Apache
    7. Test
1. Install Apacheapache is open source software for the Windows environment. It does not directly provide a compilation version number.
Ability to download version numbers for your environment in http://www.apachelounge.com/download/. According to my own current environment. I chose the Win32 VC10 compiled Apache2.4 version number.
Download the compressed package to this machine, and then copy the Apache24 directory inside the compressed package to C: \.


Of course, you can copy to your system regardless of location, but Apache's default configuration is C:\Apache24.

Assume that you have IIS executed natively. Turn it off. Because both IIS and Apache use the default of 80port. Suppose you want to configure another port. I believe that it is not difficult. Wait until the flask has been successfully deployed.

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

Then open the browser and enter
http://localhost

Assuming the it works! is displayed on the Web page, that means Apacheserver is executing.


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 website. Simply saying a little bit about the configuration of the httpd.confg 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
The package contains 32-bit and 64-bit compiled version numbers. Select mod_wsgi-windows-4.4.12.tar\mod_wsgi-windows-4.4.12\apache24-win32-vc10\modules\ after downloadmod_wsgi-py34-vc10.so。 Since the python I am currently installing is 32-bit, you must select 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 Join for example the following configuration
LoadModule Wsgi_module modules/mod_wsgi.so

New start httpd, assuming no error. Description The Mod_wsgi module was loaded successfully in Apache.

3. Install python I am installing Python 3.4 here. The default installation is under the C:\Python34\ folder.
Python 3.4 is installed. It is a good idea to add Python to the System folder. After the installation, you can directly execute the Python program, such as the following to install the flask to use the PIP


4. Installing flask It's ironic that. I forgot to install flask while trying to deploy the flask application. 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 reviewing C:\Apache24\logs\error.log only found that the original is flask not installed.
Open cmd directly. Execute such as the following command
Pip Install flask

It will be on its own initiative to flask and flask rely on the two library all installed, this is too convenient.

5. Create a web App The following code 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, just recognize application.


Other than that. Unlike IIS, there is no need to increase the access rights of network service users to the C:\Test_Web directory.



6. Configuring the site in Apache only requires adding 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.

Assuming that my understanding is wrong, but also hope that you pass the Apache configuration master correction.
In the course of my research, I also saw someone put the site's configuration in another conf file. It is then included to httpd.conf.




7. Test today. Open your browser. Input Http://localhost/flasktest/hello?name=CZY

Suppose you see a page that appears onHello czy!It means that your site is executing.

The rest of the job is to start the Python web App development journey with the current work prototype.

Let's say you're having trouble trying. Please give me a message at any time on the csdn.







































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

Related Article

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.