【1】安装需要的依赖
① 安装依赖如下
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel libtool glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
如果使用yum安装依赖的时候提示如下:
Existing lock /var/run/yum.pid: another copy is running as pid 10292. Another app is currently holding the yum lock; waiting for it to exit..
yum只能支持一个例程运行,所以如果有一个例程已经在运行,其他的必须等待该进程退出释放lock。出现这种情况时,可以用以下命令来恢复:
rm -f /var/run/yum.pid
② 换yum源切换为阿里云源(非必须)
还可能遇到问题类似如下:
No package gcc-c++ available. No package gcc-g77 available. No package flex available. No package bison available. No package libtool available. No package libtool-libs available. No package libjpeg available.
此时建议更换yum源为阿里源,过程如下:
备份本机软件源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 5 get -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo 或者 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo CentOS 6 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 或者 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo CentOS 7 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 或者 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- 运行yum makecache生成缓存
yum makecache
依赖安装完毕如下:
【2】编译源码进行安装
① 进入/usr/local/src目录,解压tar.gz
源码文件放在了/usr/local/src目录下。
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# ll total 17852 -rwxr-xr-x. 1 root root 18277406 Jun 27 11:48 php-7.2.0.tar.gz [root@localhost src]# tar -xvf php-7.2.0.tar.gz
② 进入解压后的目录,进行configure
[root@localhost src]# ll total 17856 drwxrwxr-x. 14 root root 4096 Nov 28 2017 php-7.2.0 -rwxr-xr-x. 1 root root 18277406 Jun 27 11:48 php-7.2.0.tar.gz [root@localhost src]# cd php-7.2.0 [root@localhost php-7.2.0]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --enable-sockets --with-freetype-dir=/usr --with-zlib --with-libxml-dir=/usr --with-xmlrpc --enable-zip --enable-fpm --enable-xml --enable-sockets --with-gd --with-zlib --with-iconv --enable-zip --with-freetype-dir=/usr/lib/ --enable-soap --enable-pcntl --enable-cli --with-curl
configure后如下:
此时可以看到sapi路径下生成了一些conf文件,下面配置PHP时会使用。另外,最后一行有一个警告,configure: WARNING: unrecognized options: --with-mysql说明PHP7.2已经不支持这种方式,去掉即可。php7移除了 mysql 扩展,php7仅能是用 mysqli 和 pdo_mysql(php5可以使用)。
③ make&make install
在/usr/local/src/php-7.2.0下执行编译安装命令:
make&make install
如果按照过程中出现缺少依赖等错误,使用yum界面即可:
解决完错误后重新make & make install前建议执行一下make clean。
【3】配置PHP
在之前编译的源码包中,找到 php.ini-production,复制到/usr/local/php下,并改名为php.ini:
//查看当前路径 [root@localhost php-7.2.0]# pwd /usr/local/src/php-7.2.0 [root@localhost php-7.2.0]# cp php.ini-production /usr/local/php/php.ini
复制启动脚本:
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm
设置php-fpm开机启动
[root@localhost init.d]# chkconfig php-fpm on [root@localhost init.d]# chkconfig --list php-fpm php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改php-fpm配置文件:
cd /usr/local/php/etc cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
- 去掉 pid = run/php-fpm.pid 前面的分号
- 去掉error_log = log/php-fpm.log前的分号
- 去掉daemonize = yes前面的分号
cd php-fpm.d cp www.conf.default www.conf
vim www.conf
- 修改user和group的用户为当前用户(也可以不改,默认会添加nobody这个用户和用户组)
[root@localhost init.d]# ps -ef|grep php root 2988 1 0 00:38 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 2989 2988 0 00:38 ? 00:00:00 php-fpm: pool www nobody 2990 2988 0 00:38 ? 00:00:00 php-fpm: pool www
添加软链接
[root@localhost html]# ln -s /usr/local/php/php.ini /etc/php.ini [root@localhost html]# ls -l /etc/php.ini lrwxrwxrwx. 1 root root 22 Jul 1 02:18 /etc/php.ini -> /usr/local/php/php.ini [root@localhost init.d]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf [root@localhost init.d]# ls -l /etc/php-fpm.conf lrwxrwxrwx. 1 root root 31 Jul 1 00:46 /etc/php-fpm.conf -> /usr/local/php/etc/php-fpm.conf //php 命令软连接 [root@localhost init.d]# ln -s /usr/local/php/bin/php /usr/local/bin/php [root@localhost init.d]# ls -l /usr/local/bin/php lrwxrwxrwx. 1 root root 22 Jul 1 00:50 /usr/local/bin/php -> /usr/local/php/bin/php [root@localhost init.d]# php -v PHP 7.2.0 (cli) (built: Jun 28 2019 02:49:43) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies //配置php-fpm软连接 [root@localhost init.d]# ln -s /etc/init.d/php-fpm /usr/local/bin/php-fpm [root@localhost init.d]# ls -l /usr/local/bin/php-fpm lrwxrwxrwx. 1 root root 19 Jul 1 00:54 /usr/local/bin/php-fpm -> /etc/init.d/php-fpm [root@localhost init.d]# php-fpm status php-fpm (pid 2988) is running...
PHP启动/停止
/usr/local/php/sbin/php-fpm #启动php-fpm /etc/init.d/php-fpm start #php-fpm启动命令 /etc/init.d/php-fpm stop #php-fpm停止命令 /etc/init.d/php-fpm restart #php-fpm重启命令 ps -ef | grep php 或者 ps -A | grep -i php #查看是否已经成功启动PHP service php-fpm status #查看PHP运行状态 service php-fpm stop #停止PHP服务 service php-fpm start #查看启动PHP服务
实例:
[root@localhost init.d]# service php-fpm status php-fpm (pid 2988) is running... [root@localhost init.d]# service php-fpm stop Gracefully shutting down php-fpm . done [root@localhost init.d]# service php-fpm status php-fpm is stopped [root@localhost init.d]# service php-fpm start Starting php-fpm done [root@localhost init.d]# service php-fpm status php-fpm (pid 3404) is running...
【4】安装NGINX测试PHP服务
修改NGINX配置:
user nobody nobody; worker_processes 1; error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile 1024; events { use epoll; worker_connections 100; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径 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; } } }
编写index.php并放到/usr/local/nginx/html下
<?php phpinfo(); ?>
为目录赋予权限
chmod 755 /usr/local/nginx/html/ -R
重启PHP与NGINX
[root@localhost html]# php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [root@localhost html]# service nginx restart Stopping nginx: Starting nginx:
访问http://192.168.18.128/index.php如下:
【5】部署PHP项目
① 修改NGINX配置文件
user nobody nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80 ; # 域名,本地测试可以使用127.0.0.1或localhost server_name localhost; # php项目根目录 root /opt/www/xc_train; location / { # 定义首页索引文件的名称 index index.php index.html index.htm; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置. # Fastcgi服务器和程序(PHP)沟通的协议. location ~ .*\.php$ { # 设置监听端口 fastcgi_pass 127.0.0.1:9000; # 设置nginx的默认首页文件 fastcgi_index index.php; # 设置脚本文件请求的路径 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 引入fastcgi的配置文件 include fastcgi_params; } } }
② 项目拷贝到模板路径
如下图所示:
浏览器访问测试: