[英]Run Python script from browser using virtual environment on EC2 instance
I created a virtual environment on my EC2 instance.我在我的 EC2 实例上创建了一个虚拟环境。 How can I access this from the browser?
如何从浏览器访问它?
Hey everyone,嘿大家,
I created a virtual environment, following this tutorial , on my EC2 instance to run a simple Python script.我按照本教程在我的 EC2 实例上创建了一个虚拟环境,以运行一个简单的 Python 脚本。 Within the terminal, it works without errors.
在终端内,它可以正常工作。 However, I have made a web application and I would like to activate this script from the browser using the virtual environment.
但是,我制作了一个 web 应用程序,我想使用虚拟环境从浏览器激活此脚本。 When I try this, I get a "Permission denied" error.
当我尝试这个时,我收到“权限被拒绝”错误。
$output=shell_exec('bash /var/app/current/scripts/script.sh');
echo "<pre>$output</pre>";
#!/bin/bash
source /home/ec2-user/venv/python3/bin/activate
python3 /var/app/current/scripts/test.py
from datetime import datetime
from bs4 import BeautifulSoup
import requests
print('hello')
print(datetime.now())
url = "https://www.stackoverflow.com/"
website = requests.get(url).text
soup = BeautifulSoup(website, "html.parser")
print(soup.title)
/var/app/current/scripts/script.sh: line 2: /home/ec2-user/venv/python3/bin/activate: Permission denied
Traceback (most recent call last):
File "/var/app/current/scripts/test.py", line 2, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
I tried to change the permissions on the virtual environment using the following:我尝试使用以下方法更改虚拟环境的权限:
chmod a+x /home/ec2-user/venv chmod a+x /home/ec2-user/venv
This should give all users access to the virtual environment folder: /home/ec2-user/venv这应该使所有用户都可以访问虚拟环境文件夹:/home/ec2-user/venv
However, I am still getting the error:但是,我仍然收到错误消息:
/home/ec2-user/venv/python3/bin/activate: Permission denied
I have also tried to give all users the possibility of executing the activation script (/home/ec2-user/venv/python3/bin/activate):我还尝试让所有用户都可以执行激活脚本(/home/ec2-user/venv/python3/bin/activate):
chmod 665 /home/ec2-user/venv/python3/bin/activate chmod 665 /home/ec2-user/venv/python3/bin/activate
Which results in:结果是:
-rw-rw-r-x 1 ec2-user ec2-user /home/ec2-user/venv/python3/bin/activate
However, I still get the same error:但是,我仍然遇到同样的错误:
/home/ec2-user/venv/python3/bin/activate: Permission denied
You get this error because you have not added libraries that are used in the python script to the virtual env.您收到此错误是因为您尚未将 python 脚本中使用的库添加到虚拟环境。
In the tutorial you mentioned only boto library is installed.在您提到的教程中,只安装了 boto 库。
You need to install libraries you use.您需要安装您使用的库。 Run this from the command line:
从命令行运行:
source /home/ec2-user/venv/python3/bin/activate
pip install beautifulsoup4
pip install requests
Alternatively you can create a file and name it for example /home/ec2-user/requirements.txt
for example and list all requirements your script use:或者,您可以创建一个文件并将其命名为例如
/home/ec2-user/requirements.txt
并列出您的脚本使用的所有要求:
beautifulsoup4
requests
Then you can use this file to install all requirements into virtual env:然后您可以使用此文件将所有要求安装到虚拟环境中:
source /home/ec2-user/venv/python3/bin/activate
pip install -r /home/ec2-user/requirements.txt
I got some help from this post , however, needed to modify a few things.我从这篇文章中得到了一些帮助,但是需要修改一些东西。
Let's dive into what his answer was:让我们深入了解他的答案是什么:
sudo chown -R your_username:your_username path/to/virtuaelenv/
sudo chown -R your_username:your_username path/to/virtuaelenv/
Okay, this is great, but I needed a bit of information.好的,这很好,但我需要一些信息。
For me, the web application's username is webapp.对我来说,web 应用程序的用户名是 webapp。
Then, one thing that isn't very clear above is the path.然后,上面不太清楚的一件事是路径。 So, my path is:
所以,我的路径是:
/home/ec2-user/venv/python3/bin/activate
as mentioned above.正如刚才提到的。 Here, you need to change permissions to the
/home/ec2-user
and NOT to /home/ec2-user/venv
在这里,您需要将权限更改为
/home/ec2-user
而不是/home/ec2-user/venv
So, to give my application permission to my virtual environment, I needed ran:所以,为了让我的应用程序访问我的虚拟环境,我需要运行:
sudo chown -R webapp:webapp /home/ec2-user
That worked in the browser, However.但是,这在浏览器中有效。 this took away my ability to work with it on the server, To do so: I would have to switch it back to:
这剥夺了我在服务器上使用它的能力,为此:我必须将其切换回:
sudo chown -R ec2-user:ec2-user /home/ec2-user
Being far from ideal to switch back and forth, I tried to change the permissions with chmod
instead.来回切换远非理想,我尝试使用
chmod
更改权限。
sudo chmod 711 /home/ec2-user
Now I have read, write, and execution permissions whereas everyone else, including the web app, can only execute.现在我拥有读取、写入和执行权限,而其他所有人,包括 web 应用程序,都只能执行。
Now it all works现在一切正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.