To set up PHP on a Google Cloud Compute Engine VM, you create a Linux VM instance, connect via SSH, install PHP and a web server (like Apache or Nginx), and then deploy your PHP files to the server’s document root. This guide walks through the entire process, from VM creation to verifying your PHP installation, covering both Debian/Ubuntu and CentOS/RHEL families.
Prerequisites and Initial VM Setup
Before you begin, you need a Google Cloud account with billing enabled, and the gcloud command-line tool installed locally (or you can use the Cloud Shell in the Google Cloud Console). If you don’t have a project yet, create one in the console. The Compute Engine API must be enabled for your project—the console will prompt you to do this when you first try to create a VM.
To create a VM instance:
- Go to the Compute Engine section in the Cloud Console.
- Click Create Instance.
- Name your instance (e.g.,
php-server). - Choose a region and zone (pick one close to your users).
- Select a machine type (the default e2-micro is fine for testing).
- Choose a boot disk: Ubuntu 22.04 LTS or Debian 12 are good choices for PHP.
- Allow HTTP and HTTPS traffic (check the boxes under Firewall).
- Click Create.
Wait for the instance to start. You’ll see an external IP address assigned—that’s your server’s public address.
Connecting via SSH
There are two main ways to connect: using the built-in SSH button in the console (opens a browser-based terminal) or using the gcloud command from your local terminal:
gcloud compute ssh php-server --zone=your-zone
This command handles key generation and authentication automatically. If you prefer using PuTTY or another SSH client, you’ll need to generate an SSH key pair and add the public key to the instance metadata—but the built-in method is simplest for beginners.
Installing PHP and a Web Server
PHP is a server-side scripting language, so you need a web server to serve the PHP files. The two most common are Apache and Nginx. Here’s how to install both on Ubuntu/Debian:
Option 1: Apache (simpler for beginners)
sudo apt update
sudo apt install apache2 php libapache2-mod-php php-mysql
sudo systemctl restart apache2
Apache automatically configures PHP as a module, so it works out of the box. Your document root is /var/www/html.
Option 2: Nginx (better for high traffic)
sudo apt update
sudo apt install nginx php-fpm php-mysql
Nginx doesn’t handle PHP natively; it passes requests to PHP-FPM (FastCGI Process Manager). You need to edit the server block configuration to enable PHP. For example, in /etc/nginx/sites-available/default, add this to the location / block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
Then test and reload: sudo nginx -t && sudo systemctl reload nginx.
Verifying PHP Installation
To confirm PHP is working, create a test file in the document root:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Then open your browser and go to http://YOUR_EXTERNAL_IP/info.php. You should see the PHP information page. If you see a blank page or the code as plain text, something is wrong with the configuration. After testing, delete the file for security: sudo rm /var/www/html/info.php.
Deploying Your PHP Application
You can upload your PHP files using SCP or SFTP. For example, from your local machine:
scp -r /path/to/your/project/* user@EXTERNAL_IP:/var/www/html/
Make sure the web server has read permissions on the files. If you’re using a framework like Laravel, you’ll need to set the document root to the public folder and configure environment variables. For a simple PHP app, just placing files in /var/www/html works.
If your app uses a database, install MySQL or MariaDB: sudo apt install mysql-server, then secure it with sudo mysql_secure_installation. PHP’s mysqli or PDO extensions are included in the php-mysql package.
Configuring Firewall and Security
By default, Compute Engine instances have a firewall rule that only allows SSH (port 22). When you checked “Allow HTTP traffic” during creation, a rule was added for port 80. If you need HTTPS, you’ll have to add a firewall rule for port 443. You can do this in the VPC network section of the console, or via gcloud:
gcloud compute firewall-rules create allow-https --allow tcp:443 --target-tags=http-server
For production, consider these security measures:
- Disable root SSH login and use SSH keys.
- Keep PHP and the web server updated:
sudo apt update && sudo apt upgrade. - Remove the
info.phptest file. - Set proper file permissions (e.g., 644 for files, 755 for directories).
- Use HTTPS with a free certificate from Let’s Encrypt (install with
sudo apt install certbot python3-certbot-apachefor Apache).
Common Issues and Troubleshooting
Here are typical problems you might encounter:
- PHP code shows as text: The web server isn’t processing PHP. For Apache, ensure
libapache2-mod-phpis installed and restart Apache. For Nginx, check the fastcgi_pass path matches your PHP-FPM socket version (e.g.,php8.1-fpm.sock). - Connection refused: The firewall isn’t allowing traffic on port 80. Check your firewall rules and ensure the VM has a tag that matches the rule.
- Permission denied: The web server user (www-data) can’t read your files. Change ownership:
sudo chown -R www-data:www-data /var/www/html. - PHP version mismatch: Some extensions may not be available for the installed PHP version. Use
php -vto check, and install the appropriate packages (e.g.,php8.1-mysql).
Frequently Asked Questions
Can I use a pre-configured PHP image instead of installing manually?
Yes, Google Cloud Marketplace offers one-click deployments like “LAMP stack” or “WordPress” that include PHP. These are great for quick setups, but you have less control over the configuration. For learning or custom setups, manual installation is better.
How do I set up a custom domain name?
Point your domain’s DNS A record to your VM’s external IP address. Then configure the web server’s virtual host (Apache) or server block (Nginx) to respond to that domain. You’ll also need to set up HTTPS for a secure connection.
What is the difference between PHP-FPM and mod_php?
mod_php runs PHP inside the Apache process, which is simpler but uses more memory. PHP-FPM runs PHP as a separate service, which is more efficient and works with Nginx. For small sites, either is fine; for high traffic, PHP-FPM is recommended.
How do I change the PHP version?
You can install a specific version using sudo apt install php8.2 (if available in the repository). For Ubuntu, you may need to add a PPA like ppa:ondrej/php. After installing, update the web server configuration to use the new version.
Can I run PHP on a Windows VM?
Yes, you can install PHP on Windows Server with IIS, but the process is more involved. Most PHP deployments on Compute Engine use Linux because it’s easier, cheaper, and more compatible with common tools.
How do I back up my PHP application?
Back up the files in /var/www/html and the database (using mysqldump). You can use Google Cloud Storage for off-site backups, or set up a cron job to copy files to another location.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Unbox ASMR Codes: What They Are & How to Use5.00 min/km
- Repurposed Old Motorola Phone for Homelab?
- How to Evaluate AI Vendors for Data Security, Privacy & Compliance
- Best Ergonomic Mouse: Top Picks & Buying Guide
- Captions in Google Slides: How to Add & Format Them
- Use Mac Type in Japanese? Full Guide