Flask + mod_wsgi + Apache on Windows is deployed successfully (you can ask questions at any time), flaskmod_wsgi

Source: Internet
Author: User

Flask + mod_wsgi + Apache on Windows is deployed successfully (you can ask questions at any time), flaskmod_wsgi
Preface

The preface is purely an explanation. If you are in a hurry, you can skip this part. I promise not to hide any useful content here.

When people are older, they may be overwhelmed. It took me about two weeks to successfully deploy flask on windows. Fortunately, I did not give up and finally found the answer.

However, this also shows that the application and configuration of open-source software are still poor. When I see that the source code is compiled using the C/C ++ compiler of VC10, the header is too large. You said that we are used to compiling Vistual Studio. What else do you know about the make command. Fortunately, everything is available on the Internet and even compiled for you. Therefore, compilation is hard to achieve.

For open-source software such as flask, there are always a variety of options for deployment. I have tried it but I have not experienced it successfully.

Of course, flask has official deployment documents, but it is 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 have referenced many links, but this article leads me to the right path.
Final Solution Flask + mod_wsgi + ApacheFlask: A Python web framework mod_wsgi: A FastCGI module of Apache compiled for Python Apache: A Web server similar to IIS
!!! Note !!! If this is not done below, 99% of the possibility of failure will occur throughout the process. Apache, mod_wsgi, and Python must both be generated using the C/C ++ compiler of the same version. They are either 32-bit or 64-bit, and cannot be mixed.

Because the Python 3.4 I downloaded from the Python official site is 32-bit and compiled with VC10. Therefore, when downloading Apache and mod_wsgi, You must select a 32-bit version compiled with VC10. I did not notice this problem before, so the installation was not successful.
For a detailed explanation of this problem, see: https://github.com/GrahamDumpleton/mod_wsgi/blob/master/win32/README.rst

In addition, Python official site provides 32-bit versions. If you need 64-bit versions, you may need to compile them yourself.
Windows compiler, see: http://www.microsoft.com/express/vc/



Step 1. Install ApacheApache as an open-source software. For windows environments, it does not directly provide the compiled version.
You can download a version that fits your environment in the http://www.apachelounge.com/download. Based on my current environment, I chose the Apache2.4 version compiled by Win32 VC10.
Download the compressed package to the local machine, and then copy the Apache24 folder in the compressed package to C :\.
Of course, you can copy data to any location of your system, but the default configuration of Apache is C: \ Apache24.

If your local machine runs IIS, turn it off. Because both IIS and Apache use port 80 by default. If you want to configure other ports, I believe it is not difficult. Wait until the Flask deployment is successful.

Open cmd
> Cd c :\
> Cd Apache24 \ bin \
> Httpd

Open the browser and enter
Http: // localhost

If It Works is displayed on the webpage! The apache server is running.


2. The module mod_wsgi cannot be installed at will. It is the key to starting Python. I don't know why the installation of this module is not listed on the official site of Flask. Simply put, the configuration of httpd. confg is too irresponsible.
Download the mod_wsgi https://code.google.com/p/modwsgi/downloads/detail from the following link? Name = mod_wsgi-win32-ap22py27-3.3.so
This package contains a 32-bit and 64-bit compiled version, after the download is complete, select mod_wsgi-windows-4.4.12.tar \ mod_wsgi-windows-4.4.12 \ modules \ Apache24-win32-VC10. Because 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 mod_wsgi.so.

Open c: \ Apache24 \ conf \ httpd. conf and add the following Configuration:
LoadModule wsgi_module modules/mod_wsgi.so

Restart httpd. If no error is reported, the mod_wsgi module is successfully loaded in apache.

3. Install Python. Python 3.4 is installed here. It is installed in the c: \ Python34 \ directory by default.
When installing Python 3.4, it is best to add Python to the system directory. After installation, you can directly run Python-related programs, such as pip


4. Installing flask is ironic that I forgot to install flask when trying to deploy the flask application. As a result, I can see the apache error message on the webpage. I also thought it was an Apache issue at the beginning. After checking c: \ Apache24 \ logs \ error. log, I found that flask was not installed.
Open cmd and run the following command:
Pip install flask

It automatically installs flask and flask dependent libraries, which is too convenient.

5. Create a Web App and copy the following code 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 logging code (and imports) here ...#Initialize WSGI app objectapplication = app


Note that the application cannot be changed to another one. Because mod_wsgi only recognizes application when parsing this file.
In addition, different from IIS, you do not need to add the network service user access permission to the C: \ Test_Web folder.

6. to configure the site in Apache, add the following code to the C: \ Apache24 \ conf \ httpd. conf file.
<VirtualHost *:80 >ServerAdmin example@company.comDocumentRoot c:\Test_Web <Directory "c:\Test_Web">Order allow,denyAllow from all </Directory>WSGIScriptAlias /flasktest c:\Test_Web\test.wsgi</VirtualHost>


I am a layman in Apache configuration. In my understanding, a VirtualHost node is equivalent to a Web Site node in IIS. If my understanding is incorrect, I hope you can correct the Apache configuration.
During my research, I also saw someone put the site configuration in another conf file, and then included it in httpd. conf.


7. Test now. Open the browser and enter http: // localhost/flasktest/hello? Name = CZY

If you see Hello CZY on the webpage! It indicates that your site is running.

The rest of the work is to start Python Web App development based on the current work. If you encounter any problems during your attempt, please leave a message on CSDN 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.