作者归档:魏 文成

CentOS编译安装PHP的Memcached扩展

安装 Memcached

下载 wget http://www.memcached.org/files/memcached-1.6.6.tar.gz

解压缩包 tar zxf memcached-1.6.6.tar.gz

进入源码目录 cd memcached-1.6.6

编译安装 ./configure --prefix=/usr/local/memcached && make && make install

安装 libmemcached

下载 wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

解压缩包 tar zxf libmemcached-1.0.18.tar.gz

进入源码目录 cd libmemcached-1.0.18

编译安装 ./configure --prefix=/usr/local/libmemcached && make && make install

生成并安装扩展

下载 wget https://github.com/php-memcached-dev/php-memcached/archive/master.zip

解压缩包 unzip master.zip

进入源码目录 cd php-memcached-master

准备编译环境 /usr/local/php/bin/phpize

生成扩展 ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached && make && make install

安装扩展 vi /usr/local/php/lib/php.ini 添加 extension=memcached.so

使用Memcached加速WordPress

下载 https://github.com/tollmanz/wordpress-pecl-memcached-object-cache/archive/master.zip

解压后把 object-cache.php 放到 wp-content 目录内即可。

查看Memcached的使用情况

下载 http://pecl.php.net/get/memcache-3.0.8.tgz 编辑里面的 memcache.php 根据提示设置好用户名和密码,以及Memcached的相关信息,上传到服务器访问,即可查看。

安装过程中可能出现的报错以及解决办法

configure: error: libevent is required.  You can get it from https://www.monkey.org/~provos/libevent/
If it's already installed, specify its path using --with-libevent=/dir/

解决办法 yum install libevent-devel

./libtool: line 1125: g++: command not found
make[1]: *** [Makefile:5858: libhashkit/libhashkit_libhashkit_la-aes.lo] Error 1
make[1]: Leaving directory '/root/memcached-1.6.6/libmemcached-1.0.18'
make: *** [Makefile:3700: all] Error 2

解决办法 yum install gcc-c++

clients/memflush.cc:42:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   if (opt_servers == false)
                      ^~~~~
clients/memflush.cc:51:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (opt_servers == false)
                        ^~~~~
make[1]: *** [Makefile:5832: clients/memflush.o] Error 1
make[1]: Leaving directory '/root/memcached-1.6.6/libmemcached-1.0.18'
make: *** [Makefile:3700: all] Error 2

解决办法 vi clients/memflush.cc 把第42行和第51行的 if (opt_servers == false) 更改为 if (!opt_servers)

configure: error: no, libmemcached built with sasl disabled. Run configure with --disable-memcached-sasl or update libmemcached with sasl support

解决办法 yum install cyrus-sasl-devel

CentOS编译安装PHP的imagick扩展

发现WordPress后台的站点健康工具提示:缺少一个或多个推荐的模组 可选的模组imagick未被安装或已被禁用

作为一名完美主义者,这个实在不能忍,于是马上着手解决:

下载ImageMagick:wget https://imagemagick.org/download/ImageMagick-6.9.11-14.tar.gz

解压缩包:tar zxf ImageMagick-6.9.11-14.tar.gz

进入源码目录: cd ImageMagick-6.9.11-14

编译安装:./configure --prefix=/usr/local/ImageMagick && make && make install

下载imagick:wget http://pecl.php.net/get/imagick-3.4.4.tgz

解压缩包:tar zxf imagick-3.4.4.tgz

进入源码目录:cd imagick-3.4.4

准备编译环境:/usr/local/php/bin/phpize

编译安装:./configure --with-imagick=/usr/local/ImageMagick --with-php-config=/usr/local/php/bin/php-config && make && make install

vi /usr/local/php/lib/php.ini 添加 extension=imagick.so

安装过程中可能出现的报错以及解决办法

config.status: error: in `/root/ImageMagick-6.9.11-14':
config.status: error: Something went wrong bootstrapping makefile fragments
    for automatic dependency tracking.  Try re-running configure with the
    '--disable-dependency-tracking' option to at least be able to build
    the package (albeit without support for automatic dependency tracking).
See `config.log' for more details

解决办法 yum install make

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决办法 yum install autoconf

使NGINX支持Brotli压缩算法

克隆Brotli库到本地

git clone https://github.com/google/ngx_brotli
cd ngx_brotli
git submodule update --init

编译安装NGINX时添加下面的编译参数:

--add-module=/root/ngx_brotli

编辑NGINX配置文件,在http段内添加以下内容:

brotli on;
brotli_types text/css application/javascript image/jpeg image/gif image/png;

NGINX配置HTTPS和HTTP/2以及IPv6

安装NGINX的时候,添加下面两项编译参数:

--with-http_ssl_module
--with-http_v2_module

