京东云服务器如何安装PHP环境?

在京东云服务器上安装 PHP 环境,通常需要先选择好操作系统(如 CentOS、Ubuntu 等),然后根据你的需求安装 Web 服务器(Apache 或 Nginx)和数据库(如 MySQL/MariaDB),最后安装 PHP 及其扩展。以下是基于 CentOS 7/8Nginx + PHP-FPM + MySQL 的常见 LEMP 环境搭建步骤:


✅ 一、登录服务器

使用 SSH 登录你的京东云服务器:

ssh root@你的服务器IP地址

或者使用普通用户登录后 sudo su 切换为 root。


✅ 二、安装 Web 服务器(Nginx)

1. 安装 EPEL 仓库(如果未安装)

yum install epel-release -y

2. 安装 Nginx

yum install nginx -y

3. 启动并设置开机自启

systemctl start nginx
systemctl enable nginx

你可以通过浏览器访问服务器 IP 地址查看 Nginx 默认页面。


✅ 三、安装数据库(MySQL / MariaDB)

示例:安装 MariaDB(推荐轻量级)

yum install mariadb-server mariadb -y

启动 MariaDB 并设置开机自启:

systemctl start mariadb
systemctl enable mariadb

运行安全初始化脚本:

mysql_secure_installation

✅ 四、安装 PHP 和 PHP-FPM

1. 添加 EPEL 和 Remi 仓库(提供新版 PHP)

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

注意:如果是 CentOS 8,请使用对应的 remi-release-8.rpm。

2. 使用 yum-utils 启用 PHP 模块

yum install yum-utils -y
yum-config-manager --enable remi-php82   # 安装 PHP 8.2 版本(可选)

3. 安装 PHP 及常用扩展

yum install php php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-json -y

✅ 五、配置 PHP-FPM

编辑 PHP-FPM 配置文件:

vim /etc/php-fpm.d/www.conf

修改如下内容(确保与 Nginx 用户一致):

user = nginx
group = nginx
listen.owner = nobody
listen.group = nobody

保存退出,然后启动 PHP-FPM:

systemctl start php-fpm
systemctl enable php-fpm

✅ 六、配置 Nginx 支持 PHP

编辑默认站点配置文件:

vim /etc/nginx/conf.d/default.conf

将内容替换为以下内容(或修改成支持 PHP):

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ .php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

重启 Nginx:

systemctl restart nginx

✅ 七、测试 PHP 是否正常工作

创建一个测试文件:

echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php

浏览器访问:

http://你的服务器IP/info.php

如果看到 PHP 信息页,说明环境已经成功搭建!


✅ 八、(可选)配置防火墙允许 HTTP 访问

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

🧪 常见问题排查

问题 解决方法
页面显示空白或 502 Bad Gateway 检查 PHP-FPM socket 文件权限,确保 Nginx 能访问 /run/php-fpm/www.sock
PHP 不解析 .php 文件 检查 Nginx 的 location 匹配规则是否正确
无法访问 info.php 检查 Nginx 根目录路径是否正确,以及防火墙设置

✅ 总结

你现在已经完成了在京东云服务器上搭建完整的 PHP 环境(LEMP Stack):

  • Web Server: Nginx
  • Database: MariaDB / MySQL
  • Programming Language: PHP (with FPM)

如果你使用的是 Ubuntu 系统,我也可以提供对应的安装指南。需要的话请告诉我系统版本即可。

未经允许不得转载:秒懂云 » 京东云服务器如何安装PHP环境?