跳到主要内容

centos7部署php7.4

最后更新于:

实战-centos7如何部署php7.4-测试成功-2025.6.13

本次gpt的centos7如何部署php7.4,经实际测试是没问题的。

以下是gpt回答

在 CentOS 7 上部署 PHP 7.4 可以通过 Remi 仓库(官方推荐的第三方仓库)来完成。以下是详细步骤:


1. 更新系统并安装必要依赖

1sudo yum install epel-release -y
2sudo yum install yum-utils -y

2. 添加 Remi 仓库

Remi 仓库提供了最新的 PHP 版本:

1sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

3. 启用 PHP 7.4 仓库

1sudo yum-config-manager --enable remi-php74

4. 安装 PHP 7.4 及常用扩展

1sudo yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-opcache php-intl php-soap php-redis -y

📌 扩展说明:

  • php-fpm:用于 Nginx 或 Apache 的 FastCGI 模式
  • php-mysqlnd:MySQL 数据库支持
  • php-gd:图像处理
  • php-mbstring:多字节字符串支持
  • php-curl:HTTP 请求支持
  • php-xml:XML 解析支持
  • php-opcache:PHP 字节码缓存(提升性能)

5. 检查 PHP 版本

1php -v

输出应类似:

1PHP 7.4.x (cli) (built: ...)

6. 配置 PHP(可选)(暂时跳过)

编辑 php.ini 文件调整参数:

1sudo vi /etc/php.ini

常见优化选项:

1memory_limit = 256M
2upload_max_filesize = 64M
3post_max_size = 64M
4max_execution_time = 300
5date.timezone = Asia/Shanghai

7. 启动 PHP-FPM(适用于 Nginx)

如果使用 Nginx,需要启动 PHP-FPM:

1sudo systemctl start php-fpm
2sudo systemctl enable php-fpm

检查状态:

1sudo systemctl status php-fpm

8. 测试 PHP(这里测试有问题的)

创建一个测试文件:

1echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

访问:

1http://your-server-ip/info.php
2
3
4http://192.168.1.10/info.php

如果看到 PHP 信息页面,说明安装成功。


9. 配置 Nginx/Apache

📌http配置:

cat /etc/nginx/conf.d/imgapi.onedayxyy.cn.conf

 1server {
 2    listen 80;
 3    server_name imgapi.onedayxyy.cn;
 4    root /var/www/html;
 5    index index.php index.html;
 6
 7    location ~ \.php$ {
 8        fastcgi_pass 127.0.0.1:9000;
 9        fastcgi_index index.php;
10        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
11        include fastcgi_params;
12    }
13}

imgapi.onedayxyy.cn这里修改为自己的域名即可。

📌https配置:

cat /etc/nginx/conf.d/imgapi.onedayxyy.cn.conf

 1server {
 2    listen 80;
 3    server_name  imgapi.onedayxyy.cn;
 4    #配置https重定向
 5    return 301 https://$host$request_uri;
 6}
 7
 8server {
 9    listen  443 ssl;
10    server_name  imgapi.onedayxyy.cn;
11    root /images/imgapi;
12    index index.php index.html;
13
14    location ~ \.php$ {
15        fastcgi_pass 127.0.0.1:9000;
16        fastcgi_index index.php;
17        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
18        include fastcgi_params;
19    }
20
21
22
23    ssl_certificate             /etc/letsencrypt/live/onedayxyy.cn/fullchain.pem;
24    ssl_certificate_key         /etc/letsencrypt/live/onedayxyy.cn/privkey.pem;
25 
26    ssl_session_timeout 5m;
27    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
28    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
29    ssl_prefer_server_ciphers on;
30    add_header Strict-Transport-Security "max-age=31536000";
31    
32    access_log /var/log/nginx/photo.onedayxyy.cn.https.log;
33}

image-20250910064815322


10. 安全优化(暂时跳过)

  • 删除测试文件(避免暴露信息):
    1sudo rm /var/www/html/info.php
  • 防火墙开放 HTTP/HTTPS
    1sudo firewall-cmd --permanent --add-service=http
    2sudo firewall-cmd --permanent --add-service=https
    3sudo firewall-cmd --reload

常见问题

Q1:如何切换 PHP 版本?

如果安装了多个 PHP 版本,可以切换默认版本:

1sudo update-alternatives --config php

Q2:PHP 7.4 支持哪些框架?

  • Laravel 6.x/7.x/8.x
  • Symfony 4.4+
  • CodeIgniter 4
  • WordPress 5.6+

Q3:如何升级到 PHP 8.x?

只需启用 Remi 的 PHP 8.x 仓库:

1sudo yum-config-manager --enable remi-php80
2sudo yum update php*

总结

步骤操作
1更新系统并安装 EPEL
2添加 Remi 仓库
3启用 PHP 7.4 仓库
4安装 PHP 7.4 及扩展
5检查 PHP 版本
6优化 php.ini
7启动 PHP-FPM(Nginx 适用)
8测试 PHP
9配置 Nginx/Apache
10安全优化

现在你的 CentOS 7 已经成功运行 PHP 7.4!如果有问题,欢迎留言讨论。🚀

命令汇总

安装:

 1sudo yum install epel-release -y
 2sudo yum install yum-utils -y
 3
 4sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
 5
 6sudo yum-config-manager --enable remi-php74
 7
 8sudo yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-opcache php-intl php-soap php-redis -y
 9
10php -v
11
12sudo systemctl start php-fpm
13sudo systemctl enable php-fpm
14sudo systemctl status php-fpm
最新文章