yboker 2019-06-29
安装的基本步骤官方文档都有说明:PHP官网安装文档
这一步实际上已经在:LNMP环境搭建(二):Nginx上完成了
[root@localhost ~]# yum -y install gcc [root@localhost ~]# yum -y install gcc++ [root@localhost ~]# yum -y install gcc-c++ [root@localhost ~]# yum -y install wget [root@localhost ~]# yum -y install make [root@localhost ~]# yum -y install libxml2 [root@localhost ~]# yum -y install libxml2-devel [root@localhost ~]# yum -y install openssl [root@localhost ~]# yum -y install openssl-devel [root@localhost ~]# yum -y install curl-devel [root@localhost ~]# yum -y install libjpeg-devel [root@localhost ~]# yum -y install libpng-devel [root@localhost ~]# yum -y install freetype-devel [root@localhost ~]# yum -y install bison [root@localhost ~]# yum -y install autoconf [root@localhost ~]# yum -y install automake [root@localhost ~]# yum -y install libtool [root@localhost ~]# yum -y install cmake [root@localhost ~]# yum -y install zlib [root@localhost ~]# yum -y install zlib-devel [root@localhost ~]# yum -y install pcre-devel
[root@localhost ~]# wget http://php.net/distributions/php-7.2.12.tar.gz [root@localhost ~]# tar -zxvf php-7.2.12.tar.gz
[root@localhost ~]# ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-mysql-sock=/tmp/mysql.sock --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-fast-install
--prefix=/usr/local/php [php安装的根目录] --exec-prefix=/usr/local/php [php执行文件所在目录] --bindir=/usr/local/php/bin [php/bin目录] --sbindir=/usr/local/php/sbin [php/sbin目录] --includedir=/usr/local/php/include [php包含文件所在目录] --libdir=/usr/local/php/lib/php [php/lib目录] --mandir=/usr/local/php/php/man [php/man目录] --with-config-file-path=/usr/local/php/etc [php的配置目录] --with-mysql-sock=/tmp/mysql.sock [php的Unix socket通信文件] --with-mhash [Mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启] --with-openssl [OpenSSL 是一个安全套接字层密码库] --with-mysqli=shared,mysqlnd [php依赖mysql库] --with-pdo-mysql=shared,mysqlnd [php依赖mysql库] --with-gd [gd库] --with-iconv [关闭iconv函数,种字符集间的转换] --with-zlib [zlib是提供数据压缩用的函式库] --enable-zip [打开对zip的支持] --enable-inline-optimization [优化线程] --disable-debug [关闭调试模式] --disable-rpath [关闭额外的运行库文件] --enable-shared [启用动态库] --enable-xml [开启xml扩展] --enable-bcmath [打开图片大小调整,用到zabbix监控的时候用到了这个模块] --enable-shmop [共享内存] --enable-sysvsem [内存共享方案] --enable-mbregex [开启多字节正则表达式的字符编码。] --enable-mbstring [开启多字节字符串函数] --enable-ftp [开启ftp] --enable-gd-native-ttf [开启gd库原有字体] --enable-pcntl [PHP的进程控制支持实现了Unix方式的多进程创建] --enable-sockets [开启套节字] --with-xmlrpc [打开xml-rpc的c语言] --enable-soap [开启简单对象访问协议简单对象访问协议] --without-pear [开启php扩展与应用库] --with-gettext [开户php在当前域中查找消息] --enable-session [允许php会话session] --with-curl [允许curl扩展] --with-jpeg-dir [指定jpeg安装目录yum安装过后不用再次指定会自动找到] --with-freetype-dir [指定freetype安装目录yum安装过后不用再次指定会自动找到] --enable-opcache [开启使用opcache缓存] --enable-fpm [开启fpm] --with-fpm-user=nginx [php-fpm的用户] --with-fpm-group=nginx [php-fpm的用户组] --without-gdbm [数据库函数使用可扩展散列和类似于标准UNIX dbm的工作] --enable-fast-install [为快速安装优化] --disable-fileinfo [防止低内存VPS出现内存不住的错误]
[root@localhost ~]# make && make install
> -r: 添加系统用户 > -g: 指定要创建的用户所属组 > -s: 新帐户的登录shell > -d: 新帐户的主目录 > -M: 不要创建用户的主目录
[root@localhost ~]# groupadd -r php [root@localhost ~]# useradd -r -g php -M php [root@localhost ~]# passwd php [root@localhost ~]# usermod -s /sbin/nologin php ###文件配置
> php.ini #php运行核心配置文件 > www.conf #php-fpm进程服务的扩展配置文件 > php-fpm.conf #php-fpm进程服务的配置文件
[root@localhost ~]# cp php.ini-development /usr/local/php/etc/php.ini [root@localhost ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@localhost ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf [root@localhost ~]# cp sapi/fpm/php-fpm /usr/local/bin
[root@localhost ~]# echo $PATH [root@localhost ~]# ln -s /usr/local/php/sbin/php-fpm /usr/local/sbin/php-fpm [root@localhost ~]# ln -s /usr/local/php/bin/php /usr/local/bin/php
在环境变量:/usr/local/bin/ 下创建 /usr/local/php/bin/php的软连接,当我们在任意目录输入 php时,系统去环境变量中查找,通过软连接指向 /usr/local/php/bin/php。php-fpm同理
[root@localhost ~]# ps -ef | grep php-fpm root 75608 1 0 02:32 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php- fpm.conf) nginx 75609 75608 0 02:32 ? 00:00:00 php-fpm: pool www nginx 75610 75608 0 02:32 ? 00:00:00 php-fpm: pool www root 75641 96978 0 02:32 pts/0 00:00:00 grep --color=auto php-fpm
配置 Nginx 使其支持 PHP 应用
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
修改默认的 location 块,使其支持 .php 文件:
location / { root html; index index.php index.html index.htm; }
下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并改为以下的内容:
location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
重启 Nginx
[root@localhost ~]# sudo /usr/local/nginx/sbin/nginx -s stop [root@localhost ~]# sudo /usr/local/nginx/sbin/nginx
创建测试文件
[root@localhost ~]# rm /usr/local/nginx/html/index.html [root@localhost ~]# echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
访问服务器的:localhost 可以看到 phpinfo
[root@localhost ~]# vim /usr/local/php/etc/php.ini extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/" date.timezone = Asia/Shanghai
查看 php-fpm 进程重启 php-fpm 查看更改
[root@localhost ~]# ps aux | grep php-fpm [root@localhost ~]# kill 22747 [root@localhost ~]# php-fpm
具体文法参考:Systemd 入门教程:实战篇
在 php-7.2.12.tar.gz 的解压目录下已有一个 service 文件了,复制并重命名到 /etc/systemd/system/ 下即可
[root@localhost ~]# find / -name "php-fpm.service" [root@localhost ~]# cp /home/soft/php-7.1.23/sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service
[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 PrivateTmp=true [Install] WantedBy=multi-user.target
启动 php,设置 php 自动启动
[root@centos7 /]# systemctl start php-fpm [root@centos7 /]# systemctl enable php-fpm
当启动失败时可以尝试终止进程再次启动
[root@centos7 /]# yum -y remove php*
[root@centos7 /]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm [root@centos7 /]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
等同于
[root@centos7 /]# yum install epel-release -y [root@centos7 /]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@centos7 /]# yum -y install php72w [root@centos7 /]# yum -y install php72w-cli [root@centos7 /]# yum -y install php72w-fpm [root@centos7 /]# yum -y install php72w-common [root@centos7 /]# yum -y install php72w-pdo [root@centos7 /]# yum -y install php72w-pdo_mysql [root@centos7 /]# yum -y install php72w-pdo_sqlite [root@centos7 /]# yum -y install php72w-devel
[root@centos7 /]# php -v # PHP 7.2.11 (cli) (built: Oct 11 2018 19:14:35) ( NTS ) # Copyright (c) 1997-2018 The PHP Group # Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies [root@centos7 /]# php -m # 查看安装的扩展
[root@centos7 /]# systemctl start php-fpm [root@centos7 /]# systemctl enable php-fpm
参考:编译安装PHP扩展
添加软连接,使 phpize 可以全局使用
[root@localhost ~]# echo $PATH [root@localhost ~]# ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
[root@localhost ~]# php -m
[PHP Modules] bcmath Core ctype curl date dom fileinfo filter ftp gd gettext hash iconv json libxml mbstring mysqlnd openssl pcntl pcre PDO pdo_sqlite Phar posix Reflection session shmop SimpleXML soap sockets SPL sqlite3 standard sysvsem tokenizer xml xmlreader xmlrpc xmlwriter zip zlib [Zend Modules]PECL 是PHP Extensions的存储库,提供了用于下载和开发PHP扩展的所有已知扩展和托管工具的目录。
[root@localhost ~]# wget http://pecl.php.net/get/redis-4.1.1.tgz [root@localhost ~]# tar -xvzf redis-4.1.1.tgz [root@localhost ~]# phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@localhost ~]# make && make install
前面在 php.ini 设置了拓展的安装路径,于是此处已自动将 redis.so 加入拓展目录中,只要在 php.ini,添加 extension = redis.so 即可
[root@localhost ~]# vim /usr/local/php/etc/php.ini extension = redis.so
[root@localhost ~]# php -m | grep redis
根据 PHP 版本选择对应的 Xdebug
[root@localhost ~]# wget http://pecl.php.net/get/xdebug-2.6.1.tgz [root@localhost ~]# tar -xvzf xdebug-2.6.1.tgz [root@localhost ~]# phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@localhost ~]# make && make install
[root@localhost ~]# vim /usr/local/php/etc/php.ini zend_extension = xdebug.so
[root@localhost ~]# php -m | grep xdebug
----------------------------------------------------------DONE------------------------------------------------------------
LNMP环境搭建(一):Mysql
LNMP环境搭建(二):Nginx
LNMP环境搭建(三):PHP
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh