本文共 3540 字,大约阅读时间需要 11 分钟。
PHP 是一款广泛应用于 Web 开发领域的开源脚本语言,语法灵活,学习成本低,是现代 Web 开发的核心技术之一。
在安装 PHP 之前,首先需要安装一些必要的系统依赖包:
# 安装 gcc 及相关开发包yum -y install gcc gcc-c++ make
将 PHP 源码下载到标准目录并解压:
# 下载并解压cd /usr/local/srcwget https://ftp.php.net/apache/phips/php最新版本tar -xzf php-消费者版本
在 /usr/local/src/php-消费者版本
目录下执行以下命令:
./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-pdo-mysql=/usr/local/mysql/bin/mysql_config \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-mysql-sock=/tmp/mysql.sock \ --with-pdo-mysql=/usr/local/mysql \ --with-gd \ --with-png-dir=/usr/local/libpng \ --with-jpeg-dir=/usr/local/jpeg \ --with-freetype-dir=/usr/local/freetype \ --with-xpm-dir=/usr/local/xpm \ --with-zlib-dir=/usr/local/zlib \ --with-iconv \ --enable-libxml \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --enable-opcache \ --enable-mbstring \ --enable-ftp \ --enable-gd-native-ttf \ --with-openssl \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-mcrypt \ --with-curl \ --enable-ctype
makemake install
将 PHP 的配置文件和模板文件复制到安装目录,并创建符号链接:
# 复制 php.ini 文件cp php.ini-production /usr/local/php/etc/php.ini# 删除系统默认配置rm -rf /etc/php.ini# 创建符号链接ln -s /usr/local/php/etc/php.ini /etc/php.ini# 复制 php-fpm 配置文件并设置符号链接cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.confln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
编辑 PHP 配置文件 /usr/local/php/etc/php.ini
,进行以下修改:
# 禁用危险函数disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,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
date.timezone = PRC
expose_php = Off
short_open_tag = On
opcache.enable=1opcache.enable_cli=0
在配置末尾添加:
zend_extension=opcache.so
复制 init script 并赋予执行权限:
cp /usr/local/src/php-消费者版本/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod 755 /etc/init.d/php-fpm
设置开机启动:
chkconfig --levelpad 20 php-fpm:0:off 1:off 2:on 3:on 4:on 5:on 6:off
编辑 PHP 配置文件 /usr/local/php/etc/php-fpm.conf
,确保以下内容存在:
pid = run/php-fpm.piduser = wwwgroup = www# 填写你的域名或IP地址listen=127.0.0.1:9000
# 查看用户组cat /etc/passwd | grep wwwcat /etc/group | grep www
vim /usr/local/nginx/conf/nginx.conf
添加以下内容:
user www www;location / { root html; index index.html index.php;}location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;}
/etc/init.d/nginx start/etc/init.d/php-fpm start
进入默认网站根目录创建测试文件:
cd /usr/local/nginx/htmlrm -rf *vim index.php
设置目录权限:
chown -R www.www /usr/local/nginx/htmlchmod -R 755 /usr/local/nginx/html
访问服务器 IP 地址即可查看测试页面。
转载地址:http://vhwfk.baihongyu.com/