Header

How to deploy a PHP website for free?

PHP, Tutorials, and VPS

Post Image

Getting your PHP application online quickly and efficiently without breaking the bank is a common goal for developers and small businesses. The good news is that you can achieve just that by combining the generous free tiers of AWS Lightsail and DeployHQ. This guide will walk you through setting up a free environment for your PHP application, from server provisioning to automated deployments.

Why AWS Lightsail and DeployHQ are Your Go-To Free Solution

AWS Lightsail simplifies cloud infrastructure, offering virtual private servers (VPS) with everything you need—compute, storage, and networking—in easy-to-understand, low-cost plans. Its free tier is particularly attractive for new projects, providing 750 hours per month free for 90 days on select Linux/Unix plans. This is ample time to run a small instance continuously for three months, complete with a static IP for a consistent public address and a generous bundled data transfer allowance.

Complementing Lightsail, DeployHQ is a fantastic service that streamlines the process of moving your code from your repository to your server. Its free plan is perfect for personal projects or single applications, offering one project, up to 20 deployments per month, and 30 build minutes. A key feature is its auto-deploy on push capability, which means you can set it up once, and DeployHQ will automatically deploy your changes whenever you push to your Git repository. By leveraging these free offerings, you can establish a robust, automated deployment pipeline for your PHP application without incurring initial costs.

A Step-by-Step Guide to Deploying Your PHP Application

The journey begins by setting up your AWS Lightsail instance. If you don't already have one, create an AWS account, keeping in mind that the Lightsail free tier benefits apply to accounts that started using Lightsail on or after July 8, 2021. Once logged into the Lightsail console, choose to create a new instance. Select your preferred AWS Region, ideally one close to your target audience for optimal performance. Crucially, opt for the "LAMP (PHP)" blueprint under the "Apps + OS" tab; this pre-installs Linux, Apache, MySQL, and PHP, significantly reducing your setup time. Be sure to select a plan that falls within the free tier, such as the $3.50/month or $5/month Linux/Unix plan, and give your instance a memorable name before creation.

To ensure a consistent public address for your application, attach a static IP to your Lightsail instance. Your instance's public IP address will otherwise change every time you stop and start it. You can do this from the "Networking" tab on your Lightsail instance management page. Next, connect to your instance via SSH using the browser-based client from the Lightsail console. For Bitnami LAMP stacks, you'll typically find the default application password (often for MySQL and other applications) by using the cat command:

cat /opt/bitnami/bitnami_application_password

or

cat $HOME/bitnami_application_password

Make a note of this password. Remember that your web root for PHP applications on a Bitnami LAMP instance is usually /opt/bitnami/apache2/htdocs.

Before moving to deployment, ensure your PHP application is ready. It should be stored in a Git repository (GitHub, Bitbucket, GitLab, or self-hosted), and your project structure should place your index.php or main entry point in a public-facing directory, such as public_html or a public subdirectory.

Now, it's time to set up DeployHQ. Begin by creating a free DeployHQ account. Then, create a new project and connect your Git repository, selecting the branch you intend to deploy (e.g., main or master). Proceed to configure your server within DeployHQ. Choose "SSH/SFTP" as the server type, and for the hostname, enter the static IP address of your AWS Lightsail instance. The username will typically be bitnami for a Bitnami LAMP instance, or ubuntu/ec2-user for other Linux images. For authentication, select "SSH Key." You'll need to generate an SSH key pair within DeployHQ and add the public key to your Lightsail instance. This involves creating the .ssh directory if it doesn't exist:

mkdir -p ~/.ssh

Then, set correct permissions:

chmod 700 ~/.ssh

Open or create the authorized_keys file:

nano ~/.ssh/authorized_keys

Paste the public key from DeployHQ, save, and then set correct permissions for authorized_keys:

chmod 600 ~/.ssh/authorized_keys

The deployment path on your Lightsail instance, where your application files will reside, is typically /opt/bitnami/apache2/htdocs/ for a Bitnami LAMP stack. If your PHP application has a subdirectory that serves as the web root (e.g., public), specify it as the deployment subdirectory. Finally, save your server configuration.

For a smoother workflow, consider configuring optional deployment options. If your PHP application uses Composer, you'll likely want to run composer install --no-dev after deployment. You can add this as a build command in your DeployHQ project settings, ensuring it executes on DeployHQ's build server before files are transferred:

composer install --no-dev

To automate the process, enable "Automatic Deployment" for your chosen branch under your server settings. This means a push to that branch in your Git repository will automatically trigger a deployment.

With everything configured, you're ready for your first deployment. Navigate to your project in DeployHQ and click the "Deploy" button. DeployHQ will connect to your repository, execute any build commands, and then transfer your files to your Lightsail instance. Once the deployment is complete, open your Lightsail instance's static IP address in a web browser, and you should see your PHP application live!

Expanding Your Free-Tier Capabilities

Even within the free tier, you have options to enhance your setup. You'll likely want to point a custom domain name to your application, and Lightsail provides built-in DNS management for easily adding A records that point to your static IP. If your PHP application requires a database, while Lightsail offers managed databases with associated costs, for smaller applications, you can install MySQL directly on your Lightsail instance, provided your usage remains within the free instance's resources. For better security, manage sensitive information like database credentials using environment variables, either directly on your Lightsail instance or within DeployHQ's build pipeline.

By combining AWS Lightsail's free tier with DeployHQ's free plan, you gain a powerful, automated, and cost-effective solution for deploying and managing your PHP applications. Get started today and experience the simplicity of modern web deployment!

A little bit about the author

Facundo | CTO | DeployHQ | Continuous Delivery & Software Engineering Leadership - As CTO at DeployHQ, Facundo leads the software engineering team, driving innovation in continuous delivery. Outside of work, he enjoys cycling and nature, accompanied by Bono 🐶.

Tree

Proudly powered by Katapult. Running on 100% renewable energy.