在 Google Compute Engine 上为 Flask 配置 Apache WSGI(使用 mod_wsgi 的 Flask 导入错误)
Configuring Apache WSGI for Flask on Google Compute Engine (Flask Import Error using mod_wsgi)
我正在尝试学习 Flask 的介绍 book/tutorial,教程的一部分要求您在虚拟专用服务器上同时设置本地开发环境和生产环境。
简而言之,我收到一个看起来像 Python 导入错误的错误,抱怨找不到 Flask。
我浏览了其他 Flask 导入错误堆栈溢出帖子,但无济于事:我不相信我的 mod_wsgi 与我的 python 版本不匹配,我已经插入了我的内容相信是 Flask 目录进入路径(大约有两打不同的方法,没有任何效果)。我想知道是否存在问题,因为可能 Google Compute Engine 运行s apache 作为某个特定用户,而 Flask 安装到“~”的子文件夹中,我相信这取决于我使用的用户名SSH in——换句话说,也许 Apache 看不到那个文件夹,或者任何 "user folder"?。我对 Linux 没有经验,所以我正在努力缩小问题的范围。
但总的来说,当通过 mod_wsgi 执行时,如何更正 hello.py 的导入错误?
非常感谢勇敢的灵魂敢于继续阅读!下面有更多详细信息,包括 files/contents、重现步骤等
当我访问本应显示 hello.py 应用程序的页面时,出现 500 内部服务器错误。查阅日志显示以下错误(我添加了一些 python 打印语句以了解发生了什么):
[Mon Jul 03 00:58:21.619295 2017] [wsgi:error] [pid 4520:tid
139990843242240] PYTHON SYS.PATH:
[Mon Jul 03 00:58:21.619440 2017] [wsgi:error] [pid 4520:tid 139990843242240] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:21.619501 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON VERSION:
[Mon Jul 03 00:58:21.619539 2017] [wsgi:error] [pid 4520:tid 139990843242240] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:21.619581 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:21.619619 2017] [wsgi:error] [pid 4520:tid 139990843242240] /usr/bin/python
[Mon Jul 03 00:58:21.619646 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619674 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619806 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:21.619844 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:21.619898 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] Traceback (most recent call last):
[Mon Jul 03 00:58:21.619952 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:21.620010 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from hello import app as application
[Mon Jul 03 00:58:21.620042 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:21.620082 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from flask import Flask
[Mon Jul 03 00:58:21.620131 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] ImportError: No module named flask
[Mon Jul 03 00:58:40.176748 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON SYS.PATH:
[Mon Jul 03 00:58:40.176930 2017] [wsgi:error] [pid 4520:tid 139990725629696] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:40.176983 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON VERSION:
[Mon Jul 03 00:58:40.177023 2017] [wsgi:error] [pid 4520:tid 139990725629696] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:40.177060 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:40.177092 2017] [wsgi:error] [pid 4520:tid 139990725629696] /usr/bin/python
[Mon Jul 03 00:58:40.177122 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177150 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177327 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:40.177380 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:40.177430 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] Traceback (most recent call last):
[Mon Jul 03 00:58:40.177490 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:40.177559 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from hello import app as application
[Mon Jul 03 00:58:40.177604 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:40.177660 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from flask import Flask
[Mon Jul 03 00:58:40.177709 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] ImportError: No module named flask
我可能错了,但我认为错误的重要部分是:
ImportError: No module named flask
奇怪的是,如果我运行 "python hello.py"(文件和内容见下文),没有烧瓶导入错误。这告诉我,当 Apache 使用 mod_wsgi 执行 hello.py 时,它 "forgets" Flask 所在的位置。我不确定如何告诉它在哪里看。当我验证是否安装了 Flask 时,PIP 指定了它的安装位置。
alexanderblank@instance-1:/var/www/firstapp$ pip install flask
Requirement already satisfied: flask in /home/alexanderblank/.local/lib/python2.7/site-packages
Requirement already satisfied: itsdangerous>=0.21 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: click>=2.0 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in /home/alexanderblank/.local/lib/python2.7/site-packages (from Jinja2>=2.4->flask)
我尝试将该文件夹 "insert" 放入 WSGI 文件中的 Python 搜索路径:
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
我已经在下面详细说明了我采取的具体步骤(服务器设置和代码文件),并且已经尝试解决这个问题几个小时了。
我在 Google Compute Engine 上创建了一个新的 Ubuntu 16 LTS 实例——我将其称为 "VPS"(用于虚拟专用服务器)。我完成了以下步骤:
(1) SSH 进入 VPS。
(2)运行以下命令安装python、pip和flask:
sudo apt-get update
sudo apt-get install python-pip
pip install --user Flask
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
(3) 通过访问 VPS public IP 测试了配置,它显示了默认的 Apache2 Ubuntu 页面(因此防火墙有效,网络服务器似乎能够提供默认页面)。
然后我提取了我使用 GIT 在本地创建的 Python 文件。在 /var/www/ 我有以下文件:
instance-1:/var/www$ tree -R
.
├── firstapp
│ ├── hello.py
│ ├── hello.pyc
│ ├── hello.wsgi
│ └── README.md
└── html
└── index.html
在 /etc/apache2/sites-available 我创建了一个名为 hello.conf
的新文件
├── 000-default.conf
├── default-ssl.conf
└── hello.conf
我还递归地获得了 /etc/apache2 目录的所有权,使用:
sudo chown -R [my_ssh_username] /etc/apache2
然后我告诉 Apache 忽略旧的 .conf 文件,并通过 运行ning:
查看 hello.conf
sudo a2dissite 000-default.conf
sudo a2ensite hello.conf
sudo service apache2 reload
当我访问 VPS 的 public IP 地址时,这会导致 500 内部服务器错误。我通过 运行ning:
查看了日志文件
sudo tail -f /var/log/apache2/error.log
奇怪的是,运行从 SSH shell 运行文件按预期工作(导入烧瓶没有问题):
instance-1:/var/www/firstapp$ python hello.py
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Debugger is active!
* Debugger PIN: 278-244-316
我已经把我做的重要文件的内容收录在下面了。
hello.conf
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicationGroup ${GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
hello.py
import sys
print "PYTHON SYS.PATH:"
print sys.path
print "PYTHON VERSION:"
print sys.version_info
print "PYTHON LOCATION OF EXECUTABLE:"
print sys.executable
print "\n"
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(port=5000, debug=True)
hello.wsgi
import sys
sys.path.insert(0, "/var/www/firstapp")
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
from hello import app as application
您不能在添加到 sys.path.insert()
的路径中使用 ~
。
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
如果您想这样做,请使用:
sys.path.insert(0, os.path.expanduser("~/.local/lib/python2.7/site-packages"))
有关如何使用 Python 虚拟环境设置 mod_wsgi 的一般信息,请参阅:
我正在尝试学习 Flask 的介绍 book/tutorial,教程的一部分要求您在虚拟专用服务器上同时设置本地开发环境和生产环境。
简而言之,我收到一个看起来像 Python 导入错误的错误,抱怨找不到 Flask。
我浏览了其他 Flask 导入错误堆栈溢出帖子,但无济于事:我不相信我的 mod_wsgi 与我的 python 版本不匹配,我已经插入了我的内容相信是 Flask 目录进入路径(大约有两打不同的方法,没有任何效果)。我想知道是否存在问题,因为可能 Google Compute Engine 运行s apache 作为某个特定用户,而 Flask 安装到“~”的子文件夹中,我相信这取决于我使用的用户名SSH in——换句话说,也许 Apache 看不到那个文件夹,或者任何 "user folder"?。我对 Linux 没有经验,所以我正在努力缩小问题的范围。
但总的来说,当通过 mod_wsgi 执行时,如何更正 hello.py 的导入错误?
非常感谢勇敢的灵魂敢于继续阅读!下面有更多详细信息,包括 files/contents、重现步骤等
当我访问本应显示 hello.py 应用程序的页面时,出现 500 内部服务器错误。查阅日志显示以下错误(我添加了一些 python 打印语句以了解发生了什么):
[Mon Jul 03 00:58:21.619295 2017] [wsgi:error] [pid 4520:tid
139990843242240] PYTHON SYS.PATH:
[Mon Jul 03 00:58:21.619440 2017] [wsgi:error] [pid 4520:tid 139990843242240] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:21.619501 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON VERSION:
[Mon Jul 03 00:58:21.619539 2017] [wsgi:error] [pid 4520:tid 139990843242240] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:21.619581 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:21.619619 2017] [wsgi:error] [pid 4520:tid 139990843242240] /usr/bin/python
[Mon Jul 03 00:58:21.619646 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619674 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619806 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:21.619844 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:21.619898 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] Traceback (most recent call last):
[Mon Jul 03 00:58:21.619952 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:21.620010 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from hello import app as application
[Mon Jul 03 00:58:21.620042 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:21.620082 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from flask import Flask
[Mon Jul 03 00:58:21.620131 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] ImportError: No module named flask
[Mon Jul 03 00:58:40.176748 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON SYS.PATH:
[Mon Jul 03 00:58:40.176930 2017] [wsgi:error] [pid 4520:tid 139990725629696] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:40.176983 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON VERSION:
[Mon Jul 03 00:58:40.177023 2017] [wsgi:error] [pid 4520:tid 139990725629696] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:40.177060 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:40.177092 2017] [wsgi:error] [pid 4520:tid 139990725629696] /usr/bin/python
[Mon Jul 03 00:58:40.177122 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177150 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177327 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:40.177380 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:40.177430 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] Traceback (most recent call last):
[Mon Jul 03 00:58:40.177490 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:40.177559 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from hello import app as application
[Mon Jul 03 00:58:40.177604 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:40.177660 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from flask import Flask
[Mon Jul 03 00:58:40.177709 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] ImportError: No module named flask
我可能错了,但我认为错误的重要部分是:
ImportError: No module named flask
奇怪的是,如果我运行 "python hello.py"(文件和内容见下文),没有烧瓶导入错误。这告诉我,当 Apache 使用 mod_wsgi 执行 hello.py 时,它 "forgets" Flask 所在的位置。我不确定如何告诉它在哪里看。当我验证是否安装了 Flask 时,PIP 指定了它的安装位置。
alexanderblank@instance-1:/var/www/firstapp$ pip install flask
Requirement already satisfied: flask in /home/alexanderblank/.local/lib/python2.7/site-packages
Requirement already satisfied: itsdangerous>=0.21 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: click>=2.0 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in /home/alexanderblank/.local/lib/python2.7/site-packages (from Jinja2>=2.4->flask)
我尝试将该文件夹 "insert" 放入 WSGI 文件中的 Python 搜索路径:
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
我已经在下面详细说明了我采取的具体步骤(服务器设置和代码文件),并且已经尝试解决这个问题几个小时了。
我在 Google Compute Engine 上创建了一个新的 Ubuntu 16 LTS 实例——我将其称为 "VPS"(用于虚拟专用服务器)。我完成了以下步骤:
(1) SSH 进入 VPS。 (2)运行以下命令安装python、pip和flask:
sudo apt-get update
sudo apt-get install python-pip
pip install --user Flask
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
(3) 通过访问 VPS public IP 测试了配置,它显示了默认的 Apache2 Ubuntu 页面(因此防火墙有效,网络服务器似乎能够提供默认页面)。
然后我提取了我使用 GIT 在本地创建的 Python 文件。在 /var/www/ 我有以下文件:
instance-1:/var/www$ tree -R
.
├── firstapp
│ ├── hello.py
│ ├── hello.pyc
│ ├── hello.wsgi
│ └── README.md
└── html
└── index.html
在 /etc/apache2/sites-available 我创建了一个名为 hello.conf
的新文件├── 000-default.conf
├── default-ssl.conf
└── hello.conf
我还递归地获得了 /etc/apache2 目录的所有权,使用:
sudo chown -R [my_ssh_username] /etc/apache2
然后我告诉 Apache 忽略旧的 .conf 文件,并通过 运行ning:
查看 hello.confsudo a2dissite 000-default.conf
sudo a2ensite hello.conf
sudo service apache2 reload
当我访问 VPS 的 public IP 地址时,这会导致 500 内部服务器错误。我通过 运行ning:
查看了日志文件sudo tail -f /var/log/apache2/error.log
奇怪的是,运行从 SSH shell 运行文件按预期工作(导入烧瓶没有问题):
instance-1:/var/www/firstapp$ python hello.py
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Debugger is active!
* Debugger PIN: 278-244-316
我已经把我做的重要文件的内容收录在下面了。
hello.conf
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicationGroup ${GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
hello.py
import sys
print "PYTHON SYS.PATH:"
print sys.path
print "PYTHON VERSION:"
print sys.version_info
print "PYTHON LOCATION OF EXECUTABLE:"
print sys.executable
print "\n"
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(port=5000, debug=True)
hello.wsgi
import sys
sys.path.insert(0, "/var/www/firstapp")
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
from hello import app as application
您不能在添加到 sys.path.insert()
的路径中使用 ~
。
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
如果您想这样做,请使用:
sys.path.insert(0, os.path.expanduser("~/.local/lib/python2.7/site-packages"))
有关如何使用 Python 虚拟环境设置 mod_wsgi 的一般信息,请参阅: