我一直在努力寻找下面代码中的错误。我有一个Flask/PostgreSQL/SQLAlchemy程序托管在一个GCP计算引擎虚拟机上,使用Nginx和Gunicorn作为服务器解决方案。
我的工作是编写代码,而不是部署它。(最好有更多的技能,但它就是这样。)而且我现在没有任何人可以委托这项任务或获得帮助。
该应用程序在本地部署没有任何问题。然而,将其放在GCP服务器上有点问题。我已经检查了几十个Stackoverflow帖子,并在Google上搜索了每个短语迭代,但我无法找出代码的哪一部分是关闭的。
Nginx服务器是在线的,因为当我在浏览器中检查域时,我会看到“欢迎使用nginx!”页面。但在gunicorn启动后,服务失败。请参阅下面的代码。知识渊博的建议将不胜感激。
计算虚拟机/home/user/
上的my_project
文件夹
user@my_project-vm:~/my_project$ ls -la
total 72
drwxr-xr-x 8 user user 4096 Dec 27 21:00 .
drwxr-xr-x 8 user user 4096 Dec 27 21:13 ..
-rw-r--r-- 1 user user 2148 Dec 27 05:00 .env
-rw-r--r-- 1 user user 34 Dec 27 20:56 .flaskenv
drwxr-xr-x 8 user user 4096 Dec 27 20:56 .git
-rw-r--r-- 1 user user 1039 Dec 27 04:41 .gitignore
drwxr-xr-x 2 user user 4096 Dec 27 04:41 .vscode
-rw-r--r-- 1 user user 2322 Dec 27 04:41 GCP_email_notification.json
drwxr-xr-x 2 user www-data 4096 Dec 27 20:10 __pycache__
drwxr-xr-x 10 user user 4096 Dec 27 20:56 app
-rw-r--r-- 1 user user 7403 Dec 27 20:56 app.py
-rw-r--r-- 1 user user 604 Dec 27 04:41 extensions.py
srwxrwx--- 1 user www-data 0 Dec 27 21:00 my_project.sock
drwxr-xr-x 3 user user 4096 Dec 27 04:41 migrations
-rw-r--r-- 1 user user 1740 Dec 27 05:00 requirements.txt
-rw-r--r-- 1 user user 810 Dec 27 04:41 tmp-policy.json
drwxr-xr-x 6 user user 4096 Dec 27 04:48 venv
-rw-r--r-- 1 user user 45 Dec 27 20:10 wsgi.py
字符串/home/user/my_project/
上的wsgi.py
from app import create_app
app = create_app()
型/home/user/my_project/
上的app.py
from app import create_app
if __name__ == "__main__":
app = create_app()
app.run(host='0.0.0.0')
型/home/user/my_project/app/
上的__init__.py
import ...
def create_app():
app = Flask(__name__,
static_url_path='/',
static_folder='../app/static',
template_folder='../app/templates')
...
app.config.from_object(config['default'])
config['default'].init_app(app)
@app.context_processor
...
@app.template_filter('datetime')
...
api.init_app(app)
db.init_app(app)
mail.init_app(app)
moment.init_app(app)
from app.models import ...
migrate = Migrate(app, db)
from .templates.about import about_bp
from .templates.errors import error_bp
from .templates.main import main_bp
...
app.register_blueprint(main_bp)
app.register_blueprint(error_bp)
app.register_blueprint(about_bp, url_prefix='/about')
...
return app
型/etc/nginx/sites/available
上的my_project
server {
listen 80;
server_name my_domain.com www.my_domain.com;
location / {
proxy_pass http://unix:/home/user/my_project/my_project.sock
}
}
型/etc/systemd/system
上的my_project.service
[Unit]
Description= You can change time text
After=network.target
[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/my_project
Environment="PATH=/home/user/my_project/venv/bin"
ExecStart=/home/user/my_project/venv/bin/gunicorn --workers 3 --bind unix:my_project.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
型
这是我在sudo systemctl status my_project
之后得到的。最后一行是事情向南的地方...
my_project.service - my_project primary web server setup
Loaded: loaded (/etc/systemd/system/my_project.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-12-27 21:00:09 UTC; 1h 22min ago
Main PID: 131051 (gunicorn)
Tasks: 7 (limit: 2353)
Memory: 207.9M
CPU: 6.629s
CGroup: /system.slice/my_project.service
├─131051 /home/user/my_project/venv/bin/python3 /home/user/my_project/venv/bin/gunicorn --workers 3 --bind unix:my_project.sock -m 007 wsgi:app
├─131052 /home/user/my_project/venv/bin/python3 /home/user/my_project/venv/bin/gunicorn --workers 3 --bind unix:my_project.sock -m 007 wsgi:app
├─131053 /home/user/my_project/venv/bin/python3 /home/user/my_project/venv/bin/gunicorn --workers 3 --bind unix:my_project.sock -m 007 wsgi:app
└─131054 /home/user/my_project/venv/bin/python3 /home/user/my_project/venv/bin/gunicorn --workers 3 --bind unix:my_project.sock -m 007 wsgi:app
Dec 27 21:00:09 my_project systemd[1]: Started my_project primary web server setup.
Dec 27 21:00:09 my_project gunicorn[131051]: [2023-12-27 21:00:09 +0000] [131051] [INFO] Starting gunicorn 21.2.0
Dec 27 21:00:09 my_project gunicorn[131051]: [2023-12-27 21:00:09 +0000] [131051] [INFO] Listening at: unix:my_project.sock (131051)
Dec 27 21:00:09 my_project gunicorn[131051]: [2023-12-27 21:00:09 +0000] [131051] [INFO] Using worker: sync
Dec 27 21:00:09 my_project gunicorn[131052]: [2023-12-27 21:00:09 +0000] [131052] [INFO] Booting worker with pid: 131052
Dec 27 21:00:09 my_project gunicorn[131053]: [2023-12-27 21:00:09 +0000] [131053] [INFO] Booting worker with pid: 131053
Dec 27 21:00:09 my_project gunicorn[131054]: [2023-12-27 21:00:09 +0000] [131054] [INFO] Booting worker with pid: 131054
Dec 27 21:52:46 my_project systemd[1]: my_project.service: Current command vanished from the unit file, execution of the command list won't be resumed.
型
提前感谢。享受假期!
1条答案
按热度按时间yws3nbqq1#
这个特殊的问题通过修改
/etc/systemd/system
上的my_project.service
文件来解决,以在ExecStart
行中包含--chdir
标志,如下所示:字符串
解决方案来自How to run gunicorn from a folder that is not the django project folder