準備安裝包(PHP: Hypertext Preprocessor)
下載安裝包以及組件
wget https://www.php.net/distributions/php-8.0.0.tar.bz2
wget https://github.com/phpredis/phpredis/archive/5.3.2.tar.gz -O phpredis-5.3.2.tar.gz
wget https://github.com/swoole/swoole-src/archive/v4.5.9.tar.gz -O swoole-src-4.5.9.tar.gz
wget http://pecl.php.net/get/mcrypt-1.0.3.tgz
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
wget https://github.com/Kitware/CMake/releases/download/v3.16.0/cmake-3.17.2.tar.gz
wget https://libzip.org/download/libzip-1.5.2.tar.xz
wget https://github.com/Kitware/CMake/releases/download/v3.19.4/cmake-3.19.4.tar.gz
分別解壓安裝
------- 安裝cmake -------
tar -zxvf cmake-3.17.2.tar.gz && cd cmake-3.17.2
./bootstrap && gmake && gmake install
cmake -version
------- 安裝libiconv -----
tar zxvf libiconv-1.16.tar.gz && cd libiconv-1.16 && ./configure --prefix=/usr/local/libiconv && make && make install
ln -s /usr/local/libiconv/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
------ 安裝libzip ------
tar xvJf libzip-1.5.2.tar.xz && cd libzip-1.5.2
mkdir build && cd build/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libzip ..
make && make install
------ 安裝PHP8.0 ------
./configure
--prefix=/usr/local/php8 --exec-prefix=/usr/local/php8 --bindir=/usr/local/php8/bin --sbindir=/usr/local/php8/sbin
--includedir=/usr/local/php8/include --libdir=/usr/local/php8/lib/php --mandir=/usr/local/php8/php/man
--with-config-file-path=/usr/local/php8/etc
--with-MySQL-sock=/dev/shm/mysql.sock --with-mysqli=shared,mysqlnd --with-mhash
--with-openssl --with-curl
--with-pdo-mysql=shared,mysqlnd --with-iconv --with-zlib
--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
--enable-opcache --enable-fpm --without-gdbm --disable-fileinfo --with-fpm-user=www --with-fpm-group=www
make && make install
復制配置文件
cp php.ini-production /usr/local/php8/etc/php.ini
cp sapi/fpm/php-fpm.service /lib/systemd/system/php-fpm.service
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
配置php-fpm.conf文件
pid = /run/php-fpm.pid
error_log = /var/log/php/fpm-error.log
include=/usr/local/php8/etc/php-fpm.d/*.conf
配置php-fpm.d/www.conf文件
######設置用戶和用戶組
user = www
group = www
######根據Nginx.conf中的配置fastcgi_pass unix:/dev/shm/php-fpm.sock;設置PHP監聽
;listen = 127.0.0.1:9000 #####不建議使用
listen = /dev/shm/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0777
listen.allowed_clients = /dev/shm/php-fpm.sock
######使用靜態進程數max_children=內存/512
pm = static
pm.max_children = 200
pm.max_requests = 0
pm.status_path = /FpmStatus
######開啟慢日志
slowlog = /var/log/php/fpm-$pool-slow.log
request_slowlog_timeout = 30s
request_slowlog_trace_depth = 20
request_terminate_timeout = 0
配置php.ini文件
######避免PHP信息暴露在http頭中
expose_php = Off
######常用配置
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
display_errors = Off
log_errors = On
html_errors = Off
error_log = /var/log/php/cli-error.log
ignore_repeated_errors = On
ignore_repeated_source = On
memory_limit = 512M
max_execution_time = 600 (0為不限制)
max_input_time = -1
post_max_size = 128M
max_input_vars = 1000000
file_uploads = On
upload_tmp_dir = /tmp/www
upload_max_filesize = 100M
session.gc_maxlifetime = 14400
default_socket_timeout = 600 (-1為不限制)
######設置PHP的擴展
extension_dir = "/usr/local/php8/lib/php/extensions/以安裝目錄為準"
zend_extension=opcache.so
extension=mcrypt.so
extension=mysqli.so
extension=pdo_mysql.so
extension=redis.so
extension=swoole.so
######設置PHP的時區
date.timezone = "Asia/Shanghai"
######開啟opcache
[opcache]
opcache.enable=1
opcache.enable_cli=1
#####開啟JIT
opcache.jit_buffer_size=128M
opcache.jit=1205
opcache.huge_code_pages=1
opcache.file_cache=/tmp
######設置PHP腳本允許訪問的目錄(需要根據實際情況配置)
;open_basedir = /usr/share/nginx/html;
----- 使用redis的session才需要配置 ----
session.save_handler = redis
session.save_path = "tcp://*****.aliyuncs.com:6379?
auth=******"
[MySQLi]
mysqli.reconnect = On
最后配置文件權限
mkdir -p /var/log/php/
mkdir -p /var/log/php-fpm/
mkdir -p /run/php-fpm/
mkdir -p /var/lib/php/session
touch /dev/shm/php-fpm.sock
touch /run/php-fpm.pid
chown www:www /dev/shm/php-fpm.sock
chmod 777 /dev/shm/php-fpm.sock
chown -R www:www /var/lib/php
######配置PHP連接MySQL的端口監聽文件(沒有安裝MySQL的可以先不監聽)
touch /dev/shm/mysql.sock
chmod 777 /dev/shm/mysql.sock
測試php-fpm配置,設置開機自啟動
######測試配置
/usr/local/php8/sbin/php-fpm -t
######修改文件權限
chmod 745 /lib/systemd/system/php-fpm.service
######設置為開機啟動
systemctl enable php-fpm.service
######啟動php-fpm
systemctl start php-fpm.service
把PHP加入環境變量
vi /etc/profile 底部加入
export PATH=$PATH:/usr/local/php8/bin
source /etc/profile