更新系统软件

yum update

安装编译工具

yum install gcc automake autoconf libtool gcc-c++

安装服务端程序(二选一)

(a) nginx

  1. 安装nginx源

    yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  2. 安装nginx
    yum install nginx
  3. 启动nginx
    service nginx start

    成功显示: Redirecting to /bin/systemctl start nginx.service

(b) tengine

  1. 安装编译工具

    yum install gcc-c++
    yum -y install pcre pcre-devel
    yum -y install zlib zlib-devel
    yum -y install openssl openssl-devel
  2. 下载 tengine 并编译安装

    wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
    tar -zxvf tengine-2.2.0.tar.gz
    
    ./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid
    
    make && make install
  3. 测试运行 tengine

    /usr/local/nginx/sbin/nginx
  4. 设置为开机自启动

    cd /lib/systemd/system && vim nginx.service
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    修改权限并设置开机启动

    chmod 745 nginx.service
    systemctl enable nginx.service

安装MySQL5.7.*

  1. 安装mysql源

    yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
  2. 安装mysql

    yum install mysql-community-server

    确认一下mysql的版本,有时可能会提示mysql5.6

  3. 安装mysql的开发包,以后会有用

    yum install mysql-community-devel
  4. 启动mysql

    service mysqld start

    成功返回 Redirecting to /bin/systemctl start mysqld.service

  5. 查看mysql启动状态

    service mysqld status

    出现pid证明启动成功

  6. 获取mysql默认生成的密码

    grep 'temporary password' /var/log/mysqld.log

    2017-02-10T14:59:42.328736Z 1 [Note] A temporary password is generated for root@localhost: s/giN9Vo>L9h

    冒号后面的都是密码(没有空格) 复制就好

  7. 换成自己的密码

    mysql -u root -p

    Enter password: 输入上面复制的密码

  8. 更换密码

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

    这个密码一定要足够复杂,不然会不让你改,提示密码不合法

  9. 退出mysql

    quit;
  10. 用新密码再登录,试一下新密码

    mysql -uroot -p

    Enter password: 输入你的新密码

  11. 确认密码正确后,退出mysql
    quit;

