搭建 PHP 运行环境需要以下组件:操作系统(如 Linux、macOS 或 Windows)Web 服务器(如 Apache 或 Nginx)PHP 解释器

搭建 PHP 运行环境需要以下组件:
选择一个兼容 PHP 的操作系统,例如:
安装一个 Web 服务器来处理 HTTP 请求并运行 PHP 代码,例如:
Apache
立即学习“PHP免费学习笔记(深入)”;
<code>sudo apt-get install apache2 (Ubuntu) sudo yum install httpd (CentOS) sudo brew install httpd (macOS)</code>
Nginx
<code>sudo apt-get install nginx (Ubuntu) sudo yum install nginx (CentOS) sudo brew install nginx (macOS)</code>
安装 PHP 解释器,它将执行 PHP 代码:
<code>sudo apt-get install php7.4-fpm (Ubuntu) sudo yum install php7.4-fpm (CentOS) sudo brew install php7.4 (macOS)</code>
注意: PHP 版本号可能会根据你的需求而有所不同。
配置 Web 服务器以使用 PHP 解释器处理 PHP 请求:
Apache
在配置文件 /etc/apache2/sites-available/000-default.conf 中添加以下内容:
<code><VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost></code>Nginx
在配置文件 /etc/nginx/sites-available/default 中添加以下内容:
<code>server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}
}</code>启动 Web 服务器和 PHP-FPM 服务:
Apache
立即学习“PHP免费学习笔记(深入)”;
<code>sudo systemctl start apache2</code>
Nginx
<code>sudo systemctl start nginx sudo systemctl start php7.4-fpm</code>
创建一个简单的 PHP 文件(如 index.php)并将其放置在你的 Web 根目录中:
<code class="php"><?php echo "Hello World!"; ?></code>
访问你的 PHP 文件以查看是否正确运行:http://localhost/index.php。
如果你看到 "Hello World!",则你的 PHP 运行环境已成功搭建。
以上就是如何搭建php运行环境的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号