0%

Linux安装php7.4和nginx配置

模块说明

特别注意:php7.3开始要求系统libzip的version >= 0.11,但是目前通过 yum -y install libzip-devel 安装的是0.10的版本,官方版本从1.4.0开始要依赖cmake安装,对cmake的版本还有要求

报错如图:

下面介绍三种安装方式:

1、yum安装(推荐)

1
2
3
4
5
6
7
8
# 卸载原有低版本的libzip包
sudo yum -y remove libzip libzip-devel
# 重新安装看看版本是否符合要求
sudo yum -y install libzip libzip-devel
# 否则添加wlnmp源
sudo rpm -ivh https://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
# 安装libzip包,当前wlnmp提供的libzip包版本1.8.0
sudo yum install wlibzip

2、不需要安装cmake(次推荐)

1
2
3
4
5
6
7
8
9
# 装1.3.2的版本,不需要安装cmake
cd /home/www/soft_download
sudo yum -y remove libzip-devel libzip
wget -c https://libzip.org/download/libzip-1.3.2.tar.gz
tar -zxvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
sudo make -j 4
sudo make install

3、编译安装

编译安装 CMake 3.16.2

1
2
3
4
5
6
7
8
9
10
cd /home/www/soft_download
sudo yum -y remove libzip-devel libzip
wget https://github.com/Kitware/CMake/releases/download/v3.16.2/cmake-3.16.2.tar.gz
tar -zxvf cmake-3.16.2.tar.gz
cd cmake-3.16.2
./bootstrap --prefix=/usr/local/cmake --datadir=/usr/local/cmake-data --docdir=/usr/local/cmake-doc
sudo make
sudo make install
sudo ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
cmake --version

编译安装 libzip 1.8.0

1
2
3
4
5
6
7
8
9
10
sudo yum install -y openssl-devel bzip2 bzip2-devel
cd /home/www/soft_download
wget --no-check-certificate https://libzip.org/download/libzip-1.8.0.tar.gz
tar -zxvf libzip-1.8.0.tar.gz
cd libzip-1.8.0
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/cmake
sudo make
sudo make install

配置

1
2
3
4
5
6
7
8
9
10
11
# 查看系统当前环境变量(用于扩展pkg-config的搜索路径)
echo $PKG_CONFIG_PATH
# 临时修改配置
export PKG_CONFIG_PATH="/usr/local/cmake/lib64/pkgconfig":$PKG_CONFIG_PATH
# 永久修改配置
vim /etc/profile # 如果只修改当前用户的环境变量则是`vim ~/.bashrc`
export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/cmake/lib64/pkgconfig
# 立刻生效
source /etc/profile
# 检查
pkg-config --list-all|grep libzip

PHP安装

这里安装php7.4.30

1、下载php7.4.30 这里安装php7.4.30

2、上传到/usr/local并解压 cd php-7.4.30

3、安装必备依赖

1
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libzip gcc-c++

4、进行预编译
cd /usr/local/php-7.4.30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
./configure  \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/lib \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-fpm \
--enable-opcache \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--enable-gd-jis-conv \
--with-jpeg \
--with-freetype \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm

5、编译&&安装

1
make && make install

6、配置并启动

1
2
3
4
cd /usr/local/php/  #先进入安装目录
cp ./etc/php-fpm.conf.default ./etc/php-fpm.conf #配置文件
vim /usr/local/php/etc/php-fpm.conf
将;pid = run/php-fpm.pid 前面的;分号删掉

#配置文件

1
2
cp /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/local/php-7.4.30/php.ini-production /usr/local/php/lib/php.ini

7、修改php.ini配置文件

1
2
3
4
5
max_execution_time = 300
max_input_time = 600
post_max_size = 80M
max_file_uploads = 20
date.timezone = Asia/Shanghai

8、配置启动服务

方案一:

(1)、添加php-fpm服务启动脚本

1
vim  /lib/systemd/system/php-fpm.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm ##需要注意修改自己的路径
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target

**服务字段说明**

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

(2)、 systemctl list-unit-files –type=service查看有没有php-fpm.service
如果没有就是用systemctl daemon-reload重新加载,在使用以上命令查看

(3)、添加开机自启 systemctl enable php-fpm.service

方案二(常用):

(1)、/拷贝php-fpm脚本至/etc/init.d目录

1
cp /usr/local/php-7.4.30/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm

(2)、设置权限并启动php-fpm

1
2
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start

php-fpm以服务的方式启动、停止和重启

1
2
3
service php-fpm start
service php-fpm stop
service php-fpm reload

(3)、设置php-fpm开机自启

1
2
3
cp /usr/local/php-7.4.22/sapi/fpm/php-fpm.service  /usr/lib/systemd/system

systemctl enable php-fpm

Nginx解析PHP

进入到nginx安装目录 cd /usr/local/nginx/

修改配置文件 vim ./conf/nginx.conf (或者添加vhost下的子配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 8088;
server_name localhost;
index index.html index.htm index.php default.html default.htm default.php;
root /xxx/xx; #项目路径

#charset koi8-r;
#access_log logs/host.access.log main;

#tp框架伪静态
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /xxx/xxx; #项目路径
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

到项目地址新建info.php文件

1
2
<?php
phpinfo();

启动nginx服务 nginx -s reload

访问http://127.0.0.1:8088/info.php 查看php是否生效