公司要配置php的环境,还必须手动编译配置到指定目录下,参考了网上的教程记录一下我的配置过程

安装依赖

首先安装PHP依赖组件(这里默认Nginx装好了)

yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel

创建用户组

groupadd www
useradd -g www www

安装PHP

首先下载并且解压源码包

# 下载
wget https://mirrors.sohu.com/php/php-7.2.34.tar.gz
# 解压
tar -xvf php-7.2.34.tar.gz
cd php-7.2.34

接下来设置变量,其中--prefix=/www/php是指定安装PHP的目录,--with-config-file-path=/www/php/etc是指定配置文件的目录

./configure --prefix=/www/php \
--with-config-file-path=/www/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm \

准备开始编译源码

make && make install 
# 嫌慢的话可以用这个,可以提高编译速度
make -j 4 && make install

初始化配置文件

由于php安装后并没有默认好的配置文件,需要在完成安装后需要进行初始化配置文件

#复制源码包中配置文件到PHP配置文件目录中
cp php.ini-development /www/php/etc/php.ini
#复制两个默认的配置文件到PHP配置文件目录中
cp /www/php/etc/php-fpm.conf.default /www/php/etc/php-fpm.conf
cp /www/php/etc/php-fpm.d/www.conf.default /www/php/etc/php-fpm.d/www.conf

修改参数

修改 php.ini,根据需要自行调整

expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2

设置OPcache缓存(在php.ini添加下面内容),zend_extension的值需要根据实际生成的文件名称进行修改,大致路径为PHP安装目录下的 lib/php/extensions/no-debug-zts-xxxxxx/目录下,xxxxx是进行编译源码的日期

[opcache]
zend_extension=/www/php/lib/php/extensions/no-debug-zts-20210927/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

添加禁用函数列表(在php.ini中有,修改即可)

disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

然后继续配置www.conf,需要取消以下注释并修改

listen = /var/run/www/php-cgi.sock
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
listen.backlog = -1
pm.max_children = 180
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 180
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log

创建php-cgi.sock存放目录

mkdir /var/run/www/
chown -R www:www /var/run/www

配置php-fpm.conf

pid = /www/php/var/run/php-fpm.pid

创建和配置PHP服务

创建服务脚本
使用vi /usr/lib/systemd/system/php-fpm.service进行编辑,随后把下面的内容添加进来

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
 
[Service]
Type=simple
PIDFile=/www/php/var/run/php-fpm.pid
ExecStart=/www/php/sbin/php-fpm --nodaemonize --fpm-config /www/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
 
[Install]
WantedBy=multi-user.target

启动php-fpm服务并加入开机自启动

systemctl enable php-fpm.service
systemctl restart php-fpm.service

后续你就可以使用下面的命令来进行管理了

service php-fpm start/stop/restart
最后修改:2021 年 09 月 29 日 09 : 23 AM
如果觉得我的文章对你有用,请随意赞赏