编译安装php7.1.3

  1. 下载php7源码包

    cd /usr/src && wget -O php7.tar.gz http://cn2.php.net/get/php-7.1.3.tar.gz/from/this/mirror
  2. 解压源码包

    源码包的位置可以使用 find / -name php7 查找 并进入所在文件夹

    tar -xvf php7.tar.gz
  3. 打开解压好的包

    cd php-7.1.3
  4. 安装php依赖包

    yum install libxml2 libxml2-devel openssl openssl-devel bzip2 \
    bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng \
    libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt \
    libmcrypt-devel readline readline-devel libxslt libxslt-devel
  5. 编译配置

    ./configure --prefix=/usr/local/php --with-config-file-path=/etc \
    --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx \
    --enable-inline-optimization --disable-debug --disable-rpath \
    --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc \
    --with-openssl --with-mcrypt --with-mhash --with-pcre-regex \
    --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 \
    --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif \
    --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd \
    --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir \
    --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv \
    --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring \
    --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig \
    --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir \
    --with-pdo-sqlite --with-readline --enable-session --enable-shmop \
    --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem \
    --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip \
    --enable-mysqlnd-compression-support --with-pear --enable-opcache

    (这一步可能我们会遇到很多configure error,我们一一解决,基本都是相关软件开发包没有安装导致),如有有出现错误可查找一下。

    Configure error:

    • configure: error: xml2-config not found. Please check your libxml2 installation.

      $ yum install libxml2 libxml2-devel
    • configure: error: Cannot find OpenSSL's <evp.h>

      $ yum install openssl openssl-devel
    • configure: error: Please reinstall the BZip2 distribution

      $ yum install bzip2 bzip2-devel
    • configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

      $ yum install libcurl libcurl-devel
    • If configure fails try --with-webp-dir=<DIR>
      configure: error: jpeglib.h not found.

      $ yum install libjpeg libjpeg-devel
    • If configure fails try --with-webp-dir=<DIR>
      checking for jpeg_read_header in -ljpeg... yes
      configure: error: png.h not found.

      $ yum install libpng libpng-devel
    • If configure fails try --with-webp-dir=<DIR>
      checking for jpeg_read_header in -ljpeg... yes
      checking for png_write_image in -lpng... yes
      If configure fails try --with-xpm-dir=<DIR>
      configure: error: freetype-config not found.

      $ yum install freetype freetype-devel
    • configure: error: Unable to locate gmp.h

      $ yum install gmp gmp-devel
    • configure: error: mcrypt.h not found. Please reinstall libmcrypt.

      $ yum install libmcrypt libmcrypt-devel
    • configure: error: Please reinstall readline - I cannot find readline.h

      $ yum install readline readline-devel
    • configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
      $ yum install libxslt libxslt-devel
  6. 编译与安装

    make && make install

    这里要make好久,要耐心一下

  7. 添加 PHP 命令到环境变量

    vim /etc/profile

    在末尾加入

    PATH=$PATH:/usr/local/php/bin
    export PATH

    要使改动立即生效执行

    source /etc/profile

    查看环境变量

    echo $PATH

    查看php版本

    php -v
  8. 配置php-fpm

    cp php.ini-production /etc/php.ini
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
  9. 配置nginx

    vim /etc/nginx/conf.d/default.conf
    # file content
    server {
      listen       80 default;
      server_name  localhost;
      root         /home/inetpub/com.derzh.jx3;
    
      #charset koi8-r;
      #access_log  /var/log/nginx/log/host.access.log  main;
    
      location / {
          index  index.html index.htm index.php;
      }
    
      #error_page  404              /404.html;
    
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
    
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      location ~ \.php(/|$) {
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }
    
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
    }

    配置多站点

    vim /etc/nginx/conf.d/com.zhaiyiming.conf
    # file content
    server {
      listen       80;
      server_name  zhaiyiming.com;
      root         /home/inetpub/com.zhaiyiming;
    
      #charset koi8-r;
      #access_log  /var/log/nginx/log/host.access.log  main;
    
      location / {
          index  index.html index.htm index.php;
      }
    
      #error_page  404              /404.html;
    
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
    
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      location ~ \.php(\/|$) {
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }
    
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
    }

    重启nginx使配置生效

    service nginx restart
  10. 配置php.ini

    vim /etc/php.ini
    # find and change
    pdo_mysql.default_socket=/var/lib/mysql/mysql.sock
  11. 启动php-fpm
    /etc/init.d/php-fpm start

编译安装 php7-memcached

  1. 安装 libmemcached

    # Install dependencies
    yum install cyrus-sasl-devel zlib-devel gcc-c++
    
    # Get the latest libmemcached
    wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
    tar -xvf libmemcached-1.0.18.tar.gz
    cd libmemcached-1.0.18
    
    # Compile and install liblmemcached with sasl disabled
    ./configure --disable-memcached-sasl
    make
    make install
    
    # Restart any PHP related daemons
    service httpd restart
  2. 安装 php-memcached

    cd /usr/src && git clone https://github.com/php-memcached-dev/php-memcached.git
    cd php-memcached/ && git checkout php7
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/
    make && make install
  3. 修改 php.ini

    vi /etc/php.ini
    # 在module适合位置加上
    extension=/usr/src/php-memcached/modules/memcached.so
  4. 重启 php-fpm

    service php-fpm restart
  5. 访问 info.php 查看 memcached 开启情况

配置 opcache

  1. 编辑 php.ini 开启 opcache

    vi /etc/php.ini
    
    [opcache]
    zend_extension=opcache.so  # 新加此行
    opcache.enable=1           # 启用opcache
    opcache.enable_cli=1       # 在cli模式下启用opcache
    opcache.revalidate_freq=10 # 设置10s检查一次文件变化
  2. 重启 php-fpm
    service php-fpm restart

参考资料

https://www.zhihu.com/question/50615606/answer/145699091