jenkins+phantomjs环境搭建及使用

#jenkins+phantomjs 前端性能自动化测试的安装和使用
#gcc GNU编译器套件 https://gcc.gnu.org/

#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-yslowjs

#Harviewer 可视化查询har文件的工具 http://www.softwareishard.com/blog/har-viewer/ https://github.com/janodvarko/harviewer
#NetExport 将http加载的详细性能数据导出为har文件,并上传给 Showslow

#jenkins 监控和调度执行job https://jenkins.io/

#操作系统:
#CentOS Linux release 7.3.1611 (Core)

#安装包版本:
#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版本


1)安装依赖包
#查看版本
python -v
make -v


#安装依赖包epel
#epel包地址:http://dl.fedoraproject.org/pub/epel/7/x86_64/

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

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

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就可以看到分析信息了。

原文地址:https://www.cnblogs.com/NiceTime/p/8567613.html