CentOS 7搭建PHP环境

news/2024/7/7 9:57:31

    PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。用WordPress搭建的博客需要用到PHP环境,进行博客环境的编译处理。
    安装PHP分为这几个步骤: 安装cmake安装libzip安装PHP配置启动文件创建PHP测试文件

一、安装cmake

1、为了避免不必要的麻烦,先安装需要到的依赖。复制粘贴,然后下载安装完成就可以。

yum install gcc \
              gcc-c++ \
              libxml2 \
              libxml2-devel \
              openssl \
              openssl-devel \
              libcurl \
              libcurl-devel \
              freetype \
              freetype-devel \
              libjpeg \
              libjpeg-devel \
              libpng \
              libpng-devel \
              libxslt \
              libxslt-devel \
              systemd-devel \
              libicu-devel \
              libedit-devel

2、因为用 yum install cmake -y 安装的是3.0以下的版本,比较旧。一般需要3.0以上的版本。所以先查看yum库里面有没有3.0的版本。

yum list all|grep cmake

3、使用 yum安装 cmake3

yum install epel-release -y
yum install cmake3 -y

4、安装完成以后输入cmake -version查看版本验证安装。看到下图证明安装成功。

5、因为我们安装PHP的时候使用的是cmake命令。而3.0版本是cmake3。为了方便使用,把cmake3改为cmake。查看位置,然后改名为cmake

输入:which cmake3
看到输出:/usr/bin/cmake3
修改名称:cp /usr/bin/cmake3 /usr/bin/cmake

二、安装libzip

1、不能用 yum install libzip安装,这样版本太低,可以到官网下载:libzip官网地址。然后用xftp上传到服务器。也可以直接用wget命令下载。

wget https://libzip.org/download/libzip-1.5.2.tar.gz

2、如果没有wget命令,可以用yum进行安装。

yum install wget -y

3、下载好以后进行解压。

tar xf libzip-1.5.2.tar.gz

4、进入libzip-1.5.2目录,进行编译。完成以下操作即可。

进入目录:cd libzip-1.5.2
创建build目录:mkdir build
进入build目录:cd build
输入命令:cmake ..
输入命令:make
输入命令:make install

三、安装PHP(源码安装)

1、下载PHP,可以到官网下载:PHP官网地址。然后用xftp上传到服务器。也可以直接用wget命令下载。进行解压。

wget https://www.php.net/distributions/php-7.3.12.tar.gz
tar xf php-7.3.12.tar.gz
进入目录:cd php-7.3.12

2、配置PHP需要创建用户。

创建用户:useradd -M -s /sbin/nologin www
查看用户信息:id www

3、需要修改库加载路径。

用vi编辑器打开:vim /etc/ld.so.conf

添加如下几行:
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64

使之生效:ldconfig

4、配置PHP,复制粘贴即可。

./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-config-file-scan-dir=/usr/local/php/etc/conf.d \
    --disable-cgi \
    --enable-fpm \
    --with-fpm-user=www \
    --with-fpm-group=www \
    --enable-ftp \
    --with-curl \
    --with-gd \
    --with-gettext \
    --with-iconv-dir \
    --with-kerberos \
    --with-libedit \
    --with-openssl \
    --with-pcre-regex \
    --with-pdo-mysql \
    --with-xsl \
    --with-zlib \
    --with-mhash \
    --with-mysqli \
    --with-png-dir=/usr/lib \
    --with-jpeg-dir=/usr/lib\
    --with-freetype-dir=/usr/lib \
    --enable-mysqlnd \
    --enable-bcmath \
    --enable-libxml \
    --enable-inline-optimization \
    --enable-gd-jis-conv \
    --enable-mbregex \
    --enable-mbstring \
    --enable-opcache \
    --enable-pcntl \
    --enable-shmop \
    --enable-soap \
    --enable-sockets \
    --enable-sysvsem \
    --enable-xml \
    --enable-zip \
    --enable-calendar \
    --enable-intl \
    --enable-exif

5、如果遇到类似的问题configure: error: Package requirements (sqlite3 > 3.7.4) were not met: No package 'sqlite3' found是正常现象。因为有的系统有些依赖不存在,需要手动安装。根据提示用yum -y install ......安装相应的依赖即可。依赖安装完重复第4步操作,直到最后出现Thank you for using PHP和下图样式。PHP才算安装完成。

6、需要编译`PHP`。(可能要`10`分钟左右,一般不会有错。)
进行编译:make
完成后进行:make install

四、配置PHP启动文件

1、需要复制一份配置文件到指定位置。将php-fpm.conf.default改为php-fpm.conf

复制配置文件:cp php.ini-development /usr/local/php/etc/php.ini

进入目录: cd /usr/local/php/etc
修改文件名:cp php-fpm.conf.default php-fpm.conf

2、拷贝启动脚本到指定目录。因为要用systemctl启动,所以要将启动脚本复制到/usr/lib/systemd/system/下。将www.conf.default改为www.conf

拷贝启动脚本到指定目录:
cp /root/php-7.3.12/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

进入目录:cd /usr/local/php/etc/php-fpm.d/
修改文件名:cp www.conf.default www.conf

3、用命令进行启动、查看状态、设置开机自启。也可以直接用/usr/local/php/sbin/php-fpm启动。

启动 PHP:
systemctl start php-fpm
查看状态:
systemctl status php-fpm
设置开机自启:
systemctl enable php-fpm
查看进程是否存在:
ps -ef|grep php-fpm
查看端口是否启动:
ss -lntup|grep 9000
4、配置`Nginx`解析。如果没有安装`Nginx`请查看:CentOS 7安装并配置Nginx。找到配置文件的`server`部分,修改站点路径,我这里为`root`根目录下的:`/www`,添加以下语句。首页文件改为`index.php`。
进入Nginx配置文件目录:
cd /etc/nginx/
server内添加:
location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
}
5、创建PHP测试文件。在浏览器输入`你的IP/index.php`,看到以下页面证明PHP已经测试完成,可以使用了。
进入目录:cd /www/
创建PHP测试文件:
echo “<?php phpinfo();?>” >index.php
重启Nginx:
nginx -s reload

五、总结(安装PHP遇到过的那些坑)

1、刚开始没有添加依赖,直接安装了cmake3,然后安装libzip的时候需要make编译。然后就出错了。cmake ..一直报错,make也出现错误。后来才知道缺少gcc依赖。所以要先安装依赖。

2、选择PHP版本的时候尽量不要用最新版本。问题太多。我刚开始安装的是PHP-7.4.2(当时最新版本),安装完成,编译也通过了,最后启动的时候报错,错误是我的系统有问题。(我觉得系统没有问题)。后来换成了PHP-7.3.12。最后居然成功启动了。所以说还是用稳定版本比较好。


http://www.niftyadmin.cn/n/3649356.html

相关文章

Android Context 详解

Android中context可以作很多操作&#xff0c;但是最主要的功能是加载和访问资源。 在android中有两种context&#xff0c;一种是application context&#xff0c;一种是activity context&#xff0c;通常我们在各种类和方法间传递的是activity context。 继承关系&#xff1a;…

如何在JavaScript中替换字符串的所有实例

介绍 (Introduction) Replacing text in strings is a common task in JavaScript. In this article you’ll look at using replace and regular expressions to replace text. 替换字符串中的文本是JavaScript中的常见任务。 在本文中&#xff0c;您将研究如何使用replace和正…

[C#]I/O完成端口的实现

在VC中我几乎每一个Windows Service都是采用I/O完成端口。至于在C#中如何使用I/O完成端口&#xff0c;一直很少见人提及。William Kennedy的三篇文章《IOCP Thread Pooling in C#》&#xff0c;对实现这种机制很有帮助&#xff0c;唯一美中不足的是&#xff0c;它只能把int数值…

MyEclipse 10破解方法及下载地址

破解地址:http://blog.csdn.net/xexiyong/article/details/8273440 下载地址&#xff1a;http://xiazai.sogou.com/detail/34/16/-3776446113025264156.html?e1970

个人博客搭建教程——基于WordPress

WordPress是使用PHP语言开发的博客平台&#xff0c;是免费开源的。用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把WordPress当作一个内容管理系统&#xff08;CMS&#xff09;来使用。本教程采用NginxMySQLPHPWordPress搭建个人博客系统。 使用Wor…

python 代码生成器_如何为VS代码创建Python类生成器

python 代码生成器介绍 (Introduction) If you hate stubbing out Python classes, here’s how you can create an extension in Visual Studio Code to do it for you. In this article, you’ll see how to create that extension. We will use several techniques to do so…

[C#]如何将自定义的structure转换为byte[]?

如何将自定义的structure转换为byte[]&#xff1f;整理者&#xff1a;郑昀UltraPower示例如下&#xff1a;using System.Runtime.InteropServices;public static byte[] RawSerialize( object anything ){int rawsize Marshal.SizeOf( anything );IntPtr buffer Marshal.Allo…

在Node.js中使用服务器发送的事件来构建实时应用

The goal of this article is to present a complete solution for both the back-end and front-end to handle realtime information flowing from server to client. 本文的目的是为后端和前端提供一个完整的解决方案&#xff0c;以处理从服务器到客户端的实时信息流。 The…