我正在Google Cloud Platform上使用Ubuntu 18.04,并且试图运行名为login.php
的测试文件。路径为/var/www/login.php
。每当尝试运行它时,我都会使用sudo php -f /var/www/login.php
,然后在网络浏览器中检查 http://localhost/var/www/login.php 。但是,我的网络浏览器返回无法访问此站点,本地主机拒绝连接。。我到处都在寻找解决方案,但我的网络浏览器总是返回错误。
答案 0 :(得分:2)
您不应使用http://localhost
来访问在GCP中运行的VM。
要解决您的问题,请遵循文档Built-in web server,还应为VM实例配置network tags并创建新的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
$ gcloud compute instances create instance-1 --zone=us-central1-a --tags=php-http-server
$ 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
$ 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 Web服务器:$ 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.
$ curl http://localhost:8080
Hello World!
$ 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.
$ curl http://34.XXX.XXX.121:8080
Hello World!
通过网络浏览器在http://34.XXX.XXX.121:8080
上获得相同的结果:
Hello World!
此外,请查看Getting started with PHP on Compute Engine来了解替代解决方案。