1、下载php安装包
# php下载地址
https://www.php.net/downloads.php
2、安装php
useradd -M -s /sbin/nologin www
yum install -y gcc libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libXpm-devel freetype-devel libmcrypt-devel make autoconf ImageMagick-devel libssh2-devel gcc-c++ cyrus-sasl-devel sqlite-devel oniguruma-devel openldap-devel sqlite-devel libcurl-devel
tar -xf php-8.3.10.tar.gz
cd php-8.3.10
CPPFLAGS="-I/usr/include" \
LDFLAGS="-L/usr/lib64 -L/usr/lib64 -lldap -llber" \
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--disable-ipv6 \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-gd \
--with-external-gd \
--with-jpeg \
--with-freetype \
--with-xpm \
--with-webp \
--enable-mbregex \
--enable-mbstring \
--enable-mysqlnd \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-static \
--enable-sysvsem \
--enable-xml \
--with-curl \
--with-gettext \
--with-iconv \
--with-mhash \
--with-mysqli \
--with-pdo-mysql \
--with-pear \
--with-openssl \
--with-zlib \
--with-ldap \
--with-ldap-sasl \
--disable-debug \
--disable-phpdbg
make -j $(nproc)
make install
3、配置PHP
cat >> /etc/profile << EOF
#### php ####
export PATH=$PATH:/usr/local/php/bin
EOF
source /etc/profile
cp /root/php-8.3.10/php.ini-production /usr/local/php/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 /usr/local/php/sbin/php-fpm /usr/local/bin/
cp /usr/local/php/bin/php /usr/local/bin/
4、编译遇到的问题
configure: error: Cannot find ldap libraries in /usr/lib.
ls /usr/lib64 | grep libldap*
ln -s /usr/lib64/libldap* /usr/lib/
5、配置启动文件
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
6、验证PHP
mkdir -p /var/www/html/
echo "<?php phpinfo();?>" > /var/www/html/index.php
server {
listen 8001;
server_name _;
root /var/www/html/;
index index.html index.htm index.php;
location ~ [^/]\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
index index.php;
fastcgi_index index.php;
include fastcgi.conf;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/4fd7b244f45a46cbbfbfdf7cba447ad5.png)
7、install.sh
#!/bin/bash
install_php () {
user=www
echo "##### 检测网络 #####"
if ! ping -c1 -W1 www.baidu.com &> /dev/null
then
echo "网络不可以用, 请检查网络"
exit 1
fi
echo "##### 创建php用户 #####"
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "$user 已存在"
else
useradd -M -s /sbin/nologin $user &> /dev/null
echo "$user 创建成功"
fi
echo "##### 安装依赖 #####"
packages=(gcc libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libXpm-devel freetype-devel libmcrypt-devel make autoconf ImageMagick-devel libssh2-devel gcc-c++ cyrus-sasl-devel sqlite-devel oniguruma-devel openldap-devel sqlite-devel libcurl-devel)
for pkg in $(echo ${packages[*]})
do
yum install -y ${pkg} >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "error:install "$pkg" error,try again."
exit 2
fi
done
echo "##### 下载php #####"
if [ $? -ne 0 ]
then
echo -e "\033[31m 下载 'php' 失败\033[0m"
exit 3
fi
tar -xf php-8.3.10.tar.gz
if [ $? -ne 0 ]
then
echo -e "\033[31m error: tar xvf 'php' failed\033[0m"
exit 4
fi
echo "##### 安装php #####"
cd php-8.3.10
if [ $? -ne 0 ]
then
echo -e "\033[31m error:cd 'php' failed\033[0m"
exit 5
fi
CPPFLAGS="-I/usr/include" \
LDFLAGS="-L/usr/lib64 -L/usr/lib64 -lldap -llber" \
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--disable-ipv6 \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-gd \
--with-external-gd \
--with-jpeg \
--with-freetype \
--with-xpm \
--with-webp \
--enable-mbregex \
--enable-mbstring \
--enable-mysqlnd \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-static \
--enable-sysvsem \
--enable-xml \
--with-curl \
--with-gettext \
--with-iconv \
--with-mhash \
--with-mysqli \
--with-pdo-mysql \
--with-pear \
--with-openssl \
--with-zlib \
--with-ldap \
--with-ldap-sasl \
--disable-debug \
--disable-phpdbg >> /dev/null 2>> stderr.log
if [ $? -ne 0 ]
then
echo -e "\033[31m error:./configure 'php' failed\033[0m"
exit 6
fi
make -j $(nproc) >> /dev/null 2>> stderr.log
if [ $? -ne 0 ]
then
echo -e "\033[31m make 'php' failed\033[0m"
exit 7
fi
make install >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -e "\033[31m error:make install 'php' failed\033[0m"
exit 8
fi
echo "##### 配置环境变量 #####"
cat >> /etc/profile << EOF
#### php ####
export PATH=$PATH:/usr/local/php/bin
EOF
source /etc/profile >/dev/null 2>&1
cp /root/php-8.3.10/php.ini-production /usr/local/php/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 /usr/local/php/sbin/php-fpm /usr/local/bin/
cp /usr/local/php/bin/php /usr/local/bin/
}
install_php