title | subtitle | date | tags | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
PHP and Web Dev Phobia |
Dynamic web pages are easier (and harder) than you think |
2024-09-03 |
|
PHP is, for better and for worse, the Python of web dev in my eyes. It is exceptionally easy to get started, in a way which I think younger developers may not be fully aware of.
So here I'd like to make them aware of it! That's right, this is a Slowstart for people who have never touched PHP or web dev before.
Start the way we usually do on this blog, with the "tutorial-in-a-box" by installing Vagrant and Virtualbox so you can create a disposable virtual machine with just a few commands.
mkdir tutorial/
cd tutorial/
vagrant init debian/bookworm64
This will create tutorial/Vagrantfile
, which is a neat
little recipe for spinning up new VMs ad nauseam.
We only have one change we want to make to that file so we can see what we're actually building, and that is to uncomment this line:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
config.vm.network "forwarded_port", guest: 80, host: 8080
Everything else we're going to do inside the box itself. Run
vagrant up
vagrant ssh
If vagrant up
complains about a port 8080 already being
used, just change it to 12345
or something. If you
ever want to start over from scratch, just run
vagrant destroy --force; vagrant up; vagrant ssh
in that same tutorial/
area, and you'll get a fresh new
VM to hack on.
Now we should be inside our VM. Run the following block of Bash commands to install PHP and a lightweight web server:
# While SSH'ed into the Debian VM
sudo apt-get update
sudo apt-get install -y lighttpd php-cgi
sudo lighty-enable-mod fastcgi
sudo lighty-enable-mod fastcgi-php
sudo systemctl restart lighttpd
At this point, if you go to localhost:12345
in your
local web browser, you'll see the Lighttpd Placeholder
page. Great!
We can get a basic HTML page up and running by doing
# While SSH'ed into the Debian VM
cd /var/www/html/
sudo mv index.lighttpd.html index.php
sudo nano index.php # or vi, etc
and changing the content to be
<h1>Hello, world!</h1>
<?php phpinfo(); ?>
Reload localhost:8080
(12345
, etc) one more time
and you should see a screen with some HTML and
some dynamic content generated by PHP itself.
Believe it or not, that's all you need with PHP to get a basic dynamic web page running. PHP is web shell, after all, and we should expect it to be (almost) as user-friendly as the ordinary Bash shell is, whatever that means to you.
The purpose of this little exercise is definitely not to imply you should do things this way. Rather, for a much longer time than I want to admit, I had an irrational fear of really diving in and learning or doing anything with web technologies, because over the past ~30 years we've learned an awful lot of what not to do -- most of that coming from the many footguns inherent in early PHP. At the same time, I knew plenty of web devs, and never really squared the circle as to how they could be so bad at programming and still make so much money, whereas teen me had trouble landing any kind of long-term work at all with my ... purely local Python scripting skills, I guess?
Nowadays I know better. Web dev has, and had, both a low barrier to entry, and a sky-high skill ceiling; but the reason there are so many talented web devs today is in part because so many of them got started many years ago with a process not too much more complicated than this. For all its faults, PHP is extremely fast to just get started and making things with, and I feel it's a useful historical lesson at least to see this with your own two eyes. Even if you never actually use PHP for anything in the real world.