PHP7.2 源码安装
<h2>1-源码安装说明</h2>
<p>1--系统为 CentOS 7 64位</p>
<p>2--一般开发环境可以使用一键安装包,例如 phpstudy</p>
<p>3--生产环境一般使用源码包安装</p>
<p>4-关闭防火墙</p>
<pre><code class="language-bash"># 1--查看防火墙状态
systemctl status firewalld
# 2--关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 3--修改 SELINUX 为禁用
vi /etc/sysconfig/selinux
SELINUX=disabled
# 4、重启服务器
reboot</code></pre>
<p>5--安装php所需环境</p>
<p>需要提前设置好yum源,再进行安装</p>
<pre><code class="language-bash">yum -y install gcc gcc-c++ \
epel-release oniguruma oniguruma-devel \
freetype freetype-devel \
gd gd-devel \
glibc-devel glib2-devel \
libtool libmcrypt-devel \
mcrypt mhash ncurses-devel \
libxml2 libxml2-devel bzip2-devel libcurl-devel \
curl-devel
libjpeg-devel libpng-devel \
net-snmp-devel \
openssl openssl-deve python-devel zlib-devel libxslt* bison autoconf re2c \
libzip-devel pcre-devel</code></pre>
<h2>2-源码安装php</h2>
<h4>1--官网下载网址</h4>
<p> <a href="https://www.php.net/downloads">https://www.php.net/downloads</a></p>
<h4>2--7.2版本下载地址</h4>
<p> <a href="http://cn2.php.net/distributions/php-7.2.10.tar.gz">http://cn2.php.net/distributions/php-7.2.10.tar.gz</a></p>
<h2>3-PHP7.2源码安装步骤</h2>
<h4>1--创建目录</h4>
<pre><code class="language-bash">cd /opt
mkdir php
cd php</code></pre>
<h4>2--下载php并解压</h4>
<pre><code class="language-bash"># 下载php7.4
wget http://cn2.php.net/distributions/php-7.2.10.tar.gz
ls # 显示 php-7.2.10.tar.gz
# 解压php
tar xvf php-7.2.10.tar.gz
ls # 显示 php-7.2.10
cd php-7.2.10/
ls</code></pre>
<h4>3--配置</h4>
<h5>1、查看configure帮助,一些具体命令</h5>
<pre><code class="language-bash">./configure --help</code></pre>
<h5>2、configure作用</h5>
<pre><code>对即将安装的软件进行一些配置</code></pre>
<h5>3、先安装gcc,如果没有安装,会提示先安装gcc编译器</h5>
<h5>4、进行配置</h5>
<p> <strong>一定不要采用默认安装,就是直接执行 ./configure ,系统会把php安装到不同路径下,删除时,非常麻烦,要一个一个找系统安装php路径进行删除</strong></p>
<pre><code class="language-bash"># 设置安装目录
cd /usr/local/
mkdir php
ls
# 设置具体配置
./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=/var/run/mysql/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-pcntl \
--enable-calendar \
--enable-exif \
--enable-sockets \
--with-xmlrpc \
--with-libxml-dir \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-bz2 \
--enable-opcache \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--without-gdbm \
--enable-fast-install \
--disable-fileinfo</code></pre>
<p> ./configure 的一些注释</p>
<pre><code class="language-bash">--prefix=/usr/local/php \ # php所安装的路径前缀,例如 /usr/local
--exec-prefix=/usr/local/php \ # php执行路径前缀
--bindir=/usr/local/php/bin \ # php的 bin 目录
--sbindir=/usr/local/php/sbin \ # php的 sbindir 目录
--includedir=/usr/local/php/include \ # php的 include 目录
--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=/var/run/mysql/mysql.sock \ # php的 mysql.sock</code></pre>
<p>Thank you for using PHP. 出现此句显示配置完成,无错误,可以执行编译</p>
<h4>4--编译</h4>
<pre><code class="language-bash"># 编译时间有点长,请耐心等待
make
# 检测之前进行的make编译步骤中遗漏的错误,确保服务的编译正常
make test</code></pre>
<h4>5-安装</h4>
<pre><code class="language-bash">make install</code></pre>
<h5>1、make install信息</h5>
<pre><code class="language-bash">Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing PHP FPM defconfig: /usr/local/php/etc/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
Installing phpdbg binary: /usr/local/php/bin/
Installing phpdbg man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
/opt/php/php-7.2.10/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/</code></pre>
<h5>2、测试是否安装成功</h5>
<pre><code class="language-php">/usr/local/php/bin/php -v</code></pre>
<h2>4-简化PHP执行命令,设置php命名</h2>
<pre><code class="language-bash"># 1--进入当前用户根目录下
vi ~/.bash_profile
# 添加别名
alias php=/usr/local/php/bin/php
# 2--在当前bash环境下读取并执行 .bash_profile 文件
source ~/.bash_profile
# 3--测试别名是否设置成功,查看php版本
php -v</code></pre>
<h2>5-设置php.ini</h2>
<pre><code class="language-ini"># 复制 php.ini 到 php 配置文件目录中
cp -a php.ini-production /usr/local/php/etc/php.ini
# 修改 php.ini
vim /usr/local/php/etc/php.ini
# 修改内容为
[PHP]
expose_php = Off #禁止显示PHP版本
short_open_tag = ON #支持PHP短标签
[Date]
date.timezone = Asia/shanghai #指定时区为上海
[opcache]
opcache.enable=1 #PHP支持opcode缓存
opcache.enable_cli=0
zend_extension=opcache.so #在[opcache]最后一行添加,开启opcode缓存功能</code></pre>
<h2>6-配置php-fpm和www.conf</h2>
<pre><code class="language-bash">cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# php-fpm 具有执行权限
chmod +x /etc/rc.d/init.d/php-fpm
# 检查 php-fpm 服务
chkconfig --add php-fpm</code></pre>
<h2>7-设置php环境变量</h2>
<pre><code class="language-bash">vi /etc/profile
# 添加php环境变量路径 /usr/local/php/bin/
PATH=$PATH:/usr/local/nginx/sbin/:/usr/local/php/bin/
# 在当前bash环境下读取并执行 /etc/profile 文件
source /etc/profile</code></pre>
<h2>8-启动 php-fpm</h2>
<pre><code class="language-bash"># 启动 php-fpm
systemctl start php-fpm
# 查看 php-fpm 状态
ss -ant|grep 9000 # 启动成功显示 LISTEN 0 128 127.0.0.1:9000 </code></pre>
<h2>9-让 Nginx 支持 php</h2>
<pre><code class="language-nginx"># 进入nginx配置文件目录
cd /usr/local/nginx/conf/
ls
# 备份nginx配置文件,防止配置文件修改错误,无法还原
cp nginx.conf nginx.conf_bak
ls
# 修改 nginx 配置文件
vi nginx.conf
# 修改代码
server {
index index.html index.htm index.php; # 增加 index.php
}
# 去掉下边代码的注释,fastcgi_param 后边代码 /scripts 替换为 $document_root
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;
}
# 重启 nginx 服务,让修改配置文件生效
systemctl restart nginx.service</code></pre>
<h2>10-创建php文件,进行测试</h2>
<pre><code class="language-php"># 进入 nginx 的 web 目录
cd /usr/local/nginx/html
# 创建 test.php 文件
vi test.php</code></pre>
<h4>1--test.php 代码为</h4>
<pre><code class="language-php"><?php
phpinfo();</code></pre>
<h4>2--查看当前 IP 地址</h4>
<pre><code class="language-bash">ip addr</code></pre>
<h4>3--浏览器浏览地址:</h4>
<p> <a href="http://192.168.0.102/test.php">http://192.168.0.102/test.php</a></p>
<p> 查看是否可以正常访问,显示 phpinfo 的内容。</p>
<p> 可以访问,显示内容,安装成功。</p>