Chrome官方網站已經說的很清楚,不再支持6.x的centos,至少7以上。 可是很多時候我們使用的服務器版本并不能隨便升級,即便已經很難受了,但是還得繼續使用低版本,裝起來那是真叫一個費勁,還好就是費勁一些,最終還是可以裝成功的。
什么是 Headless Chrome
Headless Chrome 是 Chrome 瀏覽器的無界面形態,可以在不打開瀏覽器的前提下,使用所有 Chrome 支持的特性運行你的程序。相比于現代瀏覽器,Headless Chrome 更加方便測試 web 應用,獲得網站的截圖,做爬蟲抓取信息等。相比于出道較早的 PhantomJS,SlimerJS 等,Headless Chrome 則更加貼近瀏覽器環境。
CentOS版本
lsb_release -a

最新版本google-Chrome安裝
CentOS/RedHat 7以上安裝google-chrome可以完全參考https://intoli.com/blog/installing-google-chrome-on-centos/ (6及以下版本不適用)。
指定yum源
服務器應該指定合適yum源,避免找不到某些依賴的尷尬。 修改 /etc/yum.repos.d/CentOS-Base.repo,可以使用阿里的yum repo:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repoyum clean all
安裝google-chrome
根據https://intoli.com/blog/installing-google-chrome-on-centos/ ,執行命令:
curl https://intoli.com/install-google-chrome.sh | bash
腳本會自動檢測當前版本安裝chrome所缺失的依賴包并下載。

檢查是否還缺乏依賴:
ldd /opt/google/chrome/chrome | grep "not found"
返回為空,說明CentOS下chrome依賴問題基本解決。
運行chrome
執行
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.suning.com/。
訪問成功,在當前目錄會生成截圖screenshot.png,如果報錯
[0100/000000.311368:ERROR:broker_posix.cc(43)] Invalid node channel message
則需要安裝依賴包:
yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
最新版本Chromedriver安裝
當前的chrome版本是 google-chrome-stable-72.0.3626.109-1.x8664,chromedrive的官網是https://sites.google.com/a/chromium.org/chromedriver/downloads

下載地址https://chromedriver.storage.googleapis.com/2.46/chromedriverlinux64.zip 或者選擇taobao鏡像下載http://npm.taobao.org/mirrors/chromedriver/ 鏡像下載地址http://npm.taobao.org/mirrors/chromedriver/2.46/chromedriver_linux64.zip
解壓后部署到/opt/drivers目錄下,嘗試運行:
./chromedriver Starting ChromeDriver 72.0.3626.7 (efcef9a3ecda02b2132af215116a03852d08b9cb) on port 9515Only local connections are allowed.[1550143530.011][SEVERE]: CreatePlatformSocket() returned an error, errno=0: Address family not supported by protocol (97)
另外要修改/etc/hosts,綁定127.0.0.1 localhost,否則,JAVA Selenium運行時chromedriver可能因為找不到localhost報超時異常
安裝selenium
- 安裝 Python 并配置好環境變量 shell輸入: python-V 出現對應版本號即安裝成功!
- 安裝 pip python默認自帶 pip 在安裝目錄的scripts目錄下,自行配置至環境變量即可,配置好后shell輸入: pip-V 出現對應版本號即安裝成功!
- 安裝 selenium shell輸入: pip install selenium 提示:Successfully installed selenium-即安裝成功!
python>>>from selenium import webdriver>>>driver = webdriver.Chrome()>>>driver.get('https://www.baidu.com')
已經可以了,正常寫python腳本即可。
搭建環境時遇到的問題
1、/lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./chromedriver)
#查看系統版本cat /etc/redhat-release #查看glibc支持的版本strings /lib64/libc.so.6 |grep GLIBC_wget http://ftp.gnu.org/gnu/glibc/glibc-2.14.tar.gz wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.14.tar.gz tar -xvf glibc-2.14.tar.gz tar -xvf glibc-ports-2.14.tar.gzmv glibc-ports-2.14 glibc-2.14/portsmkdir glibc-2.14/buildcd glibc-2.14/build ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/binmakemake install
安裝編譯過程中需要注意三點:
- 要將glibc-ports解壓到glibc目錄下
- 不能在glibc當前目錄下運行configure
- 加上優化開關,export CFLAGS="-g -O2 -march=i486",否則會出現錯誤
- 在make install過程中可能會出現 nsstest1加載不了的情況, 此時可以將加載libnsstest1.so.2的地方注釋掉,用 grep"nss_test1".-nr命令在 /glibc目錄下查找一下,加載的地方也不多(這只是一個測試nss的靜態庫,可以不要)
2、/lib64/libc.so.6: version `GLIBC_2.16' not found (required by ./chromedriver)
wget http://ftp.gnu.org/gnu/glibc/glibc-2.16.0.tar.gz wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.16.0.tar.gz tar -xvf glibc-2.16.0.tar.gz tar -xvf glibc-ports-2.16.0.tar.gzmv glibc-ports-2.16.0 glibc-2.16.0/portsmkdir glibc-2.16.0/buildcd glibc-2.16.0/build ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/binmakemake install
報錯
Unmatched ( in regex; marked by HERE in m/$( <-- HERE if $(abi-64-ld-soname),$(abi-64-ld-soname),ld/ at scripts/test-installation.pl line
可以參考解決辦法https://sourceware.org/bugzilla/attachment.cgi?id=6616&action=diff
- glibc-2.16.0/Makefile
ifeq (,$(install_root)) CC="$(CC)" $(PERL) scripts/test-installation.pl $(common-objpfx)endif
改成
ifeq (,$(install_root)) LD_SO=$(ld.so-version) CC="$(CC)" $(PERL) scripts/test-installation.pl $(common-objpfx)endif
- glibc-2.16.0/scripts/test-installation.pl
sub usage { print "Usage: test-installation [soversions.mk]n";
上面添加
if ($ENV{LD_SO}) { $LD_SO = $ENV{LD_SO};} else { $LD_SO= "";}sub usage { print "Usage: test-installation [soversions.mk]n";
在
} else { if (/^ld.so/) { ($ld_so_name, $ld_so_version)= /=(.*).so.(.*)$/;
上面添加
} elsif ($LD_SO ne "") { ($ld_so_name, $ld_so_version) = split ('.so.', $LD_SO);} else { if (/^ld.so/) { ($ld_so_name, $ld_so_version)= /=(.*).so.(.*)$/;
歡迎關注 “后端老鳥” 公眾號,接下來會發一系列的專題文章,包括Java、Python、Linux、SpringBoot、SpringCloud、Dubbo、算法、技術團隊的管理等,還有各種腦圖和學習資料,NFC技術、搜索技術、爬蟲技術、推薦技術、音視頻互動直播等,只要有時間我就會整理分享,敬請期待,現成的筆記、腦圖和學習資料如果大家有需求也可以公眾號留言提前獲取。由于本人在所有團隊中基本都處于攻堅和探路的角色,搞過的東西多,遇到的坑多,解決的問題也很多,歡迎大家加公眾號進群一起交流學習。