Centos7编译安装Tengine和PHP7

发布于 2016-04-19  5655 次阅读


哀差闷闲着没事干,听说php7的速度翻了翻,就想配置个玩玩,搭载上阿里牛鼻的tengine。顺便开个https装装逼。

  • 首先要介绍下Tengine:

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

  • 然后是php7:

php7效率是php5.3的4倍,是5.5的2倍,换成PHP7,wordpress性能提升3倍。Rasmus Lerdorf与PHP核心贡献团队花了许多心力减少程序运作时搬动的内存位数,由此加速执行的性能。例如,PHP中储存变量的数据架构zval从24位缩减至16位、Hashtable从72位减少至56位。

天朝的技术一般比较落后,国内的web环境普遍是php5.3的版本。所以哀差闷这次要勇敢的追上技术的浪潮。:)

  • 最后是https

HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。传统的HTTP模式,存在着大量的灰色中间环节,相关信息很容易被窃取,但HTTPS却是通过认证用户与服务器,将数据准确地发送到客户机与服务器,并采用加密方式以防数据中途被盗取,大大降低了第三方窃取信息、篡改冒充身份的风险。

  • 总结

Tengine + PHP7 + HTTPS +MySql 就是一个牛逼的、开源的、免费的、高效的、安全的web环境。而哀差闷已经准备好用这个来装逼了:)~

准备

  • 关闭防火墙
[root@CentOS ~]# service iptables stop
  • 关闭selinux(据说selinux会影响编译)
[root@Centos ~]# /usr/sbin/sestatus -v
#查看SElinux的状态,腾讯云提供的centos默认未启动
SELinux status:                 disabled
[root@Centos ~]# setenforce 0
#临时关闭,setenforce 1表示开启
[root@Centos ~]# vim /etc/selinux/config
#永久关闭,需要重启:修改配置文件,将SELINUX=enforcing改为SELINUX=disabled
  • 下载tengine:http://tengine.taobao.org/      哀差闷使用最新版2.1.2
  • 下载php7:http://php.net/downloads.php  哀差闷使用php-7.3.10.tar.gz (sig)
  • 安装相关类库和开发包:
[root@CentOS ~]# yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel libzip libzip-devel

编译安装Tengine

[root@Centos home]# tar zxvf tengine-2.1.2.tar.gz
[root@Centos home]# cd tengine-2.1.2
[root@Centos tengine-2.1.2]# ./configure --user=nginx --group=nginx --prefix=/usr/share/nginx --with-http_stub_status_module --with-http_ssl_module  --with-pcre --with-http_gzip_static_module --with-http_v2_module
[root@Centos tengine-2.1.2]# make
[root@Centos tengine-2.1.2]# make install

OK编译安装完毕,上面给nginx指定的是nginx用户和用户组。如果这个用户和用户组不存在,启动nginx时会报错:nginx: [emerg] getpwnam("nginx") failed

[root@Centos tengine-2.1.2]# /usr/sbin/groupadd -f nginx    #创建nginx用户组
[root@Centos tengine-2.1.2]# /usr/sbin/useradd -g nginx nginx    #在nginx用户组中创建nginx用户
[root@Centos tengine-2.1.2]# /usr/share/nginx/sbin/nginx    #启动nginx(tengine)

使用浏览器访问试试,不出意外是可以看到Welcome to tengine!

如果要在一个公网IP上部署多个Https域名,需要开启TLS支持,详情可参考:http://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

设置tengine开启启动,新建启动文件:

[root@Centos ~]# vim /etc/rc.d/init.d/nginx

往里面添加下面内容:

