xampp apache_在Windows上使用Xampp Apache服务Flask应用程序

本文档翻译自Medium文章,介绍如何在Windows操作系统中使用Xampp的Apache服务来部署和运行Flask应用程序,为Python开发者提供了一个在本地环境运行Web应用的指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xampp apache

As we all know Flask, Flask is a web microframework written in python. Flask has a lot of advantages including (for myself) is an easy and agile way of developing web services. Unfortunately, not really agile in a way of deploying into production. That’s why I decided to make this article to tell people how difficult and hideous it is to deploy a Flask app to XAMPP Apache web service and another reason is to make sure I won’t get trouble if I had to deploy Flask again.

众所周知,Flask是用python编写的Web微框架。 Flask有很多优点,包括(对我自己而言)是一种开发Web服务的简单而敏捷的方法。 不幸的是,在部署到生产环境中并没有真正敏捷。 这就是为什么我决定写这篇文章来告诉人们将Flask应用程序部署到XAMPP Apache Web服务有多么困难和可怕,另一个原因是要确保如果我不得不再次部署Flask不会遇到麻烦。

So, to deploy our Flask app, our main module to use is called mod-wsgi. This library is maintained by someone called Graham Dumpleton (even though I wrote this article without asking his approval), and I’d like to say thank you so much for maintaining this wonderful tools for us.

因此,要部署我们的Flask应用,我们要使用的主要模块称为mod-wsgi。 这个库是由一个叫Graham Dumpleton的人维护的(即使我写这篇文章时并没有征得他的同意),我要非常感谢您为我们维护了这个很棒的工具。

Now let’s jump into technical things. Before I know anything about mod-wsgi, I found a tutorial by Thilina Madumal on how exactly to serve a Flask app on Apache. And before you read this article any further, I suggest you read that article first in here.

现在让我们跳入技术性话题。 在不了解mod-wsgi之前 ,我找到了Thilina Madumal的教程,内容涉及如何在Apache上准确地提供Flask应用程序。 而且,在您进一步阅读本文之前,建议您首先在此处阅读该文章。

先决条件 (Prerequisites)

As mentions by Madumal in his article above, you’re going to need to prepare some installations.