编辑配置文件 vi /usr/local/nginx/conf/nginx.conf 填写以下内容:

server {
        listen       443 ssl http2;          #监听IPv4的443端口并启用HTTPS和HTTP/2
        listen       [::]:443 ssl http2;     #监听IPv6的443端口并启用HTTPS和HTTP/2
        server_name  你的域名;               #绑定的域名
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";    #开启HSTS

        ssl_certificate      你的证书路径;          #证书位置
        ssl_certificate_key  你的私钥路径;          #私钥位置
        ssl_session_cache    shared:SSL:10m;        #会话缓存的类型和大小
        ssl_session_timeout  10m;                   #会话缓存的过期时间
        ssl_early_data       on;                    #开启TLS1.3的0-RTT
        ssl_protocols        TLSv1.2 TLSv1.3;       #指定支持的加密协议
        ssl_prefer_server_ciphers  on;              #优先使用服务器加密套件
        ssl_stapling         on;                    #开启 OCSP Stapling
        resolver         8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=300s;    #查询OCSP的DNS服务器和查询缓存时间
        resolver_timeout 5s;                        #DNS查询超时时间
        ssl_stapling_verify  on;                    #开启 OCSP Stapling 验证
    }

设置HTTP请求301跳转到HTTPS

server {
        listen      80;                #监听IPv4的80端口
        listen      [::]:80;           #监听IPv6的80端口
        server_name 你的域名;          #绑定的域名
        return 301 https://你的域名;   #跳转类型和跳转目标
    }

设置DNS CAA记录

以 Let’s Encrypt 证书为例,CAA记录填写为 0 issue "letsencrypt.org"

另外,使用 Let’s Encrypt 证书时,NGINX可能会报错: "ssl_stapling" ignored, host not found in OCSP responder "ocsp.int-x3.letsencrypt.org" in the certificate "你的证书路径"

这是由于不可抗力导致 Let’s Encrypt 的 OCSP 服务器无法访问造成的,可以尝试编辑Hosts:vi /etc/hosts 添加 23.205.154.146 ocsp.int-x3.letsencrypt.org

更改TLS1.3的加密套件

TLS1.3的加密套件无法在NGINX中直接指定,但是我们可以通过修改系统配置文件的方法来设定 vi /etc/crypto-policies/back-ends/opensslcnf.configCiphersuites 中列出了可选的加密套件,假如我们想使用 TLS_AES_128_GCM_SHA256 就更改为 Ciphersuites = TLS_AES_128_GCM_SHA256

使NGINX支持PHP

首先配置PHP

安装PHP之后,执行以下操作:

cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf
cp /root/php-7.4.6/php.ini-production /usr/local/php/lib/php.ini

开启OPcache vi /usr/local/php/lib/php.ini 添加 zend_extension=opcache.so

vi /usr/local/php/etc/php-fpm.d/www.conflisten = 127.0.0.1:9000 更改为 listen = /usr/local/php/php-fpm.sock 再把下面三行前面的分号 ; 去掉

;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660

启动PHP-FPM /usr/local/php/sbin/php-fpm

然后配置NGINX

安装NGINX之后,执行以下操作:

vi /usr/local/nginx/conf/nginx.conf 在server段内添加以下内容:

location ~ \.php$ {
            root           html;
            fastcgi_pass   unix:/usr/local/php/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  HTTPS            $https;
            include        fastcgi_params;
        }

启动NGINX /usr/local/nginx/sbin/nginx

设置PHP-FPM和NGINX开机自动启动 vi /etc/rc.d/rc.local 添加 /usr/local/php/sbin/php-fpm/usr/local/nginx/sbin/nginx 然后授权 chmod +x /etc/rc.d/rc.local

CentOS编译安装NGINX

下载 wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压缩包 tar zxf nginx-1.18.0.tar.gz

进入源码目录 cd nginx-1.18.0

编译安装 ./configure --prefix=/usr/local/nginx --user=nobody --group=nobody && make && make install

安装过程中可能出现的报错以及解决办法

./configure: error: C compiler cc is not found

解决办法 yum install gcc

./configure: error: the HTTP rewrite module requires the PCRE library.

解决办法 yum install pcre-devel

./configure: error: SSL modules require the OpenSSL library.

解决办法 yum install openssl-devel

./configure: error: the HTTP gzip module requires the zlib library.

解决办法 yum install zlib-devel

CentOS编译安装PHP

下载 wget https://www.php.net/distributions/php-7.4.6.tar.gz

解压缩包 tar zxf php-7.4.6.tar.gz

进入源码目录 cd php-7.4.6

