jenkins+phantomjs環境搭建及使用

#jenkins+phantomjs 前端性能自動化測試的安裝和使用
#gcc GNU編譯器套件 https://gcc.gnu.org/php

#nginx 高性能的HTTP和反向代理服務器 http://nginx.org
#php 通用開源腳本語言 http://php.net
#Node.js 事件驅動I/O服務端JavaScript環境 https://nodejs.org/en/
#Phantomjs 前端自動化測試工具,將結果導出爲har文件,並上傳給 Showslow http://phantomjs.org/download.html
#Phantomas 基於phantomjs前端性能數據收集工具 https://github.com/macbre/phantomas
#mysql 一種開源的關係型數據庫 https://www.mysql.com/
#Showslow 收集和展現數據,監測各類網站性能指標趨勢變化的開源工具 https://github.com/sergeychernyshev/showslow/wiki
#Yslow、PageSpeed 對網站頁面進行分析 https://github.com/sergeychernyshev/showslow/wiki http://www.mervine.net/projects/npms/yslowjs https://github.com/jmervine/node-yslowjshtml

#Harviewer 可視化查詢har文件的工具 http://www.softwareishard.com/blog/har-viewer/ https://github.com/janodvarko/harviewer
#NetExport 將http加載的詳細性能數據導出爲har文件,並上傳給 Showslow前端

#jenkins 監控和調度執行job https://jenkins.io/ node

#操做系統:
#CentOS Linux release 7.3.1611 (Core)python

#安裝包版本:
#epel-release-7-11.noarch.rpm
#gcc-7.2.0.tar.gz
#jdk-8u144-linux-x64.tar.gz
#jpegsrc.v8c.tar.gz
#mysql57-community-release-el7-11.noarch.rpm
#nginx-1.6.3.tar.gz
#node-v8.0.0-linux-x64.tar.gz
#openssl-1.0.2n.tar.gz
#pcre-8.40.tar.gz
#phantomjs-2.1.1-linux-x86_64.tar
#php-7.0.28.tar.bz2
#zlib-1.2.11.tar.gz
#phantomas.git v1.20版本mysql


1)安裝依賴包
#查看版本
python -v
make -vlinux


#安裝依賴包epel
#epel包地址:http://dl.fedoraproject.org/pub/epel/7/x86_64/nginx

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpmc++

rpm -Uvh epel-release-7-11.noarch.rpmgit

yum --enablerepo=epel-testing -y install lz4

#先安裝依賴包jpegsrc.v8c.tar.gz
#解決出現libjpeg.so.8找不到的問題 error while loading shared libraries: libjpeg.so.8: cannot open shared object file
#下載地址:http://www.ijg.org/files/jpegsrc.v8c.tar.gz

cd /usr/loca/src/
wget http://www.ijg.org/files/jpegsrc.v8c.tar.gz

tar zxvf jpegsrc.v8c.tar.gz

chown -R root:root jpeg-8c/

cd jpeg-8c/

./configure

make && make install

rm -rf jpeg-8c/


#再安裝依賴包中的庫文件 openssl-1.0.2n.tar.gz
#解決出現libssl.so.1.0.0找不到的問題 error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file
#下載地址:https://www.openssl.org/source/openssl-1.0.2n.tar.gz

cd /usr/loca/src/
wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz

tar zxvf openssl-1.0.2n.tar.gz

mv openssl-1.0.2n /usr/local/

cd /usr/local/openssl-1.0.2n/

./config shared zlib-dynamic

make

cp libssl.so.1.0.0 libcrypto.so.1.0.0 /usr/lib64/
ln -sf libssl.so.1.0.0 libssl.so
ln -sf libcrypto.so.1.0.0 libcrypto.so


#安裝pcre zlib
cd /usr/local/src/
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
wget http://zlib.net/fossils/zlib-1.2.11.tar.gz

tar zxvf pcre-8.40.tar.gz -C /usr/local/
cd /usr/local/
mv pcre-8.40 pcre8.4
chown -R root:root pcre8.4
cd pcre8.4/
./configure
make && make install

tar zxvf zlib-1.2.11.tar.gz -C /usr/local/
cd /usr/local/
mv zlib-1.2.11 zlib1.2
chown -R root:root zlib1.2
cd zlib1.2/
./configure
make && make install


#安裝依賴庫
yum -y groupinstall "Development Tools"
yum -y install autoconf automake bison bzip* bzip2 clang cmake
yum -y install cURL curl-devel fontconfig freetype-devel g gcc gcc-c++
yum -y install gd-devel git libcurl libcurl* libevent* libjpeg.x86_64 libjpeg-devel libmcrypt
yum -y install libmcrypt libmcrypt-devel libpng-devel libtool libtool-ltdl-devel libxml* libxml2-devel
yum -y install lrzsz make mcrypt mhash ncurses-devel openssl openssl*
yum -y install openssl-devel patch pcre pcre-devel php-mbstring php-mcrypt php-mysqli
yum -y install python-devel readline-devel sudo unzip wget zlib zlib-devel
yum -y install recode recode-devel libtidy libtidyp libtidy-devl libtidyp-devel libxslt libxslt-devel
yum clean all
yum makecache

#查看版本
python -v
make -v


2)安裝gcc
#因安裝nodejs時須要用到gcc4.9.4++,而centos7默認最高只能到gcc4.8.2,於是須要將gcc升級到更高的版本
#安裝gcc7.2.0
#gcc7.2.0官網地址:https://gcc.gnu.org/
#gcc7.2.0包地址:http://gcc.parentingamerica.com/releases/gcc-7.2.0/gcc-7.2.0.tar.gz
#參考文檔 https://www.cnblogs.com/davygeek/p/8018628.html

cd /usr/local/src/
wget http://gcc.parentingamerica.com/releases/gcc-7.2.0/gcc-7.2.0.tar.gz

#解壓縮文件
tar zxvf gcc-7.2.0.tar.gz -C /usr/local/

cd /usr/local/gcc-7.2.0/contrib/

#下載依賴包
./download_prerequisites

cd /usr/local/

#在該臨時目錄中構建gcc7.2
mkdir gcc7.2objdir

cd gcc7.2objdir/

/usr/local/gcc-7.2.0/configure --prefix=/usr/local/gcc7.2 --enable-languages=c,c++,go --disable-multilib

echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64' >> ~/.bashrc
/sbin/ldconfig -v

#時間大概60分鐘左右
make -j4 && make install

ln -s /usr/local/gcc7.2/bin/gcc /usr/bin/gcc
ln -s /usr/local/gcc7.2/bin/g++ /usr/bin/g++
ln -s /usr/local/gcc7.2/include/c++/7.2.0 /usr/include/c++/7.2.0
ln -s /usr/local/gcc7.2/lib64/libstdc++.so.6.0.24 /usr/lib64/libstdc++.so.6

#刪除安裝文件
cd /usr/local/

rm -rf gcc-7.2.0 gcc7.2objdir

gcc --version 或 g++ -v


3)安裝nginx
#安裝nginx1.6.3
#nginx1.6.3官網地址:http://nginx.org
#nginx1.6.3包地址:http://nginx.org/download/nginx-1.6.3.tar.gz

groupadd -r nginx
useradd -r -g nginx nginx

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.3.tar.gz

tar -zxvf nginx-1.6.3.tar.gz

cd nginx-1.6.3/

#pcre 4.4~8.4 zlib 1.13~1.2.11
./configure --prefix=/usr/local/nginx1.6 --user=nginx --group=nginx --with-poll_module --with-http_ssl_module --with-pcre=/usr/local/pcre8.4 --with-zlib=/usr/local/zlib1.2 --with-pcre-jit --with-cc-opt="-Wno-error"

make && make install

ln -sf /usr/local/nginx1.6/sbin/nginx /usr/sbin

#修改nginx.conf localhost.conf文件

#nginx.conf文件
user nginx nginx;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;

include vhosts/*.conf;
}

mkdir vhosts
cd vhosts/
cp /usr/local/nginx1.6/conf/nginx.conf.default localhost.conf
vim localhost.conf

#localhost.conf文件

server {
listen 80;
#server_name php7test.com;
server_name localhost;

root /usr/local/showslow;

location / {
#root html;
root /usr/local/showslow;
index index.php index.html index.htm;
}

#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}

location ~ \.php$ {
#root html;
root /usr/local/showslow;

fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/dev/shm/php-fcgi.sock;

fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;

include fastcgi.conf;
}
}


#檢查配置 nginx -t
#啓動 nginx
#重啓 ngxin -s reload
#中止 nginx -s stop

#查看nginx 啓動進程
ps -ef |grep nginx


4)安裝php
#查看是否安裝過php,若是有安裝過php,則不須要再安裝php
yum list installed |grep php

#安裝php7.0.28
#php官網地址:http://php.net/
#php7.0.28包地址,訪問該地址後再下載包文件:http://am1.php.net/get/php-7.0.28.tar.bz2/from/this/mirror

#解壓縮php7的包
cd /usr/local/src/
tar -jxvf php-7.0.28.tar.bz2

cd php-7.0.28/

./configure --prefix=/usr/local/php7.0 --sysconfdir=/usr/local/php7.0/etc --with-config-file-path=/usr/local/php7.0/etc --with-bz2 --with-curl --with-fpm-user=nginx --with-fpm-group=nginx --with-freetype-dir --with-gettext --with-gd --with-iconv --with-iconv-dir --with-jpeg-dir --with-libxml-dir --with-mcrypt --with-mhash --with-mysqli=mysqlnd --with-openssl --with-pcre-dir --with-pear --with-png-dir --with-pdo-sqlite --with-pdo-mysql=mysqlnd --with-recode --with-readline --with-sqlite3 --with-tidy --with-xmlrpc --with-xsl --with-zlib --enable-bcmath --enable-exif --enable-fpm --enable-gd-jis-conv --enable-gd-native-ttf --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-session --enable-shared --enable-shmop --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

make && make install

#配置環境變量
vim /etc/profile
PHP_HOME=/usr/local/php7.0
PATH=$PHP_HOME/bin:$PATH
export PHP_HOME PATH

:wq

source /etc/profile

#從安裝包中複製 php.ini、php-fpm.conf 文件到安裝目錄
cp /usr/local/src/php-7.0.28/php.ini* /usr/local/php7.0/etc/

cd /usr/local/php7.0/etc/

cp php.ini-production php.ini
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

#複製mycrypt.so到php擴展目錄下
cp /usr/lib64/php/modules/mcrypt.so mysqli.so /usr/local/php7.0/lib/php/extensions/no-debug-non-zts-20151012/

#配置php.ini文件
;設置不顯示錯誤日誌
display_errors = Off

#在關閉display_errors後開啓PHP錯誤日誌(路徑在php-fpm.conf中配置)
log_errors = On

;設置字符集
default_charset = "UTF-8"

;設置文件上傳大小
upload_max_filesize = 2M

;設置表單最大值大小,單位MB
;Maximum size of POST data that PHP will accept.
post_max_size = 8M

;設置擴展庫路徑
extension_dir = "/usr/local/php7.0/lib/php/extensions/no-debug-non-zts-20151012/"

;設置時區
date.timezone = PRC

;開啓opcache,默認是0
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1


#配置php-fpm.conf文件
;Default Value: none the default prefix is /usr/local/php7.2/var
;開啓後能夠平滑重啓php-fpm
pid = /usr/local/php7.0/var/run/php-fpm.pid


;設置錯誤日誌的路徑
;Note: the default prefix is /usr/local/php7.0/var
;Default Value: log/php-fpm.log, 即/usr/local/php7.0/var/log/php-fpm.log
error_log =/usr/local/php7.0/var/log/php-fpm.log

;Log等級
;Possible Values: alert, error, warning, notice, debug
;Default Value: notice
log_level = notice

;後臺運行,默認yes
;Default Value: yes
daemonize = yes

;引入www.conf文件中的配置
include=/usr/local/php7.0/etc/php-fpm.d/*.conf


#配置www.conf文件,在php-fpm.d目錄下
;設置用戶和用戶組,默認都是 nobody
user = nginx
group = nginx

;設置PHP監聽
;下面是默認值,不建議使用
listen = 127.0.0.1:9000
;參考nginx.conf中的配置項fastcgi_pass unix:/dev/shm/php-fcgi.sock;
;listen = /dev/shm/php-fcgi.sock

;listen.owner = nginx
;listen.group = nginx

;開啓慢日誌查詢
slowlog = /usr/local/php7.0/var/log/$pool.log.slow
request_slowlog_timeout = 10s

#創建軟鏈接
ln -sf /usr/local/php7.0/sbin/php-fpm /usr/bin/
ln -sf /usr/local/php7.0/bin/php /usr/bin/
ln -sf /usr/local/php7.0/bin/phpize /usr/bin/
ln -sf /usr/local/php7.0/bin/php-config /usr/bin/
ln -sf /usr/local/php7.0/bin/php-cgi /usr/bin/

php -m

#檢查配置是否正確
php-fpm -t

#啓動php-fpm
php-fpm

ps -ef |grep php-fpm
ps -ef | grep -c php-fpm

#關閉php-fpm
kill -INT `cat /usr/local/php7.0/var/run/php-fpm.pid`

#平滑重啓php-fpm
kill -USR2 `cat /usr/local/php7.0/var/run/php-fpm.pid`


5)安裝nodejs
#安裝nodejsv8.0.0
#nodejs官網地址:https://nodejs.org/en/
#nodejs安裝參考:http://www.runoob.com/nodejs/nodejs-install-setup.html
#nodejs安裝包地址:https://nodejs.org/download/release/v8.0.0/node-v8.0.0-linux-x64.tar.gz

cd /usr/local/src/
wget https://nodejs.org/download/release/v8.0.0/node-v8.0.0-linux-x64.tar.gz

tar zxvf node-v8.0.0-linux-x64.tar.gz -C $NODE_HOME

cd /usr/local/

chown -R root:root $NODE_HOME

#配置環境變量
vim /etc/profile

NODE_HOME=/usr/local/node8.0.0
PATH=$NODE_HOME/bin:$PATH
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export NODE_HOME PATH

:wq

#讓文件生效
source /etc/profile

驗證安裝是否成功
node -v
npm -v


6)安裝phantomjs
#安裝phantomjs1.9.8
#官網地址:http://phantomjs.org/download.html
#安裝包地址:https://bitbucket.org/ariya/phantomjs/downloads/

cd /usr/local/src/

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2

tar -jxvf phantomjs-1.9.8-linux-x86_64.tar.bz2

mv phantomjs-1.9.8-linux-x86_64 /usr/local/phantomjs1.9.8

cd /usr/local/

ln -sf /usr/local/phantomjs1.9.8/bin/phantomjs /usr/bin/


7)安裝phantomas、yslowjs
#安裝phantomasv1.20.0
#官網地址:https://github.com/macbre/phantomas
#安裝包地址:https://github.com/macbre/phantomas.git

#進去模塊安裝路徑
cd $NODE_HOME/lib/node_modules/

git clone https://github.com/macbre/phantomas.git

cd phantomas/

#大概4分鐘
npm install

cd $NODE_HOME/bin/

ln -sf ../lib/node_modules/phantomas/bin/phantomas.js phantomas

修改文件 /etc/profile
PHANTOMJS_HOME=/user/local/phantomjs1.9.8
PATH=$PHANTOMJS_HOME/bin:$PATH
export PHANTOMJS_HOME PATH
:wq

#驗證安裝是否成功
phantomas -v


#安裝yslowjs
#安裝包地址:http://yslow.org/phantomjs/ https://www.npmjs.com/package/yslowjs/tutorial

cd /$NODE_HOME/lib/node_modules/

npm install yslowjs


#修改yslow.js中內容,參考爲:
#args=phantom.args,
system = require('system'),args=system.args,


#執行測試
phantomjs yslow.js http://www.liveapp.cn/ --info all --format json grade -b http://192.168.64.133/beacon/yslow/

 

8)安裝mysql
#安裝mysql5.7.21
#訪問網站 https://dev.mysql.com/downloads/repo/yum/

#進入文件存放路徑
cd /usr/local/src/
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

#以yum方式安裝mysql
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm


#檢查mysql的yum repo源是否可用
yum repolist enabled |grep "mysql.*-community.*"


#以yum方式安裝mysql
yum -y install mysql-commuinty-server


#啓動mysql服務
systemctl start mysqld


#設置開機自動啓動
systemctl enable mysqld
systemctl daemon-reload

#獲取登陸MySQL的root用戶臨時密碼
grep 'temporary password' /var/log/mysqld.log


#利用臨時密碼,訪問mysql
#alter user 'root'@'localhost' identified by 'chineseA@123/#';
[root@centos7129 opt]# mysql -uroot -p
Enter password: 臨時密碼
mysql> alter user 'root'@'localhost' identified by 'chineseA@123/#';

#更改密碼策略
mysql> show variables like '%password';
set global validate_password_policy=0;
set global validate_password_length=4;

#再次重置密碼
mysql> alter user 'root'@'localhost' identified by '123456';

#配置mysql支持的字符集
#在[mysqld]配置項中,添加支持utf-8編碼
vim /etc/my.cnf
character-set-server=utf8
collation-server=utf8_unicode_ci
init_connect='SET NAMES utf8'

#重啓mysql
systemctl restart mysqld

#列出mysql常見路徑配置項
#配置文件路徑 /etc/my.cnf
#日誌文件路徑 /var/log/mysqld.log
#mysql啓動腳本 /usr/lib/systemd/system/mysqld.service
#socket文件路徑 /var/lib/mysql/mysql.sock
#pid文件 /var/run/mysqld/mysqld.pid


9)安裝Showslow
#安裝showslow1.2.2
#官網地址:https://github.com/sergeychernyshev/showslow/wiki/Installation-and-configuration
#安裝包地址:https://github.com/sergeychernyshev/showslow/releases/download/REL_1_2_2/showslow_1.2.2.tar.bz2

cd /usr/local/src/
wget https://github.com/sergeychernyshev/showslow/releases/download/REL_1_2_2/showslow_1.2.2.tar.bz2

tar -jxvf showslow_1.2.2.tar.bz2 -C /usr/local/

cd /usr/local/

chown -R root:root showslow_1.2.2

#創建軟鏈接
mv showslow_1.2.2 showslow
ln -s /usr/local/php7.0/bin/php /usr/local/bin/php

cd /usr/local/showslow/

cp config.sample.php config.php

#新建訪問showslow數據庫的用戶
#$ mysql -u root -p
#set global validate_password_policy=0;
#set global validate_password_length=4;
#create database showslow;
#grant usage on showslow.* to 'nginx'@'localhost' identified by 'Nginx@123456';
#grant usage on showslow.* to 'nginx'@'%' identified by 'Nginx@123456';
#grant usage on showslow.* to 'nginx'@'*' identified by 'Nginx@123456';
#grant usage on showslow.* to 'nginx'@'192.168.64.133' identified by 'Nginx@123456';
#grant all privileges on showslow.* to 'nginx'@'localhost';
#grant all privileges on showslow.* to 'nginx'@'%';
#grant all privileges on showslow.* to 'nginx'@'*';
#grant all privileges on showslow.* to 'nginx'@'192.168.64.133';
#flush privileges;
#quit


#修改config.php文件
vim config.php
date_default_timezone_set('Asia/Shanghai');
$db = 'showslow';
$user = 'nginx';
$pass = 'Nginx@123456';
$host = '192.168.64.133';
$port = 3306;
$socket = '/var/lib/mysql/mysql.sock';
:wq

#修改 /usr/local/showslow/global.php中1150行的內容,參考以下:
if (!is_null($socket)) {
#$global_conn=mysql_connect('localhost:'.$socket, $user, $pass,$db);
$global_conn=mysqli_connect($host, $user, $pass, $db, $port, $socket);
} else {
#$global_conn=mysql_connect("$host", $user, $pass,$db,$port);
$global_conn=mysqli_connect($host, $user, $pass, $db, $port);
}
mysqli_select_db($global_conn,$db);

# setting up connection settings to make MySQL communication more strict
$result = mysqli_query($global_conn,'SET SESSION SQL_MODE=STRICT_ALL_TABLES');

if (!$result) {
beaconError(mysqli_error($global_conn));
}

修改文件 /usr/local/showslow/dbupgrade.php 中718行的內容,參考以下:
#`last_event_update` timestamp NOT NULL default '0000-00-00 00:00:00' COMMENT 'Last time events were updated for this URL',
`last_event_update` timestamp NOT NULL default CURRENT_TIMESTAMP COMMENT 'Last time events were updated for this URL',

修改文件 /usr/local/showslow/users/dbupgrade.php 中718行的內容,參考以下:
#$versions[2]['up'][] = 'ALTER TABLE '.UserConfig::$mysql_prefix.'users ADD last_accessed TIMESTAMP';
$versions[2]['up'][] = 'ALTER TABLE '.UserConfig::$mysql_prefix.'users ADD last_accessed TIMESTAMP default CURRENT_TIMESTAMP';

make

經過這個地址訪問首頁:http://192.168.64.133/users/login.php


#配置dommonster
http://192.168.64.133/beacon/dommonster/


#收集數據示例
phantomjs yslow.js http://www.liveapp.cn/ --info all --format json grade -b http://192.168.64.133/beacon/yslow/

 

10)安裝HARViewer
#安裝phantomasv1.20.0
#官網地址:http://www.softwareishard.com/blog/har-viewer/
#安裝包地址:https://github.com/janodvarko/harviewer

cd /usr/local/src/
git clone https://github.com/janodvarko/harviewer.git

#將文件webapp目錄複製到 showslow目錄下,並更名爲 harviewer

cd harviewer/
cp -R webapp /usr/local/showslow/
cd /usr/local/showslow/
mv webapp harviewer

#harviewer訪問地址:
http://192.168.64.133/harviewer/

#經過chrome導出的har信息,複製黏貼到打開的瀏覽器中的文本框中,點擊Preview就能夠看到分析信息了。

相關文章
相關標籤/搜索