How to run a PHP script on localhost on Linux

Endrit Qerreti

Endrit Qerreti

You can run a HTML file and Javascript with no issues locally on your browser, but can you run PHP locally? The answer is yes, you can run PHP just like any HTML file.

Running PHP scripts locally is useful, especially for development as you can develop your project locally without having to upload it on your server.

In this tutorial, you will learn how to run a PHP website and PHP scripts locally on your computer.

Step 1 - Install PHP

First thing that we need to do is to install PHP. Installing PHP is simple, and you can install it by using the command below

sudo apt install php

Output

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php8.1 php-common php8.1 php8.1-cli php8.1-common
  php8.1-opcache php8.1-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php8.1 php php-common php8.1 php8.1-cli php8.1-common
  php8.1-opcache php8.1-readline
0 upgraded, 8 newly installed, 0 to remove and 18 not upgraded.
Need to get 5,127 kB of archives.
After this operation, 21.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

As you can see on the output above, PHP will also install the additional packages needed , continue Installing PHP by pressing the Y key.

Step 2 - Verify PHP installation

Once you have installed PHP on your computer, now you need to verify that PHP was successfully installed. We can perform multiple tests, however since we are going to run a PHP script locally we need to know whether the PHP web server can be started by using the command php -S localhost:8080.

So, let's do a quick test and start the PHP web server

php -S localhost:8080

💡

Note: We are using the 8080 port for the localhost server. However, you can use any port you want as long as that port is not being used by another process. For example, you can use port 8000

Once you execute the command above, you should see this output on your terminal

This means the server was started successfully, and you can go to localhost:8080, now feel safe to stop the server for now as this was just a test. Closing the terminal would stop the server. However, if you want to keep the terminal open, you can stop the sever by using the shortcut CTRL +C.

Step 3 - Create PHP script

Now that we know that PHP web server runs perfectly, we can proceed to create the project folder where the PHP scripts will be located.

We are going to create a folder called php-project, this folder will contain the php script that we'll be running locally.

mkdir php-project

Once you create the project folder, let's cd into it

cd php-project

Now, once you are in the php-project folder, you can proceed to create the php file

sudo nano app.php

Next, copy the code below into app.php

<?php

echo "Hello, world!";
?>

Save changes by pressing CTRL + X

Once you press CTRL +X, you will see the following prompt, press Y to confirm

Finally, press enter to save changes

Step 4 - Run PHP

The project that we created above is called php-project, and inside this folder is a php file called app.php. Now, to run the app.php file, we can do so by running the command below

php -S localhost:8080 app.php

💡

Note: There are multiple ways to run the php script. For example, our php script is called app.php and if we start the php web server without specifying which file to serve, which in this case is app.php in the command above, we will not be able to view this file since the server will try to load the index.php file.

So, let's say you want to run a PHP website which has index and all other files, you can do so by running the command below

php -S localhost:8080 

How to run multiple PHP web servers

You can also run multiple PHP web servers on different ports, this is useful in cases when you want to run multiple PHP projects at the same time.

Let's say we have 3 different projects that we want to run, we start each server on a different port as shown in the examples below

#Project1

php -S localhost:8001

#Project2

php -S localhost:8002

#Project3

php -S localhost:8003

As you can see, we have now 3 different servers running and serving 3 different scripts at the same time.

Alternatively, you can start the 3 servers via a bash script, instead of starting them manually one by one.

The bash script below will open 3 terminal and on each terminal it will execute the command to start the PHP web server.

#!/bin/bash


xfce4-terminal -e "php -S localhost:8001"
xfce4-terminal -e "php -S localhost:8002"
xfce4-terminal -e "php -S localhost:8003"

xfce4-terminal is the name of the terminal that we are using, depending on the terminal that you use, you need to change xfce4-terminal with your terminal. For example, if you use gnome Desktop environment, then you can launch gnome terminal via gnome-terminal

How to start PHP web server automatically

There are multiple ways to start PHP web server automatically once you boot your system.Depending on the linux distro you use, the easiest way to achieve this is by creating an entry on Application Startups. For example, on Linux mint, you can create a startup entry on "Sessions and Startup" application, which allows you to run a script automatically depending the trigger method you choose.

1 - Create the bash script that will start the php webserver

#!/bin/bash

php -S localhost:8080

2- Save this script as startserver.sh

3 - Make this script executable

chmod +x startserver.sh

4 - Launch Session and Startup

You can launch Session and Startup from the applications menu, by searching "Session and Startup" on the search box, or via Settings Manager as shown in the image below

5 - Create startup entry for startserver.sh on "Session and Startup"

Name - Name of the application

Description - Set a description if you want to (optional)

Command - This is the command that will run once you login on your system, now since we are going to run a script instead of a command, you need to import the bash script that you created by clicking the import button

Trigger - Choose on Login to run the bash script once you boot your system

Conclusion

In this tutorial, we explained step by step how to install PHP, and how to start a PHP web server locally on Linux. We also explained how to run multiple PHP web servers at the same time when running different projects.