PHP编译安装

  • Post author:
  • Post category:PHP
  • Page Views 225 阅读

自动安装脚本:>>>PHP自动编译安装脚本<<<

1.安装编译环境

yum install -y gcc gcc-c++ cmake cmake3

2.安装依赖库

[root@LNMP ~]# curl -o  /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo   #部分依赖库YUM仓库中没有
[root@LNMP ~]# yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel openssl-devel
[root@LNMP ~]# yum install -y freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
[root@LNMP ~]# yum install -y libmcrypt-devel mhash mcrypt
[root@LNMP ~]# yum install -y libicu-devel zlib-devel bzip2-devel glibc-devel glib2-devel ncurses-devel
[root@LNMP ~]# yum install -y krb5-devel libidn-devel openldap-devel nss_ldap jemalloc-devel
[root@LNMP ~]# yum install -y boost-devel libevent-devel readline-devel gmp-devel openjpeg-devel libsepol-devel libselinux-devel postgresql-devel
[root@LNMP ~]# yum install -y e2fsprogs-devel fontconfig-devel libaio-devel t1lib-devel libtiff-devel libvpx-devel libXpm-devel libcom_err-devel
[root@LNMP ~]# yum install -y libtool bison re2c flex php-common php-gd yasm nasm ntsysv bind-utils

3.编译安装libiconv

YUM仓库中没有

[root@LNMP ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
[root@LNMP libiconv-1.16]# tar xf libiconv-1.16.tar.gz
[root@LNMP libiconv-1.16]# cd libiconv-1.16
[root@LNMP libiconv-1.16]# ./configure --prefix=/usr/local/libiconv
[root@LNMP libiconv-1.16]# make
[root@LNMP libiconv-1.16]# make install

3.编译安装libzip

YUM仓库中libzip版本过低,编译安装会报错:system libzip must be upgraded to version >= 0.11,而yum安装的是0.10版本。

[root@LNMP ~]# yum remove libzip -y #一定要卸载旧版本,不然即使安装了新版本,还是会报错
[root@LNMP ~]# wget https://libzip.org/download/libzip-1.5.2.tar.gz
[root@LNMP ~]# cd libzip-1.5.2
[root@LNMP ~]# mkdir build #1.5+需要此方式编译
[root@LNMP ~]# cd build
[root@LNMP ~]# cmake3 ..
[root@LNMP ~]# make && make install

4.编译前准备

添加搜索路径到配置文件,否则编译会报错:configure: error: off_t undefined; check your library configuration

[root@LNMP ~]# echo '/usr/local/lib64  #每个路径单独一行,否则编译还是会报错。PHP7.2统一一行没问题。
/usr/local/lib
/usr/lib
/usr/lib64
/usr/local/libiconv/lib'>> /etc/ld.so.conf
[root@LNMP ~]# ldconfig -v

如果报错configure: error: Cannot find ldap libraries in /usr/lib

拷贝文件 cp -frp /usr/lib64/libldap* /usr/lib/

5.开始编译

./configure --prefix=/usr/local/php-7.3.5 \
--with-config-file-path=/usr/local/php/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir=/usr/local/libiconv \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli=mysqlnd \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-pear \
--with-jpeg-dir \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-zlib-dir= \
--with-bz2 \
--with-mhash \
--with-libmbfl \ #PHP7.3.5没有此参数
--with-ldap \
--enable-bcmath \
--enable-fpm \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-xml \
--enable-zip \
--disable-rpath \
--enable-mysqlnd \
--enable-sysvsem \
--enable-sysvshm \
--enable-opcache \
--enable-session \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-intl \
--enable-maintainer-zts \
--disable-fileinfo
[root@LNMP php-7.3.5]# make -j2 && make install

6.创建软链接与配置文件

[root@LNMP php-7.3.5]# ln -s /usr/local/php7.3.5 /usr/local/php
[root@LNMP etc]# cp php.ini-production /usr/local/php/etc/php.ini.bak
[root@LNMP etc]# cd /usr/local/php/etc/
[root@LNMP etc]# cp php-fpm.conf.2default php-fpm.conf #去掉数字2
[root@LNMP etc]# egrep -v '^$|;' php-fpm.d/www.conf.2default > php-fpm.d/www.conf #去掉空行与注释
[root@MySQL etc]# egrep -v '^$|;' php.ini.bak > php.ini

7.优化php.ini参数

[root@MySQL etc]# sed -i 's@expose_php = On@expose_php = Off@g' /usr/local/php/etc/php.ini #关闭版本信息
[root@MySQL etc]# sed -i 's@short_open_tag = Off@short_open_tag = ON@g' /usr/local/php/etc/php.ini #开启PHP段标签
###以下参数根据实际业务情况调整###
max_execution_time = 120 #单个脚本最大运行时间,单位时间秒
max_input_time = 120 #单个脚本等待输入的最长时间
memory_limit = 128M #单个脚本最大使用内存
post_max_size = 8M #POST 数据所允许的最大大小
max_file_uploads = 20 #单个请求上载的最大文件数
upload_max_filesize = 5M #上传文件最大许可

优化opcache参数

备注请勿拷贝

cat >> /usr/local/php/etc/php.ini <<EOF
[date]
date.timezone = Asia/Shanghai
[ldap]
extension = "ldap.so"
[opcache]
zend_extension= "/usr/local/php/lib/php/extensions/no-debug-zts-20180731/opcache.so"
opcache.enable=1 #Zend Optimizer + 的开关, 关闭时代码不再优化
opcache.interned_strings_buffer=8 #Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)
opcache.max_accelerated_files=4000 #最大缓存的文件数目
opcache.memory_consumption=128 #Zend Optimizer + 共享内存的大小, 总共能够存储多少预编译的 PHP 代码(单位:MB)
opcache.save_comments=1 #是否保存文件/函数的注释
opcache.revalidate_freq=2 #2s检查一次文件更新
EOF

创建php-cgi.sock存放目录

mkdir -p /usr/local/php/var/run
chown -R nginx:nginx /usr/local/php/var/run

8.优化www.conf

2核2G配置参数,具体可查看php-fpm,pm = dynamic使用方法。

一个php-fpm进程大约会占用20~30M的内存。

去掉数字2,一些字段与服务器组件冲突了。

cat >/usr/local/php/etc/php-fpm.2d/www.conf<<EOF
[www]
2user = nginx
2group = nginx
listen = /usr/local/php/var/run/php-cgi.sock
listen.2owner = nginx
listen.2group = nginx
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
;pm = static
listen.backlog = -1
pm.max_children = 30
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 16
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log
EOF

9.配置php-fpm.conf

sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@;error_log = log/php-fpm.log@error_log = /usr/local/php/var/log/php-fpm.log@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@;log_level = notice@log_level = notice@g' /usr/local/php/etc/php-fpm.conf

10.设置环境变量

echo 'export PATH=/usr/local/php/bin:$PATH' >> /etc/profile
source /etc/profile

11.配置system服务

cat > /usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target 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/kill2 -USR2 $MAINPID #去掉ki11后的数字2
[Install]
WantedBy=multi-user.target
EOF

12.启动服务

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


「 文章如果对你有帮助,请点个赞哦^^ 」 

0

这篇文章有一条评论

评论关闭。