Host PHP for free!

Savvas Stephanides - Oct 2 - - Dev Community

PHP was and remains one of the most popular web application languages out there. However, the choices for hosting a PHP project for free are limited, especially with services like 000webhost shutting down.

Fear not however... You can host your PHP project for free on Render using Docker!

💡 Docker, explained

Here's how:

What you'll need to have installed

Before we begin, make sure you have Docker installed on your machine. Install Docker.

Steps

 Step 1: Create a PHP project

Firstly, we'll going to create a simple PHP project with a simple "Hello world" message:

Create a new directory myPhpProject and point to the directory using your terminal:

mkdir myPhpProject
cd myPhpProject
Enter fullscreen mode Exit fullscreen mode

Next we're going to create our web page. Create a file called index.php using either your terminal or your favourite code editor.

In the index.php file add this PHP code:

<?php
echo "Hello world"
?>
Enter fullscreen mode Exit fullscreen mode

💡 What does this code do? This code opens a "code block" to write PHP code using <?php and ?>. We can write our PHP code inside it. Then we use the echo keyword from PHP to write something on the page. In this case, "Hello world".

Step 2: Create a Docker Hub account

Now let's take a few seconds to set up a free account on Docker Hub. Sign up for Docker

But why? Because in the next steps, we're going to create an image from our PHP project and publish it on Docker Hub. Docker Hub is just a library of pre-existing images like Ubuntu, Python or NodeJS.

Step 3: Create a Docker image

Now we're going to create our Docker image. Go ahead and create a file called Dockerfile in your app directory.

What's a Dockerfile? A Dockerfile is a file which gives instructions to Docker on how to create an image.

Add this to your Dockerfile:

FROM php:7.4-apache
COPY ./index.php /var/www/html
Enter fullscreen mode Exit fullscreen mode

💡 What does this do? Firstly, it gets a pre-existing handy image from Docker Hub which has PHP and Apache already installed. Next it copies the index.php file we created earlier into the html directory. This is where Apache will grab the file and show it to the browser.

Now that we created our Dockerfile, it's time to create our image. Run this command in your terminal:

docker build -t myphpproject .
Enter fullscreen mode Exit fullscreen mode

What does this do? It just tells Docker to grab the Dockerfile, create an image from it and call it myphpproject.

Now that we created our image, let's test it out by running a container from it:

docker run -it --rm -p 80:80 myphpproject
Enter fullscreen mode Exit fullscreen mode

💡 What does this do? It uses our new image, myphpproject to create a container which contains our website. The website will be available from port 80, so after a few seconds you can access it from here:

http://localhost

Step 4: Host image on Docker Hub

Now that our image is ready, it's time to host it to Docker Hub.

Firstly, login to Docker Hub from your Terminal:

docker login
Enter fullscreen mode Exit fullscreen mode

The process should look a bit like this:

Username: savvasstephanides
Password: 
Login Succeeded
Enter fullscreen mode Exit fullscreen mode

Next, we need to give our image a tag. Sort of like a version number:

docker tag myphpproject savvasstephanides/myphpproject:1.0
Enter fullscreen mode Exit fullscreen mode

You can iterate your version numbers for each new version.

Now that our image and tag are ready, we can push our image to Docker Hub, like so:

docker push savvasstephanides/myphpproject:1.0
Enter fullscreen mode Exit fullscreen mode

Your terminal should look a bit like this:

The push refers to repository [docker.io/savvasstephanides/myphpproject]
93b8f571dc38: Pushed
Enter fullscreen mode Exit fullscreen mode

Now log in to Docker Hub in your browser and check out your image.

Step 5: Sign up for Render

Render is a great service that lets you host your projects with a generous free tier. Go ahead and sign up for Render for free.

Create a new Web service on Render

After signing up to Render, go to the Dashboard.

From the Dashoard, select New and then Web service:

Next, choose Existing image:

Under Image URL, write the address of your image like so:

docker.io/<your_docker_hub_username>/<project_name>:<tag_name>
Enter fullscreen mode Exit fullscreen mode

For example:

docker.io/savvasstephanides/myphpproject:1.0
Enter fullscreen mode Exit fullscreen mode

Give your project a name:

Select Free under Instance type:

Click Deploy Web Service:

Give it a few seconds to complete:

Once it's done...

Click the link at the top left that looks like https://abc.onrender.com. Your website is up and running!
🎉

You've successfully published your PHP app online!

Have you hosted your PHP using this tutorial? Paste a link to your project in the comments to show off your creations! Thanks for reading!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player