Cookie

Start a webserver quickly with PHP

Published by TheJoe on

Estimated reading time: 3 minutes

I wasn't at home yesterday… and I remembered that I have an important file in my NAS, at home. I can access my NAS via the VPN I have activated, but the document is found in a path that I didn't remember at the time, instead, among the few things I remembered was that the file name was a generic progressive name, therefore not very useful if you have to look for it on the command line. What to do?

PHP is a programming language with multiple features. Although I don't know how to program in PHP, I remembered a function that was very useful to me in this case. This is a terminal command that allows you to start a web server with an enabled PHP interpreter. We don't have to install Apache, Nginx or whatever… PHP will suffice.

Do not you believe it?

Well, In the meantime, let's start installing the latest release of PHP (Let's take a Debian or derivative distribution as an example (but PHP can be installed on any distribution):

$ sudo apt install php8

At the end of the installation we will have our PHP installed. Let's check the version with the following command:

$ php --version
PHP 8.0.30 (cli) (built: Aug 23 2023 12:00:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies

Well: PHP is installed and ready to use. Please note that we have not installed any web server (Apache, Nginx or whatever), but only the PHP interpreter.

Now let's start the development server with a single command:

# php -S 0.0.0.0:4000

This command (run as administrator) opens to all incoming connections on the port 4000 in the directory we are in. We go to the browser and type the server IP. Let's see if it works:

An error 404 not found. Let's check the terminal:

[Sat Jan 27 17:27:42 2024] PHP 8.0.30 Development Server (http://0.0.0.0:4000) started
[Sat Jan 27 17:27:44 2024] xxx.xxx.xxx.xxx:49893 Accepted
[Sat Jan 27 17:27:44 2024] xxx.xxx.xxx.xxx:49893 [404]: GET / - No such file or directory
[Sat Jan 27 17:27:44 2024] xxx.xxx.xxx.xxx:49893 Closing

From the terminal we see the classic debug of what is happening on the browser. I made a request for the root directory “/“. It didn't end well (because I don't have a file index.php in quella directory), but I don't care because the server started and the error 404 it means it is working as it should.

Look here:  Google photo: "Dear users ... the free ride is over!"

I would instead like to list the contents of the directory as in a repository and recursively all the subdirectories. Searching through online resources I find a small project hosted on GitHub: Tiny File Manager. It is precisely a single PHP file that lists the contents of the directory in which it is located and recursively all the child directories. Note that it supports two different authentications (with and without administrative privileges) and supports file uploading and downloading. The project is well documented, installation is simple and the result is guaranteed. It has some small addictions:

$ sudo apt install php8-fileinfo php8-iconv php8-zip php8-tar php8-mbstring

Final thoughts

I haven't written it anywhere yet, but I hope it is clear that this procedure for quickly starting a webserver should only be used on a LAN and never exposed to internet traffic, much less used in any company for production. Let's say that you can sleep relatively soundly if it is used over a VPN.

I think the same thing could be done, but without PHP interpreter with Python… I have to check.


TheJoe

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.