您的位置:首页 > 编程语言 > PHP开发

Ubuntu 18.04 通过源代码编译安装 PHP 7.3.11

2020-02-03 03:59 1591 查看

操作系统:Ubuntu 18.04
应用版本:PHP7.3.11
安装目录:/usr/local/php

依赖项

源码编译安装最开始就是要解决依赖项的问题了,而 PHP源码安装特别容易出现各种各样的依赖项出错。大体上,PHP 需要的依赖项有以下这么多

gcc make openssl curl libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libssl-dev libmcrypt-dev

这些依赖项是必需的,不安装预编译阶段会报错。
然后,我们还需要手动下载编译安装
libzip
,因为官方不包含这个文件。
切换到你暂存压缩包的文件夹中将它下载下来:
wget wget https://libzip.org/download/libzip-1.5.2.tar.gz

进入解压缩后的文件夹,新建一个
build
文件夹用于编译。

cd build
cmake ..
make
make test
make install
make clean

依次输入上面的命令,就把

libzip
安装完成了。

编译安装PHP

上面的依赖项问题解决后, 开始预编译 PHP ,首先进入解压缩后的 PHP 文件夹中,键入以下命令:

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl-dir=/usr/bin/curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts

预编译完成后,会给出以下提示

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

然后进行编译

sudo make -j4
,这是一个漫长的过程,成功后给出以下提示:

Build complete.
Don't forget to run 'make test'.

如果出现这个问题:

error while loading shared libraries: libzip.so.5: cannot open shared object file: No such file or directory
Makefile:452: recipe for target 'ext/phar/phar.php' failed
make: *** [ext/phar/phar.php] Error 127

是因为进行编译操作时找不到相应的动态链接库,此时我们要手动链接:

cd /etc/ld.so.conf
//加入以下内容到新行:/usr/local/lib。保存退出

执行命令:

/sbin/ldconfig
,继续编译
make

又出现报错(这一个错误不是必然出现的)

Generating phar.phar
chmod: cannot access `ext/phar/phar.phar': No such file or directory
make: [ext/phar/phar.phar] Error 1 (ignored)

Build complete.
Don't forget to run 'make test'.

此时,运行以下命令:

cd ext/phar/
cp phar.php phar.phar

cd ..
以及
make
。会发现错误已经解决。

PHP环境的配置

安装完成后,需要进行 LEMP总体配置。
首先我们需要复制配置文件 php.ini 到安装目录下:

cp php.ini-development /usr/local/php/etc/php.ini
(此时应该处于解压缩后的php目录中)。
然后配置 php-fpm ,
cd /usr/local/php/etc

执行以下指令

cp php-fpm.conf.default  php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf

然后打开 php-fpm 服务 :

cd /usr/local/php/sbin/
并且
./php-fpm
。出现报错,因为没有设置用户。此时

groupadd www
useradd -g www www

再次

./php-fpm
,此时没有任何消息,使用命令
ps -ef | grep php-fpm
可以查到服务已经在运行。
然后,就是配置 Nginx 解析 PHP
(这一部分大量参考了Ubuntu 18.04 源码编译安装PHP7.3.5详细过程
先到 Nginx的安装目录下(在此是
/usr/local/nginx
):
cd conf

这里会有一个
nginx.conf
文件,这就是我们要配置的文件。我们进行修改
vi nginx.conf

然后我们要把所有的
server{}
部分全部注释掉,在
server{}
下加上
include ./vhosts/*.conf;

在该目录下新建一个
vhosts
目录,然后新建一个
*.com.conf
文件,将以下内容填入文件。
(其中,你要修改
server_name
root

server {
listen    80;            # 监听端口
server_name 0.0.0.0;  # 这里需要改成你自己的公网IP
root /var/www/html/;       # 站点根目,这里也是要你自己设置你的站点根目录
index index.html index.htm index.php;  # 默认导航页

location / {
index index.html index.htm index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}

}
# PHP配置
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;
}
}

当然,你也可以根据参考文章来修改配置信息。

修改完成后保存,要验证配置信息是否没有错误 ,运行命令

nginx -t

如果正确会出现以下信息:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

此时只需要重新启动 nginx 就行:

nginx -s reload

最后,你需要验证是否解析成功。那么你进入你的网站根目录(这里是

/var/www/html/
)
新建一个文件
vim info.php
并且输入以下内容

<?php
phpinfo();

保存退出,通过浏览器访问

你的公网IP/info.php
,不出意外,会弹出这么一个界面
这表示你成功了,当然你可以自己写一个html网页(例如
index.html
)然后上传到网站根目录中(这里是
/var/www/html/
)中,然后在浏览器输入
你的公网IP/index.html
,也能访问成功。

最后,一定记得把你所创建的

info.php
文件删除,它包含了你服务器的大量信息,暴露在此不安全。
sudo rm -f /var/www/html/info.php
。今后你可以随时重新生成该文件。

至此, LEMP 环境搭建完成。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
_buffer 发布了4 篇原创文章 · 获赞 0 · 访问量 161 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: