沈德山 發表於 2020-9-13 18:13:00

Linux服务器部署php环境 版本php-5.6.40(踩坑版)

<p>项目需求要在阿里云ecs ubuntu服务器上部署php环境以便配置前端代码</p>
<p>与前端小哥探讨好兼容版本后,决定使用php-5.6.40</p>
<p>&nbsp;</p>
<p>部署这玩意整了我半天。。。(踩坑...移坑!...踩坑...移坑!...)</p>
<p><strong><span style="text-decoration: underline">进入步骤</span></strong></p>
<p>1. 获取源yum,由于配置出了点问题我放弃了,还是apt-get安装工具</p>
<p>2. 安装php5.6.40</p>
<p>服务器上/usr/local/目录下执行命令</p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> apt-<span style="color: rgba(0, 0, 0, 1)">get update
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> apt-get -y <span style="color: rgba(0, 0, 255, 1)">install</span> php5.<span style="color: rgba(128, 0, 128, 1)">6.40</span></pre>
</div>
<p>由于还要加一堆扩展,我打算直接搞源码包</p>
<p>拎着tar包用sfpt直接生猛拖拽到目录下</p>
<p>推荐mac系统比较好用的svn管理工具FillZilla(这个是我用过最顺手的)/CornerStone(svn提代码版本控制记录很清晰,有时候down代码程序卡住不知啥原因)</p>
<p>回到话题,运用tar命令</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">tar</span> -zxvf php-<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz</pre>
</div>
<p>在服务器上解压出现问题</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">gzip</span>: stdin: unexpected end of <span style="color: rgba(0, 0, 255, 1)">file</span>

<span style="color: rgba(0, 0, 255, 1)">tar</span>: Unexpected EOF <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> archive

</span><span style="color: rgba(0, 0, 255, 1)">tar</span>: Unexpected EOF <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> archive

</span><span style="color: rgba(0, 0, 255, 1)">tar</span>: Error is not recoverable: exiting now</pre>
</div>
<p>查了一些大神的解决方法,大概问题都是源码包破坏或不完整,但重新下载后,尝试未果</p>
<p>问道同事原因可能是文件类型不支持,我反手就在本地同样执行了一下</p>
<p><img src="https://img2020.cnblogs.com/blog/1963728/202009/1963728-20200913163604585-127414459.png" alt="" width="457" height="304" loading="lazy"></p>
<p>&nbsp;</p>
<p>并没有任何异常。。。卡住</p>
<p>准备用最笨的方法直接把解压好的文件拖服务器上。。。上百兆的文件,两次sftp连接都中断了。。。再次卡住</p>
<p>翻大神博客的时候想到:问题一定是出在生拖到服务器导致文件破坏了</p>
<p><span style="text-decoration: underline">解决方法:</span></p>
<p>避免文件类型冲突,gz -&gt; tar -&gt; unzip file</p>
<p>到服务器/usr/local/目录</p>
<pre>sudo apt-get -y install php5.6.40<br>gunzip php-5.6.40.tar.gz</pre>
<p>得到 php-5.6.40.tar 然后tar命令执行,</p>
<pre>tar -xvf php-5.6.40.tar</pre>
<p>成功解压!</p>
<p>3. 配置php</p>
<p><code class="go plain">&nbsp;执行命令</code></p>
<div class="cnblogs_code">
<pre>cd /usr/local/php-<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span><span style="color: rgba(0, 0, 0, 1)">
.</span>/configure --prefix=/usr/local/php --with-config-<span style="color: rgba(0, 0, 255, 1)">file</span>-path=/usr/local/php/etc --with-iconv=/usr/local/php/libiconv --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-jpeg-<span style="color: rgba(0, 0, 255, 1)">dir</span> --with-png-<span style="color: rgba(0, 0, 255, 1)">dir</span> --with-pear --with-freetype-<span style="color: rgba(0, 0, 255, 1)">dir</span> --with-zlib --with-libxml-<span style="color: rgba(0, 0, 255, 1)">dir</span> --with-iconv-<span style="color: rgba(0, 0, 255, 1)">dir</span> --with-xmlrpc --with-mhash --with-mcrypt --with-curl --with-openssl --with-snmp --with-gettext --enable-pdo --enable-mbstring --enable-ctype --enable-simplexml --enable-<span style="color: rgba(0, 0, 255, 1)">ftp</span> --enable-sockets --enable-gd-native-ttf --enable-sysvsem --enable-exif --enable-sysvshm --enable-xml --enable-dom --enable-simplexml --enable-shmop --enable-<span style="color: rgba(0, 0, 255, 1)">zip</span> --enable-mbregex --enable-bcmath --enable-inline-optimization --enable-<span style="color: rgba(0, 0, 0, 1)">soap
</span><span style="color: rgba(0, 0, 255, 1)">make</span>
<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> test
</span><span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
#配置文件,移动</span>+<span style="color: rgba(0, 0, 0, 1)">改名
</span><span style="color: rgba(0, 0, 255, 1)">cp</span> /usr/local/php-<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span>/php.ini-production /usr/local/php/lib/php.ini</pre>
</div>
<p>4. index.php测试是否运行成功</p>
<p>输入命令php -v 正常查看到版本号后,在虚拟目录下放了个文件index.php</p>
<p>确保apache2正常运行后</p>
<p>地址栏输入发现无权限。。。卡住again(绝望...)</p>
<p><img src="https://img2020.cnblogs.com/blog/1963728/202009/1963728-20200913165544309-890416854.png" alt="" width="413" height="135" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;再次翻阅大神的解决方案</p>
<p>基于没有权限的问题,我尝试了所有可能</p>
<p>对于apache服务器,httpd.conf文件下需要改</p>
<p>&lt;Directory /&gt;<br>  Options FollowSymLinks<br>  AllowOverride None<br>  Order deny,allow<br>  <span style="background-color: rgba(255, 255, 255, 1); color: rgba(255, 0, 0, 1)">Allow</span> from all<br>  Satisfy all<br>&lt;/Directory&gt;&nbsp;</p>
<p>对于apache2,<span class="s1">apache2.conf文件下要</span></p>
<p>保证<span style="color: rgba(255, 0, 0, 1)">Require all granted</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">Directory </span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    AllowOverride All
    Require all granted
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">Directory</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">IfModule </span><span style="color: rgba(255, 0, 0, 1)">dir_module</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    DirectoryIndex index.html index.htm index.php
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">IfModule</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)"><br></span></pre>
</div>
<p><span style="text-decoration-line: underline">解决方法:</span></p>
<p>找到apache2.conf /&nbsp;httpd.conf</p>
<p><span style="color: rgba(0, 0, 0, 1)">找不到的话:</span></p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">find</span> / -name apache2.conf</pre>
</div>
<p>cd到目录</p>
<div class="cnblogs_code">
<pre>$ vim apache2.conf</pre>
</div>
<p>按上面修改/添加内容</p>
<p>esc键</p>
<p>:wq(保存退出)</p>
<p>重启apache</p>
<p>打开网页后发现问题:输出了源代码</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php

    </span><span style="color: rgba(0, 128, 128, 1)">phpinfo</span><span style="color: rgba(0, 0, 0, 1)">();

</span>?&gt;</pre>
</div>
<p>显然apache没有解析php</p>
<p>再次查看大神的方案,修改配置文件FilesMatch</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">FilesMatch </span><span style="color: rgba(255, 0, 0, 1)">\.php$</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
SetHandler application/x-httpd-php
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">FilesMatch</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>并在配置文件上加上这一行&nbsp;</p>
<div class="cnblogs_code">
<pre>AddType application/x-httpd-php .php</pre>
</div>
<p>重启apache,无反应</p>
<p>通过搜索大神们的经验,我发现</p>
<p class="p1"><span class="s1">/usr/lib/apache2/modules</span><span class="s1">/</span></p>
<p class="p1"><span class="s1">此目录下没有libphp5.so,导致apache无法解析php</span></p>
<p class="p1"><span style="text-decoration: underline"><span class="s1">解决方法:</span></span></p>
<p class="p1"><span class="s1">回到php安装目录下重新make,./configure</span></p>
<div class="cnblogs_code">
<pre>cd /usr/local/php-<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span>
<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> clean #重新编译前一定要clean!!!
#之前发现configure后面扩展参数跟了一大堆,但没有几个需要用,想要php支持谁就加上
.</span>/<span style="color: rgba(0, 0, 0, 1)">configure
</span>--prefix=/png/php/<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span><span style="color: rgba(0, 0, 0, 1)">      #php安装目录
</span>--enable-<span style="color: rgba(0, 0, 0, 1)">fpm                  #支持fpm组件<br>--with-openssl&nbsp;         #支持生成证书
</span>--with-apxs2=/usr/bin/<span style="color: rgba(0, 0, 0, 1)">apxs2   #最重要!!生成libphp5.so
                  #mysql
</span>--with-<span style="color: rgba(0, 0, 0, 1)">mysqli
</span>--with-pdo-<span style="color: rgba(0, 0, 0, 1)">mysql
</span>--with-pdo-<span style="color: rgba(0, 0, 0, 1)">sqlite<br>
</span><span style="color: rgba(0, 0, 255, 1)">make</span>
<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> test
</span><span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span></pre>
</div>
<p>重启apache2</p>
<p>报错:<span style="text-decoration: underline">Apache is running a threaded MPM,but your PHP Modle is not compiled to be threadsafe. You need to recompile PHP.</span></p>
<p>又一次翻了好多大神经验,我这次真的有点怕把apache搞崩。。。</p>
<p>主要错误意思是:php编译模式出错。我安装的版本php不是线程安全的,但apache2是基于MPM模式。</p>
<p>windows系统下的php源码包不能下载Non-thread-safe zip pkg,</p>
<p>linux下编译的时候不能生成认证证书,干掉<span style="color: rgba(255, 0, 0, 1)">--with-openssl</span> 扩展参数</p>
<p>configure加上参数&nbsp;</p>
<h1>--enable-maintainer-zts</h1>
<p>这个太重要了,整了我好久。。。</p>
<p><span style="text-decoration: underline">解决方法:</span></p>
<p>回到php安装目录下重新make,./configure</p>
<div class="cnblogs_code">
<pre>cd /usr/local/php-<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span>
<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> clean

.</span>/configure --prefix=/png/php/<span style="color: rgba(128, 0, 128, 1)">5.6</span>.<span style="color: rgba(128, 0, 128, 1)">40</span> --enable-fpm --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --with-pdo-sqlite --enable-maintainer-<span style="color: rgba(0, 0, 0, 1)">zts

</span><span style="color: rgba(0, 0, 255, 1)">make</span>
<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> test
</span><span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span></pre>
</div>
<p>重启Apache2</p>
<p>apachectl -k restart</p>
<p>打开页面index.php</p>
<p><img src="https://img2020.cnblogs.com/blog/1963728/202009/1963728-20200913180913653-437407184.png" alt="" width="621" height="465" loading="lazy"></p>
<p>&nbsp;完美!</p>
<p>完结,散花!!!</p>
<p>p.s. 整了一天多,踩了很多坑,吸取教训,多查资料,多思考,总会解决问题的。</p>
<p>记录自己的技术生涯,大神莫喷,有问题大家沟通伐!</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/sjzhai/p/13661782.html
頁: [1]
查看完整版本: Linux服务器部署php环境 版本php-5.6.40(踩坑版)