关于IT运维技术的
最佳实践博客网站

搭建Centos7+Nginx+HHVM+Jemalloc+ImageMagick高性能服务器

Hiphop是Facebook开发一款PHP二进制化的一个工具,最开始是由php转为C++,但是后来发现编译为c++的话,许多的时间会花费在编译代码上面,调试不方便,对于代码来说也不是即见即所得。

所以hiphop经历了这么几个阶段:

HPHPC=>HPHPI=>HHVM

HPHPC是静态编译,也就是把php转为c++

HPHPI是一个过渡产品,类似php zend虚拟机,性能还不如zend虚拟机,但是可以运行查看效果;

HHVM是在HPHPI基础上,应用了JIT技术,性能已经接近了HPHPC,目前facebook打算慢慢抛弃HPHPC;

性能上HPHPC和HHVM比zend虚拟机+加速器要节约cpu在50%在300%间(官方提供),,我实际应用中,一般节约CPU在100%-300%间左右,流量越大越明显。

HHVM几乎已经完全支持PHP了,但还是有不少bug,所以不是所有应用程序都可以运行。因此,Facebook准备让最流行的20个开源PHP项目能够运行在HHVM上。第一个已经支持的项目就是Wordpress。

HHVM官网

1、官方网站:https://www.hhvm.com
2、项目地址:https://github.com/facebook/hhvm

1、基本依赖软件包

yum -y install git libmcrypt-devel glog-devel jemalloc-devel tbb-devel libdwarf-devel mysql-devel libxml2-devel libicu-devel pcre-devel gd-devel boost-devel sqlite-devel pam-devel bzip2-devel oniguruma-devel openldap-devel readline-devel libc-client-devel libcap-devel libevent-devel libcurl-devel libmemcached-devel gcc gcc-c++ ncurses-devel perl ImageMagick-devel binutils-devel elfutils-libelf-devel libedit-devel libxslt-devel libyaml-devel libzip-devel unixODBC-devel cmake elfutils-libelf-devel enca gmp-devel gperf libedit-devel libvpx-devel libxslt-devel libyaml-devel libzip-devel lz4-devel ocaml psmisc unixODBC-devel openssl-devel

2、安装nginx

cd /usr/local/src
wget https://nginx.org/download/nginx-1.9.2.tar.gz
tar zxvf nginx-1.9.2.tar.gz
./configure
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-https_stub_status_module \
--with-https_ssl_module \
--with-https_gzip_static_module \
--with-ld-opt=-ljemalloc \
--with-https_realip_module

使用jemalloc优化Nginx

--with-ld-opt=-ljemalloc

3、添加启动脚本

vim /usr/lib/systemd/system/nginx.service

systemctl 操作nginx的脚本

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

赋予执行权限:

chmod +x /usr/lib/systemd/system/nginx.service

操作nginx的命令:

#开机启动
systemctl enable nginx.service
#启动nginx
systemctl start nginx.service
#停止nginx
systemctl stop nginx.service
#重启nginx
systemctl restart nginx.service
#配置重载
systemctl reload nginx.service

验证jemalloc优化Nginx是否生效

lsof -n | grep jemalloc

4、安装HHVM

依赖关系:google-glog 和 oniguruma

google-glog 是一个基于程序级记录日志信息的c++库,编程使用方式与c++的stream操作类似

cd /usr/local/src/
wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz
tar xvzf glog-0.3.3.tar.gz
cd glog-0.3.3
./configure
make && make install
cd ..

oniguruma 是一个正则表达式扩展库

wget https://www.geocities.jp/kosako3/oniguruma/archive/onig-5.9.5.tar.gz 
tar xvzf onig-5.9.5.tar.gz
cd onig-5.9.5
./configure
make && make install
cd ..

拉取hhvm源码以及第三方库(有点慢)

git clone https://github.com/facebook/hhvm -b master  hhvm  --recursive
cd hhvm
./configure

使用ImageMagick,提升图片处理性能。

rpm -Uvh https://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y remove ImageMagick
yum -y install ImageMagick-last* --enablerepo=remi

加入ImageMagick库 注意结尾有个点 .

cmake -D LIBMAGICKWAND_INCLUDE_DIRS="/usr/include/ImageMagick-6" -D LIBODBC_INCLUDE_DIRS="/usr/lib64" -D LIBODBC_LIBRARIES="/usr/lib64" -D LIBVPX_INCLUDE_DIRS="/usr/lib64" -D LIBXSLT_INCLUDE_DIR="/usr/lib64" -D LIBXSLT_LIBRARIES="/usr/lib64/libxslt.so"  -D LIBMAGICKWAND_LIBRARIES="/usr/lib64/libMagickWand-6.Q16.so" -D LIBMAGICKCORE_LIBRARIES="/usr/lib64/libMagickCore-6.Q16.so" .

