Flask、Nginx、Gunicorn Stack 启动 Selenium 实例
Flask, Nginx, Gunicorn Stack Launching Selenium instance
我有一个用户已添加到 www-data
组。我有一个 运行 和 API 的服务设置,它启动了一个 Selenium 实例。我能够以我的用户身份启动代码,但 Web 应用程序无法启动。我最初遇到 Xvfb 问题,但在将路径添加到服务文件的环境后,问题得到解决。我现在遇到了 google-chrome 未执行的类似问题。
我试过添加环境路径,并更改文件和文件夹的权限。
nginx 配置文件
server {
listen 443 ssl;
root /usr/share/nginx/html;
index index.html index.htm;
server_name private;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
ssl_certificate /etc/letsencrypt/live/private/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/private/privkey.pem; # managed by Certbot
}
server {
if ($host = private) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name private;
return 404; # managed by Certbot
}
[Unit]
Description=Gunicorn serving hrapi
After=network.target
[Service]
User=artem
Group=www-data
WorkingDirectory=/home/artem/ucs
Environment="PATH=/home/artem/env/bin:/usr/bin"
ExecStart=/home/artem/env/bin/gunicorn --workers 3 --bind 127.0.0.1:8888 wsgi:app
[Install]
WantedBy=multi-user.target
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"fname": "ijay", "lname": "private", "addr": "private", "city": "scottsdale", "state":"AZ", "postal":"private", "ssn":"private"}' https://hrapi.domain.com/getreport
Error:b'Traceback (most recent call last):
File "/home/artem/ucs/get_report.py", line 158, in <module>
b = UcsBot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7])
File "/home/artem/ucs/get_report.py", line 40, in __init__
self.driver = webdriver.Chrome(\'/home/artem/ucs/chromedriver\', chrome_options=options)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn\'t exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Selenium 应该会启动,当我 运行 脚本时它会正常启动。但是无法从 API 启动脚本。
此外,我已确保我的 chrome 和 chrome 驱动程序是正确的版本。正如我之前所说,脚本本身可以从用户那里正常执行,而不是通过 API.
触发时
从详细的日志来看,问题似乎仍然是怀疑的权限。 j
[1563464973.354][INFO]: Launching chrome: /usr/bin/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.rpiwaU/internal --log-level=0 --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.aQmopJ data:,
/usr/bin/google-chrome: line 8: readlink: command not found
/usr/bin/google-chrome: line 24: mkdir: command not found
/usr/bin/google-chrome: line 45: exec: cat: not found
/usr/bin/google-chrome: line 46: exec: cat: not found
[1563464973.406][INFO]: [60f2bc8b762ce68091b9abc2d8476bf5] RESPONSE InitSession ERROR unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
[1563464973.406][DEBUG]: Log type 'driver' lost 0 entries on destruction
[1563464973.406][DEBUG]: Log type 'browser' lost 0 entries on destruction
之后我做了
sudo chown artem:www-data /usr/bin/google-chrome
sudo chown artem:www-data /usr/bin/chromedriver
还是一样的问题。
# wsgi.py
from api import app
if __name__=="__main__":
app.run()
chrome 启动脚本 /usr/bin/google-chrome
有问题,如 /usr/bin/google-chrome: line 24: XXX: command not found
:
所示
[1563464973.354][INFO]: Launching chrome: /usr/bin/google-chrome [...]
/usr/bin/google-chrome: line 8: readlink: command not found
/usr/bin/google-chrome: line 24: mkdir: command not found
/usr/bin/google-chrome: line 45: exec: cat: not found
/usr/bin/google-chrome: line 46: exec: cat: not found
这可能是一个简单的$PATH
问题,意味着uWSGI服务器找不到所有提到的命令readlink
等等,因为它在$PATH
中没有这些命令] 多变的。
警告:我会不会允许www-data
通过更改权限访问所有命令XXX
,这些将是生产服务器上的主要安全问题!
相反,我建议查看以下 SO 问题:How to fix "usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed" error in Linux?
备选方案:你能在不使用 Chrome 的情况下以某种方式解决你的工作吗?您在网络服务器上启动 Chrome 作为服务的原因是什么?
将 :/bin
添加到您的 PATH 中,如下所示:
Environment="PATH=/home/artem/env/bin:/usr/bin:/bin"
mkdir
、readlink
等在 /bin
路径中,不在修改后的 PATH
中
我有一个用户已添加到 www-data
组。我有一个 运行 和 API 的服务设置,它启动了一个 Selenium 实例。我能够以我的用户身份启动代码,但 Web 应用程序无法启动。我最初遇到 Xvfb 问题,但在将路径添加到服务文件的环境后,问题得到解决。我现在遇到了 google-chrome 未执行的类似问题。
我试过添加环境路径,并更改文件和文件夹的权限。
nginx 配置文件
server {
listen 443 ssl;
root /usr/share/nginx/html;
index index.html index.htm;
server_name private;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
ssl_certificate /etc/letsencrypt/live/private/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/private/privkey.pem; # managed by Certbot
}
server {
if ($host = private) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name private;
return 404; # managed by Certbot
}
[Unit]
Description=Gunicorn serving hrapi
After=network.target
[Service]
User=artem
Group=www-data
WorkingDirectory=/home/artem/ucs
Environment="PATH=/home/artem/env/bin:/usr/bin"
ExecStart=/home/artem/env/bin/gunicorn --workers 3 --bind 127.0.0.1:8888 wsgi:app
[Install]
WantedBy=multi-user.target
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"fname": "ijay", "lname": "private", "addr": "private", "city": "scottsdale", "state":"AZ", "postal":"private", "ssn":"private"}' https://hrapi.domain.com/getreport
Error:b'Traceback (most recent call last):
File "/home/artem/ucs/get_report.py", line 158, in <module>
b = UcsBot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7])
File "/home/artem/ucs/get_report.py", line 40, in __init__
self.driver = webdriver.Chrome(\'/home/artem/ucs/chromedriver\', chrome_options=options)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/artem/env/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn\'t exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Selenium 应该会启动,当我 运行 脚本时它会正常启动。但是无法从 API 启动脚本。
此外,我已确保我的 chrome 和 chrome 驱动程序是正确的版本。正如我之前所说,脚本本身可以从用户那里正常执行,而不是通过 API.
触发时从详细的日志来看,问题似乎仍然是怀疑的权限。 j
[1563464973.354][INFO]: Launching chrome: /usr/bin/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.rpiwaU/internal --log-level=0 --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.aQmopJ data:,
/usr/bin/google-chrome: line 8: readlink: command not found
/usr/bin/google-chrome: line 24: mkdir: command not found
/usr/bin/google-chrome: line 45: exec: cat: not found
/usr/bin/google-chrome: line 46: exec: cat: not found
[1563464973.406][INFO]: [60f2bc8b762ce68091b9abc2d8476bf5] RESPONSE InitSession ERROR unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
[1563464973.406][DEBUG]: Log type 'driver' lost 0 entries on destruction
[1563464973.406][DEBUG]: Log type 'browser' lost 0 entries on destruction
之后我做了
sudo chown artem:www-data /usr/bin/google-chrome
sudo chown artem:www-data /usr/bin/chromedriver
还是一样的问题。
# wsgi.py
from api import app
if __name__=="__main__":
app.run()
chrome 启动脚本 /usr/bin/google-chrome
有问题,如 /usr/bin/google-chrome: line 24: XXX: command not found
:
[1563464973.354][INFO]: Launching chrome: /usr/bin/google-chrome [...]
/usr/bin/google-chrome: line 8: readlink: command not found
/usr/bin/google-chrome: line 24: mkdir: command not found
/usr/bin/google-chrome: line 45: exec: cat: not found
/usr/bin/google-chrome: line 46: exec: cat: not found
这可能是一个简单的$PATH
问题,意味着uWSGI服务器找不到所有提到的命令readlink
等等,因为它在$PATH
中没有这些命令] 多变的。
警告:我会不会允许www-data
通过更改权限访问所有命令XXX
,这些将是生产服务器上的主要安全问题!
相反,我建议查看以下 SO 问题:How to fix "usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed" error in Linux?
备选方案:你能在不使用 Chrome 的情况下以某种方式解决你的工作吗?您在网络服务器上启动 Chrome 作为服务的原因是什么?
将 :/bin
添加到您的 PATH 中,如下所示:
Environment="PATH=/home/artem/env/bin:/usr/bin:/bin"
mkdir
、readlink
等在 /bin
路径中,不在修改后的 PATH