在 CentOS 系统中,您可以使用以下方法启动 PHP 服务:
使用命令行界面 (CLI) 启动 PHP 内置服务器:
在命令行中输入以下命令,将 /path/to/your/php/script.php 替换为您的 PHP 脚本的实际路径:
php /path/to/your/php/script.php
这将在当前目录下启动一个内置的 PHP 服务器,您可以通过访问 http://localhost:8000 来查看脚本。
使用 Apache HTTP 服务器运行 PHP 脚本:
首先,确保您已经安装了 Apache 和 mod_php 模块。如果没有,请使用以下命令安装:
sudo yum install httpd mod_php
然后,启动 Apache 服务:
sudo systemctl start httpd
接下来,将您的 PHP 脚本放入 Apache 的默认文档根目录(通常是 /var/www/html),或者根据需要修改 Apache 配置文件中的 DocumentRoot 指令。
最后,重启 Apache 服务以使更改生效:
sudo systemctl restart httpd
现在,您可以通过访问 http://your_server_ip_or_domain/your_script.php 来查看您的 PHP 脚本。
使用 Nginx 和 PHP-FPM 运行 PHP 脚本:
首先,确保您已经安装了 Nginx 和 PHP-FPM。如果没有,请使用以下命令安装:
sudo yum install nginx php-fpm
然后,启动 Nginx 和 PHP-FPM 服务:
sudo systemctl start nginx
sudo systemctl start php-fpm
接下来,配置 Nginx 以便将 PHP 请求转发给 PHP-FPM。编辑 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf),并添加以下内容:
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
保存更改并退出编辑器。
最后,重启 Nginx 服务以使更改生效:
sudo systemctl restart nginx
现在,您可以通过访问 http://your_server_ip_or_domain/your_script.php 来查看您的 PHP 脚本。