编译

#利用cpu多核编译加快速度,2代表核数,单核不用加-j2,
#查看cpu核数
cat /proc/cpuinfo | grep processor | wc -l
#编译
make -j2

编译完成后执行

make install

查看HHVM版本

[root@lnmp hhvm]# hhvm --version
HipHop VM 3.8.0-dev (rel)
Compiler: heads/master-0-gf8f815d06f7b791a2f32dbb5552e4e2033b0ca8d
Repo schema: b9dcb1226031b50940875a9dbe21f078c80f6de0

配置HHVM

创建并修改hhvm目录权限

mkdir /etc/hhvm /var/run/hhvm /var/log/hhvm
chown -R www.www /var/run/hhvm /var/log/hhvm

修改/usr/local/nginx/conf/nginx.conf使其中一段成为如下

location ~ .*\.(php|php5)?$  {
                fastcgi_pass   unix:/var/run/hhvm/sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include        fastcgi_params;
                }

注意:fastcgi_pass 这里启动的是HHVM的sock,如果是PHP,就是php-fpm.sock

创建hhvm配置文件/etc/hhvm/config.hdf

ResourceLimit {
  CoreFileSize = 0          # in bytes
  MaxSocket = 10000         # must be not 0, otherwise HHVM will not start
  SocketDefaultTimeout = 5  # in seconds
  MaxRSS = 0
  MaxRSSPollingCycle = 0    # in seconds, how often to check max memory
  DropCacheCycle = 0        # in seconds, how often to drop disk cache
}

Log {
  Level = Info
  AlwaysLogUnhandledExceptions = true
  RuntimeErrorReportingLevel = 8191
  UseLogFile = true
  UseSyslog = false
  File = /var/log/hhvm/error.log
  Access {
    * {
      File = /var/log/hhvm/access.log
      Format = %h %l %u % t \"%r\" %>s %b
    }
  }
}

MySQL {
  ReadOnly = false
  ConnectTimeout = 1000      # in ms
  ReadTimeout = 1000         # in ms
  SlowQueryThreshold = 1000  # in ms, log slow queries as errors
  KillOnTimeout = false
}

Mail {
  SendmailPath = /usr/sbin/sendmail -t -i
  ForceExtraParameters =
}

配置hhvm修改/etc/hhvm/server.ini

; php options
pid = /var/run/hhvm/pid

; hhvm specific
;hhvm.server.port = 9001
hhvm.server.file_socket = /var/run/hhvm/sock
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

修改/etc/hhvm/php.ini指定mysql.sock路径(hhvm默认路径sock路径是/var/lib/mysql/mysql.sock)

hhvm.mysql.socket = /tmp/mysql.sock
expose_php = 0 ;关闭头信息X-Powered-By (和hhvm.server.expose_hphp = false作用一样)
memory_limit = 400M
post_max_size = 50M

添加开机自启动脚本
保证文件/etc/systemd/system/hhvm.service内容如下:

[Unit]
Description=HHVM HipHop Virtual Machine (FCGI)

[Service]
ExecStart=/usr/local/bin/hhvm --mode daemon --user www --config /etc/hhvm/server.ini \
--config /etc/hhvm/php.ini --config /etc/hhvm/config.hdf

[Install]
WantedBy=multi-user.target

管理hhvm服务

systemctl enable hhvm #开机自启动
systemctl start hhvm #启动hhvm
systemctl status hhvm #查看hhvm状态

使用PHP探针测试:

cd /home/wwwroot/default
wget https://www.yahei.net/tz/tz.zip
unzip tz.zip

访问:https://IP/tz.php

程序测试:wordpress,完美!

性能测试参考:

https://www.freehao123.com/lnmpa/

本文参考文章来自:

https://blog.linuxeye.com/402.html

https://github.com/facebook/hhvm/wiki/Building-and-installing-hhvm-on-CentOS-7.x

未经允许不得转载:菜鸟HOW站长 » 搭建Centos7+Nginx+HHVM+Jemalloc+ImageMagick高性能服务器
分享到: 更多 (0)

留下你的脚印

希望您的评论能帮助更多人

关注动态
wpDiscuz