正如Madumal在他的上一篇文章中提到的那样,您将需要准备一些安装。

  1. Visual C++ Redistributable for Visual Studio

    Visual Studio的Visual C ++可再发行组件
  2. Visual C++ Build Tools (https://visualstudio.microsoft.com/visual-cpp-build-tools/)

    Visual C ++生成工具(https://visualstudio.microsoft.com/visual-cpp-build-tools/)
  3. python3 (Installed for all user, so make sure installation directory doesn’t go to “C:/Users”)

    python3(已为所有用户安装,因此请确保安装目录未转到“ C:/ Users”)
  4. python3-pip

    python3-pip
  5. XAMPP Apache

    XAMPP Apache

And this is going to be very important in windows machine

这在Windows机器中将非常重要

MAKE SURE YOU INSTALLED PYTHON, APACHE, AND VISUAL STUDIO AS THE SAME BINARY. For example, all of them are 32-bit version OR all of them are 64-bit version.

确保您将PYTHON,APACHE和VISUAL STUDIO安装为相同的二进制文件。 例如,它们都是32位版本,或者它们都是64位版本。

Because if you didn’t make all of that with the same binary, you’re going to face a lot of errors. This statement also mentioned by Graham in this link. As for me myself, I used the 64-bit version of Python 3.8, and 64-bit version of XAMPP Apache (yes, there is a 64-bit version of it).

因为如果不使用相同的二进制文件来实现所有功能,那么您将面临很多错误。 Graham在此链接中也提到了此声明。 至于我自己,我使用的是Python 3.8的64位版本和XAMPP Apache的64位版本(是的,它有64位版本)。

After you installed all of that to your machine, I’m going to assume you have installed python and apache in an easy directory.

在将所有这些安装到计算机上之后,我将假定您已在简单目录中安装了python和apache。

For Python directory, I’m assuming it will be residing in the program files directory, so it’s going to be something like “C:/Program Files/Python”. Because that’s the default path for all user installation. We are going to call that <Python Home> directory for this article.

对于Python目录,我假设它将位于程序文件目录中,因此它将类似于“ C:/ Program Files / Python”。 因为这是所有用户安装的默认路径。 我们将在本文中将该目录称为<Python Home>目录。

And for XAMPP Apache, it would be something like “C:/XAMPP/Apache” and we’ll call that <Apache Home> directory.

对于XAMPP Apache,它将类似于“ C:/ XAMPP / Apache”,我们将其称为<Apache Home>目录。

将Apache作为Windows服务运行 (Run Apache as a Windows Service)

Now, I can’t tell you right now on why would you need to install it as a windows service. Because I personally don’t understand it either. But from my personal experience, using apache as a windows service can help me understanding an occurring error. To do that, open up your command prompt as administrators.

现在,我现在无法告诉您为什么需要将其安装为Windows服务。 因为我个人也不了解。 但是从我的个人经验来看,使用apache作为Windows服务可以帮助我理解发生的错误。 为此,请以管理员身份打开命令提示符。

Change your directory to <Apache Home>/bin

将目录更改为<Apache Home> / bin

cd C:/XAMPP/Apache/bin

cd C:/ XAMPP / Apache / bin

After that type a command

之后输入命令

httpd.exe -k install

httpd.exe -k安装

This is what I mean by understanding errors in apache. Because as you might not know, the whole process of serving Flask in apache won’t show most errors in the error logs. And because of that, I was having a hard time understanding what’s going on with my apache. Using it as windows services I can control most of what’s going on with my apache.

这就是我理解Apache错误的意思。 因为您可能不知道,所以在Apache中投放Flask的整个过程不会在错误日志中显示大多数错误。 因此,我很难理解我的apache发生了什么。 使用它作为Windows服务,我可以控制我的Apache的大部分工作。

Now after you installed it as windows services, you can use another command including start, stop, restart the server, or even uninstalling apache from windows service. Use the command like

现在,将其安装为Windows服务后,您可以使用另一个命令,包括启动,停止,重新启动服务器,甚至从Windows服务中卸载apache。 使用类似的命令

httpd.exe -k [command]

httpd.exe -k [命令]

After you start the server, test it by opening your localhost and it should show xampp welcome page.

启动服务器后,通过打开本地主机对其进行测试,它应显示xampp欢迎页面。

安装mod_wsgi (Installing mod_wsgi)

Now, the first wave of errors is going to show up from this step onward. Whatever errors you might get you need to stay calm and cold-minded. I’ll try to address most of the errors I encountered myself, as well as steps on how to deal with it.

现在,从这一步开始将出现第一波错误。 无论您可能遇到什么错误,都需要保持镇定并保持冷静。 我将尝试解决我遇到的大多数错误,以及有关如何处理它的步骤。

Before you install mod-wsgi directly, I assume most of you are using the virtual environment to develop a Flask app. But if you didn’t, it’s not a big deal. I will suggest you to make your virtual environment inside your working project, that way it will be easier to install a requirement for your project (although many people oppose this method). So, if you worked with a virtual environment, prepare it before you install mod-wsgi.

在直接安装mod-wsgi之前,我假设你们中的大多数人都在使用虚拟环境来开发Flask应用程序。 但是,如果您不这样做,那不是什么大问题。 我建议您将虚拟环境放入正在运行的项目中,这样可以更轻松地为项目安装需求(尽管许多人反对这种方法)。 因此,如果您使用的是虚拟环境,请在安装mod-wsgi之前进行准备。

Thilina Madumal in his tutorial suggests activating your virtual environment before installing mod-wsgi. But, I’m going to suggest you to not use or activate your virtual environment. Because I had trouble loading mod-wsgi to Apache if I used a virtual environment, so I installed mod-wsgi directly to my main Python directory. Don’t worry, you’ll still be able to use a virtual environment for your project.

Thilina Madumal在他的教程中建议在安装mod-wsgi之前激活您的虚拟环境。 但是,我建议您不要使用或激活您的虚拟环境。 如果使用虚拟环境,由于在将mod-wsgi加载到Apache时遇到麻烦,因此我将mod-wsgi直接安装到我的主Python目录中。 不用担心,您仍然可以在项目中使用虚拟环境。

Now we are going to pip install mod-wsgi to Python. Before you type pip install command in the command prompt, I suggest you set an environment variable first. It’s as easy as

现在,我们将pip install mod-wsgi安装到Python。 在命令提示符下键入pip install命令之前,建议您首先设置一个环境变量。 就像

set MOD_WSGI_APACHE_ROOTDIR=<Apache Home>

设置MOD_WSGI_APACHE_ROOTDIR = <Apache主页>

This is just to ensure no errors that say “no apache directory set”. After that we can finally pip install mod-wsgi

这只是为了确保没有显示“未设置apache目录”的错误。 之后,我们终于可以pip install mod-wsgi

pip install mod-wsgi

点安装mod-wsgi

Now, if you encounter any errors doing this step, I suggest you try installing a different version of mod-wsgi in this link. I encountered 3 types of errors while installing mod-wsgi. First, no apache directory set, second is an unresolved external symbol, and third is filename.whl is not supported wheel on this platform.

现在,如果在执行此步骤时遇到任何错误,建议您尝试在此链接中安装其他版本的mod-wsgi。 安装mod-wsgi时遇到3种类型的错误。 首先,未设置apache目录,其次是未解析的外部符号,其次是filename.whl在此平台上不受支持。

The first error we can avoid by setting the apache root directory. The second and third errors, however, are a bit tricky. Basically, you need the right version of mod-wsgi for your python, so try to install it using a different version from the link I mentioned before.

我们可以通过设置apache根目录来避免第一个错误。 但是,第二个和第三个错误有些棘手。 基本上,您需要适用于python的正确版本的mod-wsgi,因此请尝试使用与我之前提到的链接不同的版本来安装它。

在Apache服务器中使用mod-wsgi (Using mod-wsgi in Apache Server)

After you successfully installed mod-wsgi to your Python, run this command in command prompt

将mod-wsgi成功安装到Python后,在命令提示符下运行此命令

mod_wsgi-express module-config

mod_wsgi-express模块​​配置

It will print a configuration for us to put into the apache. It should print out something like

它将打印出配置供我们放入apache中。 它应该打印出类似

LoadFile “C:/Python38/python38.dll”

加载文件“ C:/Python38/python38.dll”

LoadModule wsgi_module “C:/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd”

LoadModule wsgi_module“ C:/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd”

WSGIPythonHome “C:/Python38”

WSGIPythonHome“ C:/ Python38”

Copy the output of that command. Now open up apache configuration file in <Apache Home>/conf/httpd.conf and paste the output before any LoadModule statement.

复制该命令的输出。 现在,在<Apache Home> /conf/httpd.conf中打开apache配置文件,并将输出粘贴到任何LoadModule语句之前。

Note: For further details please refer to https://github.com/GrahamDumpleton/mod_wsgi#connecting-into-apache-installation

注意:有关更多详细信息,请参阅https://github.com/GrahamDumpleton/mod_wsgi#connecting-into-apache-installation

You might want to restart and test your apache server at this point because I also encountered an error in this step. But I dealt with that error by simply installing mod-wsgi in my main python directory (without virtual environment). If somehow you insist to install it in your virtual environment, the LoadModule and PythonHome path will be different from the example above (it will point to your virtual environment directory). But, as long as no errors shown up and your server is starting without problem, then it’s okay to use a virtual environment.

您可能此时需要重新启动并测试apache服务器,因为在此步骤中我也遇到了错误。 但是我通过在我的主python目录中安装mod-wsgi(没有虚拟环境)来解决该错误。 如果您以某种方式坚持要在虚拟环境中安装它,则LoadModule和PythonHome路径将与上面的示例不同(它将指向您的虚拟环境目录)。 但是,只要没有错误出现,并且服务器启动没有问题,那么使用虚拟环境就可以了。

Another error I encountered is “Cannot load module into server: %1 is not a valid Win32 application”. These errors mean there’s a different binary version of application you’re using. Either it’s Python, Apache, or even mod-wsgi itself.

我遇到的另一个错误是“无法将模块加载到服务器中:%1不是有效的Win32应用程序”。 这些错误意味着您使用的是其他二进制版本的应用程序。 是Python,Apache,甚至是mod-wsgi本身。

After dealing with that errors and get your server running again, you might want to add a listen port. This is so you won’t be using the default port Apache is using, which is port 80. Just add a line “Listen <port>” in your httpd.conf

处理完这些错误并使服务器再次运行后,您可能需要添加一个侦听端口。 这样一来,您将不会使用Apache使用的默认端口,即端口80。只需在httpd.conf中添加一行“ Listen <port>”

使用VirtualHost for Flask App (Using VirtualHost for Flask App)

After we configure the main config, open up the virtual host config in apache. It’s located at <Apache Home>/conf/extra/httpd-vhosts.conf. Now copy this line in your vhost configuration

配置完主配置后,在apache中打开虚拟主机配置。 它位于<Apache Home> /conf/extra/httpd-vhosts.conf。 现在在您的vhost配置中复制此行

<VirtualHost *:5000>

<VirtualHost *:5000>

ServerAdmin admin-name-here

ServerAdmin管理员名称-此处

ServerName server-name-here (e.g:localhost:5000)

ServerName服务器名称-此处(例如:localhost:5000)

WSGIScriptAlias / “F:/myapp/app/index/web.wsgi”

WSGIScriptAlias /“ F:/myapp/app/index/web.wsgi”

DocumentRoot “F:/myapp”

DocumentRoot“ F:/ myapp”

<Directory “F:/myapp/app/index”>

<目录“ F:/ myapp / app / index”>

Require all granted

要求所有授予

Options Indexes FollowSymLinks Includes ExecCGI

选项索引FollowSymLinks包括ExecCGI

</Directory>

</目录>

AddHandler wsgi-script .wsgi

AddHandler wsgi-script .wsgi

ErrorLog “F:/myapp/logs/error.log”

错误日志“ F:/myapp/logs/error.log”

CustomLog “F:/myapp/logs/access.log” common

CustomLog“ F:/myapp/logs/access.log”常见

</VirtualHost>

</ VirtualHost>

If you haven’t created an error log or access log, you may need to create it manually. These logs will prove to be useful from here onward.

如果尚未创建错误日志或访问日志,则可能需要手动创建。 从这里开始,这些日志将被证明是有用的。

You might notice that my configuration is a little bit different from Thilina Madumal’s configuration here. That is because his configuration apparently isn't enough for my case. And as he mentions there, there are three important things in that configuration.

你可能会注意到,我的配置是从Thilina Madumal的配置稍有不同在这里 。 那是因为他的配置显然不足以满足我的要求。 正如他提到的那样,该配置中包含三项重要内容。

  1. Server Name (Domain for your app if you have it, or simply localhost)

    服务器名称(如果有应用程序,则为您的应用程序使用域,或者仅是localhost)
  2. WSGIScriptAlias (Tells apache what script to execute when there’s a request for that domain)

    WSGIScriptAlias(告诉apache在对该域有请求时执行什么脚本)
  3. <Directory> (For security purpose, it should allow access to wsgi script but not the entire project)

    <Directory>(出于安全目的,它应允许访问wsgi脚本,但不允许访问整个项目)

So what did I add to the configuration? Apparently, if I run a configuration like Thilina Madumal’s, I get an error “AH02809: Options ExecCGI is off in this directory: F:/myapp/app/index/web.wsgi”. So I add a line inside Directory tag

那么我在配置中添加了什么? 显然,如果我运行Thilina Madumal这样的配置,则会收到错误消息“ AH02809:此目录中的Options ExecCGI已关闭:F:/myapp/app/index/web.wsgi”。 所以我在Directory标签内添加了一行

Options Indexes FollowSymLinks Includes ExecCGI

选项索引FollowSymLinks包括ExecCGI

It may just need the ‘Includes ExecCGI’ part, but I copied the entire line from Apache’s main config (httpd.conf). After that, I still got an error. This time it says “F:/myapp/app/index/web.wsgi is not executable; ensure interpreted scripts have “#!” or “‘!” first line”. That error statement suggests you to add what we called Shebang or interpreter. But I didn't need that. I just need to add a line

它可能只需要'Includes ExecCGI'部分,但是我从Apache的主要配置(httpd.conf)复制了整行。 之后,我仍然出现错误。 这次它说“ F:/myapp/app/index/web.wsgi不可执行; 确保解释后的脚本具有“#!” 要么 ”'!” 第一行”。 该错误声明建议您添加我们所谓的Shebang或解释器。 但是我不需要。 我只需要添加一行

AddHandler wsgi-script .wsgi

AddHandler wsgi-script .wsgi

This is to ensure that wsgi script would be handled by apache handler.

这是为了确保wsgi脚本将由apache处理程序处理。

And that’s what I changed in virtual host configuration. You might not get these errors yet because you haven’t created the wsgi file just yet. But before you create it, go back to httpd.conf and search for “httpd-vhosts.conf” Make sure there is a line that says

这就是我在虚拟主机配置中所做的更改。 您可能尚未收到这些错误,因为尚未创建wsgi文件。 但是,在创建它之前,请先返回httpd.conf并搜索“ httpd-vhosts.conf”。请确保其中有一行显示

# Virtual hosts Include conf/extra/httpd-vhosts.conf

#虚拟主机包括conf / extra / httpd-vhosts.conf

创建一个WSGI脚本 (Create a WSGI Script)

You need to keep in mind that you might want to change some directory order of your project. This is to ensure no circular import in your python app and enforce security in your web app. Feel free to do some trial and error for your project directory, if you changed your directory order you need to make sure the directory is still correct in vhosts configuration.

您需要记住,您可能想更改项目的某些目录顺序。 这是为了确保在您的python应用程序中不进行循环导入,并在Web应用程序中实施安全性。 随意对项目目录进行一些试验和错误,如果更改了目录顺序,则需要确保在vhosts配置中目录仍然正确。

First, create the wsgi script manually (my script is called web.wsgi) and then change the content (using notepad or vscode or any other text editor). Then add this python command to your script

首先,手动创建wsgi脚本(我的脚本称为web.wsgi),然后更改内容(使用记事本或vscode或任何其他文本编辑器)。 然后将此python命令添加到脚本中

activate_this = ‘F:/myapp/env/Scripts/activate_this.py’

activate_this ='F:/myapp/env/Scripts/activate_this.py'

with open(activate_this) as file_:

使用open(activate_this)作为file_:

exec(file_.read(), dict(__file__=activate_this))

exec(file_.read(),dict(__ file __ = activate_this))

import sys

导入系统

sys.path.insert(0, ‘F:/myapp’)

sys.path.insert(0,'F:/ myapp')

from your-app-script import app as application

从您的应用程序脚本导入应用程序作为应用程序

The ‘activate this part’ is only used if you’re working with a virtual environment. If you didn’t use any virtual environment you just need the import sys and below for your script. For more reference read this link: https://flask.palletsprojects.com/en/1.1.x/deploying/mod_wsgi/

仅在使用虚拟环境时才使用“激活此部分”。 如果您不使用任何虚拟环境,则只需要import sys及以下脚本即可。 有关更多参考,请阅读以下链接: https : //flask.palletsprojects.com/en/1.1.x/deploying/mod_wsgi/

your-app-script should be something like

您的应用脚本应类似于

from flask import Flask app = Flask(__name__) @app.route(“/”) def hello(): return “Hello World” if __name__ == “__main__”: app.run()

从烧瓶导入Flask app = Flask(__ name__)@ app.route(“ /”)def hello():如果__name__ ==“ __main__”,则返回“ Hello World”:app.run()

And that would be the last configuration you need to serve your Flask app to XAMPP Apache server on windows.

这就是将Flask应用程序提供给Windows上的XAMPP Apache服务器所需的最后一个配置。

And again, you might encounter some errors when starting your apache server. Try as hard as you can to figure out the errors. You can check the Apache error log, your app error log, or from command prompt when you start your server.

同样,在启动apache服务器时,您可能会遇到一些错误。 尽最大努力找出错误。 您可以在启动服务器时检查Apache错误日志,应用程序错误日志或从命令提示符下检查。

From my personal experience, most of the errors doesn’t have proper documentation on how to handle them. So most of the time my way to handle those errors is from a completely different context. I will link some major documentation about this issue and I hope no one would encounter as much trouble as I did (I spent a whole two weeks just to work for those errors).

根据我的个人经验,大多数错误都没有正确的文档说明如何处理它们。 因此,大多数情况下,我处理这些错误的方式是来自完全不同的上下文。 我将链接有关此问题的一些主要文档,希望没有人像我那样遇到太多麻烦(我花了整整两个星期的时间来解决这些错误)。

Thank you so much for reading this article. And special thanks for Graham Dumpleton for maintaining mod-wsgi and keep active answering questions in many platforms including GitHub, StackOverflow, or even some google groups. And Thilina Madumal for making me understand a base step to configure all of this.

非常感谢您阅读本文。 特别感谢Graham Dumpleton维护mod-wsgi并在包括GitHub,StackOverflow甚至某些Google组在内的许多平台上积极回答问题。 Thilina Madumal也让我了解了配置所有这些功能的基本步骤。

翻译自: https://medium.com/swlh/serve-flask-app-with-xampp-apache-on-windows-4debb4eb0b91

xampp apache

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值