#nx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/share/nginx/conf/nginx.conf
nginxd=/usr/share/nginx/sbin/nginx
nginx_config=/usr/share/nginx/conf/nginx.conf
nginx_pid=/usr/share/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n
quot;Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n
quot;Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/share/nginx/logs/nginx.pid } reload() { echo -n
quot;Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo
quot;Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
[root@Centos ~]# chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限
[root@Centos ~]# chkconfig nginx on #设置开机启动
[root@Centos ~]# service nginx restart

到此Tengine编译安装完毕

经过上面安装的nginx后。

  • web根目录在/usr/share/nginx/htnl
  • nginx配置文件在/usr/share/nginx/conf/nginx.conf

Yum安装MySql

[root@CentOS ~]# yum install mysql-server
[root@CentOS ~]# service mysqld start
[root@CentOS ~]# chkconfig --levels 235 mysqld on

进入mysql控制台,删除空用户,修改root密码

[root@CentOS ~]#mysql          #打开mysql控制台

mysql>select user,host,password from mysql.user;
 
mysql>drop user ''@localhost;
 
mysql>update mysql.user set password = PASSWORD('xxxxxxx') where user='root';
 
mysql>flush privileges;
mysql>\q #退出

数据库就不使用编译安装了,还是用yum方便!

记录一个方法,当忘记root密码时:vim /etc/my.cnf,在[mysqld]下面添加skip-grant-tables,即可以使用空密码登录mysql控制台,从而修改密码。修改完成后记得把skip-grant-tables删掉!重新启动mysqld

编译安装PHP7

下载php7:http://php.net/downloads.php  哀差闷使用php-7.0.5.tar.gz (sig)

首先创建php的用户和用户组,该php用户没有登录权限

[root@Centos ~]# groupadd -r php 
[root@Centos ~]# useradd -r -g php -s /bin/false -d /usr/share/php7 -M php
[root@Centos home]# tar zxvf php-7.0.5.tar.gz
[root@Centos home]# cd php-7.0.5
[root@Centos php-7.0.5]# ./configure --prefix=/usr/local/php7 --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --disable-fileinfo

上面的编译参数中指定了php的安装路径为/usr/share/php7、指定了php-fpm的用户和用户组为nginx。如果最后提示configure: WARNING: unrecognized options: --with-mysql, --enable-fastcgi,可以忽略。

运行下面的命令编译安装php

[root@Centos php-7.0.5]# make clean
[root@Centos php-7.0.5]# make
[root@Centos php-7.0.5]# make install

处理php7相关的配置文件php.ini、phph-fpm、php-fpm.conf、www.conf

[root@Centos ~]# cp /home/php-7.0.5/php.ini-production /usr/share/php7/etc/php.ini
#提示这个可以忽略:You have mail in /var/spool/mail/root
[root@Centos ~]# cp /home/php-7.0.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@Centos ~]# cp /usr/share/php7/etc/php-fpm.conf.default /usr/share/php7/etc/php-fpm.conf
[root@Centos ~]# cp /usr/share/php7/etc/php-fpm.d/www.conf.default /usr/share/php7/etc/php-fpm.d/www.conf

添加php的环境变量

[root@Centos ~]# echo -e '\nexport PATH=/usr/share/php7/bin:/usr/share/php7/sbin:$PATH\n' >> /etc/profile && source /etc/profile

创建一个文件夹,用来存放php的数据库文件,权限设为777,用户和用户组都为php,例如哀差闷:

[root@Centos ~]# mkdir -p /myl/phpMysqlData_tmp
[root@Centos ~]# chown -R php:php /myl/phpMysqlData_tmp
[root@Centos ~]# chmod 777 -R /myl/phpMysqlData_tmp

设置PHP日志目录和php-fpm的运行进程ID文件(php-fpm.sock)目录

[root@Centos ~]# mkdir -p /var/log/php-fpm/ 
[root@Centos ~]# mkdir -p /var/run/php-fpm 
[root@Centos ~]# cd /var/run/ && chown -R nginx:nginx php-fpm

修改session的目录配置

[root@Centos ~]# mkdir -p /var/lib/php/session
[root@Centos ~]# chown -R nginx:nginx /var/lib/php

设置php-fpm开机启动

[root@Centos ~]# chmod +x /etc/init.d/php-fpm
[root@Centos ~]# chkconfig --add php-fpm
[root@Centos ~]# chkconfig php-fpm on

测试PHP的配置文件是否正确

[root@Centos ~]# php-fpm -t
[19-Apr-2016 22:00:49] NOTICE: configuration file /usr/share/php7/etc/php-fpm.conf test is successful

启动php-fpm服务:

[root@Centos ~]# service php-fpm start
Starting php-fpm <span class="keyword">done</span>

可以通过命令 php -v 查看当前PHP版本信息,可以看到当前PHP7还使用了Zend OPcache缓存,因为在php.ini文件中添加了zend_extension=opcache.so 配置。

[root@Centos ~]# php -v
PHP 7.0.5 (cli) (built: Apr 19 2016 18:47:43) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

最后配置nginx使其支持php

 //修改nginx配置文件,添加fastcgi支持
[root@CentOS ~]# vim /usr/share/nginx/conf/nginx.conf
location / {
             root   html;
             index  index.php index.html index.htm;
         }
在上面的index后面加入index.php

location ~ \.php$ {
             root           /usr/share/nginx/html;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_sc    ript_name;
             include        fastcgi_params;
         }
//将以上代码注释去掉,并修改10行和13行的nginx默认路径

重启nginx和php-fpm

[root@CentOS ~]# service nginx restart
[root@CentOS ~]# service php-fpm restart

测试,在web根目录新建info.php,在里面写入下面的代码:

<?php
      phpinfo();
?>

保存,公共ip访问看看。不出意外是可以看到php的信息的。

OK。至此,一个完整的web服务器环境搭建完成

phpMyAdmin

phpmyadmin的安装可以参考哀差闷的另一篇整理:腾讯云主机Centos6.5用yum搭建LNMP环境完全攻略

哀差闷遇到一个问题:phpMyAdmin提示#2002 无法登录 MySQL 服务器和

phpMyAdmin - 错误

缺少 mysqli 扩展。请检查 PHP 配置。 See our documentation for more information.

解决方法如下:编辑/usr/share/php7/etc/php.ini 找到(大约在1100行多一点的位置)mysql.default_socket = 在后面添加上/var/lib/mysql/mysql.sock

mysql.sock这个文件的路径可以在mysql日志中找到/var/log/mysqld.log。

同时在Dynamic Extensions部分添加下面两句:

extension=mysqli.so

extension=pdo_mysql.so

重新启动php-fpm

配置https

配置https需要使用OpenSSL的证书,怎么获得这个证书请看博主的另一篇文章:

由于申请ssL证书需要域名对应的邮箱。所以我还要搭建一个邮箱服务器,心累!

关键是我的域名还没办法解析!这里不得不谴责下腾讯云了,上午刚接到邮件说备案成功了,我高兴的马上来配置这个高大的web环境。然而把域名设置解析后,等了一个小时,两个小时,没解析成功(心里暗骂腾讯的DNS太差了吧),直到晚上还是没解析。感觉不正常了。于是去控制台看看,竟然发现域名竟然没有实名认证,醉了,都备案成功了,还没有实名认证,也不提醒下。乖乖的上缴了身份证,竟然告诉了我需要4-5个工作日,泪崩。

本部分已放弃,因为https的页面无法引用http的资源,否则浏览器会提示安全警告。这意味需要做很多的本地化工作。反而会拖累服务器,装这个逼代价太高,不值得

参考

淘宝开源Web服务器Tengine基本安装步骤

2015博客升级记(五):CentOS 7.1编译安装PHP7


风雨兼程路,雨雪初霁时