编译安装 ./configure --prefix=/usr/local/php --with-mysql-sock=/var/lib/mysql/mysql.sock --enable-fpm --with-fpm-user=nobody --with-fpm-group=nobody --with-openssl --with-zlib --with-curl --enable-mbstring --with-mysqli --with-jpeg --with-webp --with-freetype --enable-gd --with-iconv-dir --enable-exif --with-zip && make && make install

安装过程中可能出现的报错以及解决办法

configure: error: in `/root/php-7.4.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

解决办法 yum install gcc

configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
Package 'libxml-2.0', required by 'virtual:world', not found

解决办法 yum install libxml2-devel

configure: error: Package requirements (openssl >= 1.0.1) were not met:
Package 'openssl', required by 'virtual:world', not found

解决办法 yum install openssl-devel

configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
Package 'sqlite3', required by 'virtual:world', not found

解决办法 yum install sqlite-devel

configure: error: Package requirements (libcurl >= 7.15.5) were not met:
Package 'libcurl', required by 'virtual:world', not found

解决办法 yum install curl-devel

configure: error: Package requirements (libpng) were not met:
Package 'libpng', required by 'virtual:world', not found

解决办法 yum install libpng-devel

configure: error: Package requirements (libwebp) were not met:
Package 'libwebp', required by 'virtual:world', not found

解决办法 yum install libwebp-devel

configure: error: Package requirements (libjpeg) were not met:
Package 'libjpeg', required by 'virtual:world', not found

解决办法 yum install libjpeg-devel

configure: error: Package requirements (freetype2) were not met:
Package 'freetype2', required by 'virtual:world', not found

解决办法 yum install freetype-devel

configure: error: Package requirements (oniguruma) were not met:
Package 'oniguruma', required by 'virtual:world', not found

解决办法

yum install oniguruma
wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-1.el8.x86_64.rpm
rpm -ivh oniguruma-devel-6.8.2-1.el8.x86_64.rpm

configure: error: Package requirements (libzip >= 0.11) were not met:
Package 'libzip', required by 'virtual:world', not found

解决办法 yum install libzip-devel

cc: fatal error: Killed signal terminated program cc1
compilation terminated.
make: *** [Makefile:690: ext/date/lib/parse_date.lo] Error 1

解决办法

1.增加服务器内存;
2.使用虚拟内存(SWAP分区):
fallocate -l 1024M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap sw 0 0" >> /etc/fstab

CentOS更改MySQL端口

编辑配置文件 vi /etc/my.cnf 添加一行 port=你想使用的端口号

更改后启动报错:

Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
错误日志显示:
[ERROR] [MY-010262] [Server] Can't start server: Bind on TCP/IP port: Permission denied
[ERROR] [MY-010257] [Server] Do you already have another mysqld server running on port: 你指定的端口号 ?
[ERROR] [MY-010119] [Server] Aborting

这是SELinux导致的,解决办法有两个:

1.关闭SELinux vi /etc/sysconfig/selinuxSELINUX=enforcing 更改为 SELINUX=disabled
2.添加SELinux端口授权 semanage port -a -t mysqld_port_t -p tcp 你指定的端口号

添加SELinux端口授权时可能会报错 -bash: semanage: command not found 解决办法 yum install policycoreutils-python-utils

CentOS使用RPM安装MySQL

下载 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-1.el8.x86_64.rpm-bundle.tar

解压缩包 tar xf mysql-8.0.20-1.el8.x86_64.rpm-bundle.tar

安装

rpm -ivh mysql-community-common-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.20-1.el8.x86_64.rpm

初始化 (root密码为空) mysqld --initialize-insecure

授权 chown mysql:mysql /var/lib/mysql -R

启动 systemctl start mysqld.service

安装过程中可能出现的报错以及解决办法

error: Failed dependencies:
/usr/bin/perl is needed by mysql-community-server-8.0.20-1.el8.x86_64
perl(Getopt::Long) is needed by mysql-community-server-8.0.20-1.el8.x86_64
perl(strict) is needed by mysql-community-server-8.0.20-1.el8.x86_64

解决办法:yum install perl

error: Failed dependencies:
net-tools is needed by mysql-community-server-8.0.20-1.el8.x86_64

解决办法:yum install net-tools

error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64

解决办法:yum install libaio

error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.20-1.el7.x86_64

解决办法:

rpm -q mariadb-libs
rpm -e 上条命令的输出结果 --nodeps

Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
错误日志显示:
[ERROR] [MY-012681] [InnoDB] mmap(137428992 bytes) failed; errno 12
[ERROR] [MY-012956] [InnoDB] Cannot allocate memory for the buffer pool
[ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Generic error.
[ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
[ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
[ERROR] [MY-010119] [Server] Aborting

解决办法:

1.增加服务器内存;
2.使用虚拟内存(SWAP分区):
fallocate -l 1024M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap sw 0 0" >> /etc/fstab