> CentOS > CentOS服务器 > 环境配置 >

centos6.5 yum安装nginx+hhvm+mariadb环境

一直都听说hhvm的性能怎么怎么好,速度怎么怎么快,就想自己也搭建个环境来测试下,在搭建的中途遇到的了各种坑,让我拖到今天才终于把hhvm搭建成功.好了,下面来看教程吧.
  系统:centos 6.5(64位)
1.添加搭建hhvm需要的源 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
wget -O /etc/yum.repos.d/hop5.repo http://www.hop5.in/yum/el6/hop5.repo
rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
或者
rpm -Uvh http://download.slogra.com/yum-x86_64/epel-release-6-8.noarch.rpm

yum -y install 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

2.安装hhvm
yum install hhvm -y
点击查看原图
3.安装nginx
rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx -y

4.安装MariaDB
vi /etc/yum.repos.d/MariaDB.repo
1 [mariadb]
2 name = MariaDB
3 baseurl = http://yum.mariadb.org/5.5/centos6-amd64
4 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
5 gpgcheck=1


yum install MariaDB-server MariaDB-client MariaDB-devel -y

5.验证hhvm
hhvm --version

6.修改hhvm配置文件
cp /etc/init.d/hhvm /etc/init.d/hhvm.old--
cp /etc/hhvm/config.hdf /etc/hhvm/config.hdf.old--
cp /etc/hhvm/php.ini /etc/hhvm/php.ini.old--
cp /etc/hhvm/server.hdf /etc/hhvm/server.hdf.old--

wget -O /etc/init.d/hhvm https://cdn.zntec.cn/store/tools/vhost_hhvm/hhvm
或者
wget -O /etc/init.d/hhvm http://download.slogra.com/hhvm/hhvm
chmod +x /etc/init.d/hhvm

cd /etc/hhvm
vi server.ini
01 ; php options
02 pid = /var/run/hhvm/pid
03  
04 ; hhvm specific
05 ;hhvm.server.port = 9001
06 hhvm.server.file_socket = /var/run/hhvm/sock
07 hhvm.server.type = fastcgi
08 hhvm.server.default_document = index.php
09 hhvm.log.use_log_file = true
10 hhvm.log.file = /var/log/hhvm/error.log
11 hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc


vi config.hdf
01 ResourceLimit {
02   CoreFileSize = 0         # in bytes
03   MaxSocket = 10000        # must be not 0, otherwise HHVM will not start
04   SocketDefaultTimeout = 5 # in seconds
05   MaxRSS = 0
06   MaxRSSPollingCycle = 0   # in seconds, how often to check max memory
07   DropCacheCycle = 0       # in seconds, how often to drop disk cache
08 }
09  
10 Log {
11   Level = Info
12   AlwaysLogUnhandledExceptions = true
13   RuntimeErrorReportingLevel = 8191
14   UseLogFile = true
15   UseSyslog = false
16   File = /var/log/hhvm/error.log
17   Access {
18     * {
19       File = /var/log/hhvm/access.log
20       Format = %h %l %u % t \"%r\" %>s %b
21     }
22   }
23 }
24  
25 MySQL {
26   ReadOnly = false
27   ConnectTimeout = 1000     # in ms
28   ReadTimeout = 1000        # in ms
29   SlowQueryThreshold = 1000 # in ms, log slow queries as errors
30   KillOnTimeout = false
31 }
32  
33 Mail {
34   SendmailPath = /usr/sbin/sendmail -t -i
35   ForceExtraParameters =
36 }


vi php.ini
1 hhvm.mysql.socket = /var/lib/mysql/mysql.sock ;
2 expose_php = 0 ;
3 memory_limit = 400M
4 post_max_size = 50M


vi /etc/nginx/hhvm.conf
1 location ~ [^/]\.php(/|$) {
2     fastcgi_keep_conn on;
3     fastcgi_pass   unix:/var/run/hhvm/sock;
4     fastcgi_index  index.php;
5     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
6     include        fastcgi_params;
7 }


配置nginx虚拟主机:
vi /etc/nginx/conf.d/www.conf
01 server {
02     listen 80;
03     server_name 192.168.10.241;  #这里修改成你自己的域名或ip
04     index index.html index.htm index.php;
05     root  /var/www/vhosts/;      #这里修改成你自己的目录
06  
07     #include wordpress.conf;
08     include hhvm.conf;
09  
10     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
11         expires      30d;
12     }
13  
14     location ~ .*\.(js|css)?$ {
15         expires      12h;
16     }
17  
18     access_log  /var/log/nginx/access.log;
19 }


配置完成后,启动nginx hhvm MariaDB
service mysql start
service hhvm start
service nginx start
chkconfig mysql on
chkconfig hhvm on
chkconfig nginx on

mysql -u root 
MariaDB [(none)]>UPDATE mysql.user SET password=PASSWORD('passwd') WHERE User='root';
MariaDB [(none)]>FLUSH PRIVILEGES;

好了,现在我们来测试hhvm是否可以连上mariadb.
vi /var/www/vhosts/index.php
01 <?php
02 phpinfo();
03 $link=mysql_connect("localhost","root","passwd");
04 if(!$link) echo "FAILD!连接错误,用户名密码不对,这是slogra的测试";
05 else echo "is OK!可以连接,这是slogra的测试";
06  
07 mysql_select_db("test");
08 $sql = "CREATE TABLE user_info
09 (
10 user_id varchar(15),
11 user_name varchar(15),
12 user_age int
13 )";
14  
15 mysql_query($sql,$link);
16  
17 mysql_close($link);
18  
19 ?>


保存后,我们在浏览器访问http://192.168.10.241
点击查看原图
可以看到hhvm连接mariadb是成功的,现在我们进mariadb里看是否有数据写入.
mysql -u root -p
MariaDB [(none)]>use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [(none)]>show tables;
+----------------+
| Tables_in_test |
+----------------+
| user_info      |
+----------------+
1 row in set (0.00 sec)
MariaDB [(none)]>desc user_info;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| user_id   | varchar(15) | YES  |     | NULL    |       |
| user_name | varchar(15) | YES  |     | NULL    |       |
| user_age  | int(11)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

可以看到有数据写入,证明hhvm跟mariadb之间通信是成功的.
好了,本文就到这里了,有兴趣的朋友可以自己继续去研究.


(责任编辑:IT)