系统升级

yum update
yum install pcre-devel openssl openssl-devel libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-GD php-gd GeoIP GeoIP-devel ImageMagick ImageMagick-devel libcurl-devel libicu-devel openldap-devel net-tools wget gcc gcc-c++ autoconf automake

准备工作

将所有安装文件下载至/home/soft中,在此目录下解压编译安装。

wget https://ftp.pcre.org/pub/pcre/pcre-8.30.tar.gz 
wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz
wget http://tengine.taobao.org/download/tengine-2.2.1.tar.gz
wget http://php.net/distributions/php-7.3.9.tar.gz

PCRE安装

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx rewrite依赖于PCRE库,所以在安装Tengine前一定要先安装PCRE,最新版本的PCRE可在官网(http://www.pcre.org/)获取。具体安装流程为:

cd /home/soft
tar -zxf pcre-8.30.tar.gz
cd pcre-8.30
./configure --prefix=/usr/local/pcre-8.30
make
make install

OpenSSL安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。,安装OpenSSL(http://www.openssl.org/source/)主要是为了让tengine支持Https的访问请求。具体是否安装看需求。

cd /home/soft
tar -zxf openssl-1.0.2k.tar.gz
cd openssl-1.0.2k
./config --prefix=/usr/local/openssl-1.0.2
make
make install

Tengine安装

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

安装Tengine

cd /home/soft
tar -zxf tengine-2.2.1.tar.gz
cd tengine-2.2.1
./configure --prefix=/usr/local/tengine-2.2.1 --user=nobody --group=nobody --with-pcre=/home/soft/pcre-8.30 --with-pcre-jit --with-openssl=/home/soft/openssl-1.0.2k --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_concat_module --with-http_ssl_module --with-http_v2_module --with-http_geoip_module
make
make install

添加 /etc/init.d/nginx 启动脚本

vi /etc/init.d/nginx

Linux中按 i ,切换至insert状态,将以下内容完整粘贴

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for tengine webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f tengine defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add tengine'

### BEGIN INIT INFO
# Provides:          tengine
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the tengine web server
# Description:       starts tengine using start-stop-daemon
### END INIT INFO

# Author:   fy
# website:  http://www.yiyou.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/tengine/sbin/$NAME
CONFIGFILE=/usr/local/tengine/conf/$NAME.conf
PIDFILE=/usr/local/tengine/logs/$NAME.pid

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
        ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;

esac

esc ,退出编辑状态,输入 :wq 保存并退出。

设置开机自启动

cd /usr/local/
ln -s tengine-2.2.1 tengine
mkdir /usr/local/tengine/conf/vhost
chmod 755 /etc/init.d/nginx
chkconfig nginx on

Tengine 配置文件

修改配置文件

vi /usr/local/tengine/conf/nginx.conf

将内容替换如下,其中worker_processes根据CPU的核心数而定。

user  nobody;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    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  /home/httplogs/access.log  main;
    sendfile        on;
    gzip on;
    gzip_http_version 1.1;
    gzip_min_length 1024;
    gzip_buffers   4 16k;
    gzip_comp_level 6;
    gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
    gzip_proxied     any;
    gzip_disable     "MSIE [1-6]\.";

    server_tokens off;
    client_max_body_size 20m;
    keepalive_timeout  65;
    upstream php {
        server 127.0.0.1:9000;
    }
    server {
        listen       80;
        root /home/wwwroot/default;
        index  index.php index.html index.htm;
        location ~ \.php$ {
            fastcgi_pass php;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        access_log  /home/httplogs/default.access.log;
        error_log  /home/httplogs/default.error.log;
    }
    add_header Access-Control-Allow-Origin *;                                                                                                                       
    include vhost/*.conf;                                                                                                                                           
}

vhost/default.conf

此处是具体的虚拟主机配置,略……

php 7 安装准备工作

libzip 1.3.2安装

yum -y remove libzip-devel
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar xvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make && make install

ld.so.conf修改

# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
ldconfig -v

php 7 安装

安装 php 7

./configure --prefix=/usr/local/php-7.3.9 --with-libdir=lib64 --enable-fpm --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --enable-opcache --enable-pcntl --enable-mbstring --enable-soap --enable-zip --enable-calendar --enable-bcmath --enable-exif --enable-ftp --enable-intl --with-openssl --with-zlib --with-curl --with-gd --with-jpeg-dir=/usr/lib64 --with-png-dir=/usr/lib64 --with-freetype-dir=/usr/lib64 --with-xpm-dir=/usr/lib64 --with-zlib --with-gettext --with-mhash --with-ldap
make
make install

添加启动脚本

添加 /etc/init.d/php-fpm 启动脚本,并设置开机自启动

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
chkconfig php-fpm on
cd /usr/local/
ln -s php-7.3.9 php
vi /usr/local/php/lib/php.ini

php-fpm.conf内容范例

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice

[www]
listen = 127.0.0.1:9000
user = nobody
group = nobody
pm = dynamic
pm.max_children = 64
pm.start_servers = 24
pm.min_spare_servers = 8
pm.max_spare_servers = 40
pm.max_requests = 600
rlimit_files = 20000
slowlog = var/log/$pool.log.slow
request_slowlog_timeout = 10
catch_workers_output = yes

php.ini内容范例

[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = Off
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 20M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
always_populate_raw_post_data = -1
include_path = ".:/usr/local/lib/php"
doc_root =
user_dir =
enable_dl = Off
cgi.fix_pathinfo=0
file_uploads = On
;upload_tmp_dir =
upload_max_filesize = 20M
max_file_uploads = 20
allow_url_fopen = On                                                                                                                                               
allow_url_include = Off                                                                                                                                            
default_socket_timeout = 60                                                                                                                                        
[CLI Server]                                                                                                                                                       
cli_server.color = On                                                                                                                                              
[Date]                                                                                                                                                             
date.timezone = Asia/Shanghai                                                                                                                                      
                                                                                                                                                                   
[filter]                                                                                                                                                           
                                                                                                                                                                   
[iconv]                                                                                                                                                            
                                                                                                                                                                   
[intl]                                                                                                                                                             
                                                                                                                                                                   
[sqlite]                                                                                                                                                           
                                                                                                                                                                   
[sqlite3]                                                                                                                                                          
                                                                                                                                                                   
[Pcre]                                                                                                                                                             
                                                                                                                                                                   
[Pdo]                                                                                                                                                              
                                                                                                                                                                   
[Pdo_mysql]                                                                                                                                                        
pdo_mysql.cache_size = 2000                                                                                                                                        
pdo_mysql.default_socket=                                                                                                                                          
                                                                                                                                                                   
[Phar]                                                                                                                                                             
                                                                                                                                                                   
[mail function]                                                                                                                                                    
SMTP = localhost                                                                                                                                                   
smtp_port = 25                                                                                                                                                     
sendmail_path = /usr/sbin/sendmail -t -i                                                                                                                           
mail.add_x_header = On                                                                                                                                             
                                                                                                                                                                   
[SQL]
sql.safe_mode = Off

[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1

[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"

[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off

[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off

[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off

[OCI8]

[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0

[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10

[bcmath]
bcmath.scale = 0

[browscap]

[Session]
session.save_handler = files
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"

[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off

[Assertion]

[mbstring]

[gd]

[exif]

[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=64
opcache.interned_strings_buffer=4
opcache.max_accelerated_files=2000
opcache.revalidate_freq=2
opcache.fast_shutdown=1

安装yaf

php 7 对应的版本为 yaf 3,以下为当前最新版本 3.0.8 为例

cd /home/soft/
wget http://pecl.php.net/get/yaf-3.0.8.tgz
tar -zxf yaf-3.0.8.tgz
cd yaf-3.0.8/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

安装完成后,在php.ini中添加以下内容即可启用yaf

[yaf]
extension=yaf.so
yaf.environ="product"
;yaf.library=NULL
;yaf.cache_config=0
;yaf.name_suffix= 1
;yaf.name_separator=""
;yaf.forward_limit=5
yaf.use_namespace=1
;yaf.use_spl_autoload=1