运行 PHP 本地 Ubuntu
Running PHP locally with Ubuntu
我在 Google Cloud Platform 上使用 Ubuntu 18.04,我正在尝试 运行 一个名为 login.php
的测试文件。路径是 /var/www/login.php
。每当我尝试 运行ning 时,我都会使用 sudo php -f /var/www/login.php
然后在我的网络浏览器上检查 http://localhost/var/www/login.php。但是,我的网络浏览器 returns 无法访问此站点,本地主机拒绝连接。 我到处寻找解决方案,但我的网络浏览器总是返回一个错误。
您不应使用 http://localhost
访问 GCP 中的虚拟机 运行。
要解决您的问题,您应该遵循文档Built-in web server, also you should configure network tags for your VM instance and create new firewall rule。
请看下面我的步骤:
- 创建虚拟机实例:
$ gcloud compute instances create instance-1 --zone=us-central1-a --machine-type=n1-standard-1 --image=ubuntu-1804-bionic-v20200610 --image-project=ubuntu-os-cloud
- configure VM 实例上的网络标记:
$ gcloud compute instances create instance-1 --zone=us-central1-a --tags=php-http-server
- create 允许端口 8080 上的入站流量的防火墙规则:
$ gcloud compute firewall-rules create allow-php-http-8080 --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:8080 --source-ranges=0.0.0.0/0 --target-tags=php-http-server
- 安装 php 7.2:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install php7.2
- 创建
index.php
文件:
$ cd /var/www/
$ cat index.php
<?php
echo 'Hello World!';
?>
- 从包含
index.php
个文件的文件夹启动 PHP 网络服务器:
$ php -S localhost:8080
PHP 7.2.24-0ubuntu0.18.04.6 Development Server started at Thu Jun 18 12:31:16 2020
Listening on http://localhost:8080
Document root is /var/www
Press Ctrl-C to quit.
- 检查来自 VM 实例的连接(通过辅助 SSH 连接):
$ curl http://localhost:8080
Hello World!
- 在您的 VM 实例的内部 IP 上启动 PHP 网络服务器:
$ php -S 10.128.0.4:8080
PHP 7.2.24-0ubuntu0.18.04.6 Development Server started at Thu Jun 18 12:40:46 2020
Listening on http://10.128.0.4:8080
Document root is /var/www
Press Ctrl-C to quit.
- 检查您 VM 外部 IP 上的本地连接:
$ curl http://34.XXX.XXX.121:8080
Hello World!
通过 Web 浏览器在 http://34.XXX.XXX.121:8080
:
上的结果相同
Hello World!
此外,请查看 Getting started with PHP on Compute Engine 以了解替代解决方案。
我在 Google Cloud Platform 上使用 Ubuntu 18.04,我正在尝试 运行 一个名为 login.php
的测试文件。路径是 /var/www/login.php
。每当我尝试 运行ning 时,我都会使用 sudo php -f /var/www/login.php
然后在我的网络浏览器上检查 http://localhost/var/www/login.php。但是,我的网络浏览器 returns 无法访问此站点,本地主机拒绝连接。 我到处寻找解决方案,但我的网络浏览器总是返回一个错误。
您不应使用 http://localhost
访问 GCP 中的虚拟机 运行。
要解决您的问题,您应该遵循文档Built-in web server, also you should configure network tags for your VM instance and create new firewall rule。
请看下面我的步骤:
- 创建虚拟机实例:
$ gcloud compute instances create instance-1 --zone=us-central1-a --machine-type=n1-standard-1 --image=ubuntu-1804-bionic-v20200610 --image-project=ubuntu-os-cloud
- configure VM 实例上的网络标记:
$ gcloud compute instances create instance-1 --zone=us-central1-a --tags=php-http-server
- create 允许端口 8080 上的入站流量的防火墙规则:
$ gcloud compute firewall-rules create allow-php-http-8080 --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:8080 --source-ranges=0.0.0.0/0 --target-tags=php-http-server
- 安装 php 7.2:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install php7.2
- 创建
index.php
文件:
$ cd /var/www/
$ cat index.php
<?php
echo 'Hello World!';
?>
- 从包含
index.php
个文件的文件夹启动 PHP 网络服务器:
$ php -S localhost:8080
PHP 7.2.24-0ubuntu0.18.04.6 Development Server started at Thu Jun 18 12:31:16 2020
Listening on http://localhost:8080
Document root is /var/www
Press Ctrl-C to quit.
- 检查来自 VM 实例的连接(通过辅助 SSH 连接):
$ curl http://localhost:8080
Hello World!
- 在您的 VM 实例的内部 IP 上启动 PHP 网络服务器:
$ php -S 10.128.0.4:8080
PHP 7.2.24-0ubuntu0.18.04.6 Development Server started at Thu Jun 18 12:40:46 2020
Listening on http://10.128.0.4:8080
Document root is /var/www
Press Ctrl-C to quit.
- 检查您 VM 外部 IP 上的本地连接:
$ curl http://34.XXX.XXX.121:8080
Hello World!
通过 Web 浏览器在 http://34.XXX.XXX.121:8080
:
Hello World!
此外,请查看 Getting started with PHP on Compute Engine 以了解替代解决方案。