Elastic Beanstalk上的psycopg2 - 无法部署应用程序
我正在把我的第一个应用程序(Flask)部署到Elastic Beanstalk上,但遇到了一些问题。使用PostgreSQL需要安装psycopg2,但我不知道怎么让它运行。
我在这个帖子中(Psycopg2在Amazon Elastic Beanstalk上)以及这个帖子中(https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/)看到我需要添加一个名为.elasticbeanstalk/01_packages.config
的文件,并在里面写入以下代码:
packages:
yum:
postgresql-devel: []
我尝试了不同的方式来写postgresql93-devel: []
,但还是没有成功。下面是我的错误日志。
[2015-11-16T21:03:24.749Z] INFO [2680] - [Application update/AppDeployStage0/AppDeployPreHook/03deploy.py] : Starting activity...
[2015-11-16T21:03:27.024Z] INFO [2680] - [Application update/AppDeployStage0/AppDeployPreHook/03deploy.py] : Activity execution failed, because: Collecting psycopg2==2.6.1 (from -r /opt/python/ondeck/app/requirements.txt (line 1))
Using cached psycopg2-2.6.1.tar.gz
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info/psycopg2.egg-info
writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
writing pip-egg-info/psycopg2.egg-info/PKG-INFO
writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
warning: manifest_maker: standard file '-c' not found
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-z20gvx7_/psycopg2
2015-11-16 21:03:27,010 ERROR Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1
Traceback (most recent call last):
File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
install_dependencies()
File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1 (ElasticBeanstalk::ExternalInvocationError)
caused by: Collecting psycopg2==2.6.1 (from -r /opt/python/ondeck/app/requirements.txt (line 1))
Using cached psycopg2-2.6.1.tar.gz
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info/psycopg2.egg-info
writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
writing pip-egg-info/psycopg2.egg-info/PKG-INFO
writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
warning: manifest_maker: standard file '-c' not found
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-z20gvx7_/psycopg2
2015-11-16 21:03:27,010 ERROR Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1
Traceback (most recent call last):
File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
install_dependencies()
File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1 (Executor::NonZeroExitStatus)
看起来我确实需要安装psycopg2,但我不太确定还有哪些其他步骤可以让我解决这个问题。
相关文章:
- 暂无相关问题
5 个回答
我解决这个问题的方法是:
1. 运行命令:eb ssh
2. 在ec2的终端里运行:sudo yum install -y postgresql-devel
然后重新部署你的应用程序。这样应该就不会再出现错误了。
你把文件夹的名字写错了。配置文件应该放在 .ebextensions
这个文件夹里,而你把它放在了 .elasticbeanstalk
这个文件夹里。这样是行不通的。试试这个:
mkdir .ebextensions && mv .elasticbeanstalk/01_packages.config .ebextensions
然后再重新部署你的应用。
即使我已经安装了 postgresql93-devel,还是出现了“没有名为 psycopg2 的模块”的错误。为了解决这个问题,我需要通过 eb ssh 连接到我的环境,然后执行 sudo pip install psycopg2
来解决这个问题。
有趣的是,我在Amazon Linux 2上找到的解决方案,恰好就是原问题的内容——我添加了一个名为01_package.conf
的文件,里面的内容是
packages:
yum:
postgresql-devel: []
而且,这和我在网上找到的几乎所有答案都相反,大家都建议使用postgresql96-devel
或者其他的版本号。我发现我需要使用的包名是postgresql-devel
,这是在我通过SSH登录到EC2实例后,查看所有可用的Yum包时发现的。
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ yum list available | grep 'postgresql'
freeradius-postgresql.x86_64 3.0.13-10.amzn2 amzn2-core
pcp-pmda-postgresql.x86_64 3.12.2-5.amzn2 amzn2-core
postgresql.x86_64 10.4-5.amzn2.0.2 amzn2extra-postgresql10
postgresql-contrib.x86_64 10.4-5.amzn2.0.2 amzn2extra-postgresql10
postgresql-devel.x86_64 10.4-5.amzn2.0.2 @amzn2extra-postgresql10
postgresql-docs.x86_64 10.4-5.amzn2.0.2 amzn2extra-postgresql10
...
我看到的包名其实就是postgres-devel
。一旦我把它改成这个,部署就顺利了。如果你在使用eb
和git时,记得在运行eb deploy
之前提交你的更改——或者至少把更改添加到git的暂存区,然后用eb deploy --staged
来运行。这让我浪费了一些时间,因为我以为我已经尝试过这个包名,实际上并没有。
我发现这个StackOverflow帖子里的答案也很有帮助。祝你好运!
最后,我需要一个更新的psql
客户端(11.x
),而不是上面答案中提到的默认版本(当时是9.2
)。这个答案是我唯一能让它工作的办法。为了后人,我把答案贴在这里...
packages:
yum:
amazon-linux-extras: []
commands:
01_postgres_activate:
command: sudo amazon-linux-extras enable postgresql11
02_postgres_install:
command: sudo yum install -y postgresql-devel
我解决了这个问题,你需要先打开你的EC2实例,然后像这样安装:
sudo yum install postgresql-devel
然后部署你的EB。