Skip to content
Advertisement

Running PHP locally with Ubuntu

I am using Ubuntu 18.04 on Google Cloud Platform and I’m trying to run a test file called login.php. The path is /var/www/login.php. Whenever I try running it, I use sudo php -f /var/www/login.php then check http://localhost/var/www/login.php on my web browser. However, my web browser returns with This site can’t be reached, localhost refused to connect. I have looked everywhere for a solution but my web browser always comes back with an error.

Advertisement

Answer

You shouldn’t use http://localhost to reach your VM running in GCP.

To solve your issue you should follow the documentation Built-in web server, also you should configure network tags for your VM instance and create new firewall rule.

Please have a look on my steps below:

  1. create VM instance:
JavaScript
  1. configure network tag on VM instance:
JavaScript
  1. create firewall rule to allow inbound traffic on port 8080:
JavaScript
  1. install php 7.2:
JavaScript
  1. create index.php file:
JavaScript
  1. start a PHP web server from the folder with index.php file:
JavaScript
  1. check connection from VM instance (via secondary SSH connection):
JavaScript
  1. start a PHP web server on internal IP of your VM instance:
JavaScript
  1. check connection from on-premises on external IP of your VM:
JavaScript

same result via web browser at http://34.XXX.XXX.121:8080:

Hello World!

In addition, have a look at Getting started with PHP on Compute Engine to see an alternative solution.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement