CentOS 7下PHP 5.4环境的配置与优化指南
在当今的Web开发领域,PHP仍然是一个广泛使用的编程语言。尽管PHP的版本不断更新,但某些老旧项目可能仍然依赖于PHP 5.4版本。本文将详细讲解如何在CentOS 7系统上配置和优化PHP 5.4环境,确保你的项目能够稳定运行。
一、准备工作
更新系统 在开始安装之前,确保系统是最新的:
sudo yum update -y
安装必要的依赖 安装编译PHP所需的依赖包:
sudo yum install -y gcc make autoconf libtool automake sudo yum install -y bzip2 bzip2-devel curl curl-devel sudo yum install -y libjpeg libjpeg-devel libpng libpng-devel sudo yum install -y freetype freetype-devel gmp gmp-devel sudo yum install -y libmcrypt libmcrypt-devel mhash mhash-devel sudo yum install -y openssl openssl-devel mcrypt mcrypt-devel sudo yum install -y libxml2 libxml2-devel sqlite sqlite-devel
二、下载并安装PHP 5.4
下载PHP 5.4源码 从PHP官方镜像下载PHP 5.4的源码包:
wget http://php.net/get/php-5.4.45.tar.gz/from/this/mirror tar -zxvf php-5.4.45.tar.gz cd php-5.4.45
配置编译选项 运行以下命令配置PHP的编译选项:
./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-gd \ --with-iconv \ --with-zlib \ --with-curl \ --with-openssl \ --with-mcrypt \ --with-xmlrpc \ --with-xsl \ --with-gettext \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-mhash \ --enable-fpm \ --enable-mbstring \ --enable-sockets \ --enable-zip \ --enable-calendar \ --enable-bcmath \ --enable-gd-native-ttf \ --enable-exif \ --enable-pcntl \ --enable-ftp \ --enableshmop \ --enable-sysvsem \ --enable-sysvshm \ --enable-sysvmsg \ --enable-soap \ --enable-opcache
编译和安装
make sudo make install
三、配置PHP
复制php.ini文件 将源码包中的
php.ini-production
文件复制到安装目录:sudo cp php.ini-production /usr/local/php/etc/php.ini
配置PHP-FPM 复制PHP-FPM的配置文件:
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm.conf
文件,确保以下配置:
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/php-cgi.sock
listen.owner = apache
listen.group = apache
listen.mode = 0666
user = apache
group = apache
- 启动PHP-FPM
创建PHP-FPM的启动脚本:
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm sudo chmod +x /etc/init.d/php-fpm sudo chkconfig --add php-fpm sudo service php-fpm start
四、配置Nginx支持PHP
安装Nginx 如果尚未安装Nginx,可以通过以下命令安装:
sudo yum install -y nginx sudo systemctl start nginx sudo systemctl enable nginx
配置Nginx 编辑Nginx的配置文件
/etc/nginx/nginx.conf
,添加以下配置:server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
重启Nginx
sudo systemctl restart nginx
五、优化PHP环境
优化php.ini 编辑
/usr/local/php/etc/php.ini
文件,进行以下优化:max_execution_time = 300 memory_limit = 256M upload_max_filesize = 64M post_max_size = 64M
优化PHP-FPM 编辑
/usr/local/php/etc/php-fpm.conf
文件,进行以下优化:pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 10 pm.max_spare_servers = 30
重启PHP-FPM
sudo service php-fpm restart
六、测试PHP环境
创建一个测试PHP文件/usr/share/nginx/html/info.php
:
<?php
phpinfo();
?>