practice php on ubuntu laptop
I only have a laptop with ubuntu 20.04 installed.
I am anxious to learn php but the book I have only explains how to install php and set up local server for Window and Mac users. I found a stack overflow page that explains how but it is 10 years old and I decided to ask this question.
So could you please explain how I can set up a local server and practice and test php programming?
Use the builtin webserver from php
:
Installation:
sudo apt install php-cli
Usage (From inside your test projects directory):
php -S localhost:8000
Then point your browser to localhost:8000.
It will automatically load the index.php
file if there is any.
You can create it with any text editor and start programing:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
There are three things you will need for this:
- an Internet connection (to install software)
- Apache
- PHP
Here's how you can get a bare minimum installed and running on your system:
- Open Terminal (if it's not already open)
- Update
apt
:sudo apt update
- Install Apache:
sudo apt install apache2
- Test Apache works by opening a browser and visiting http://127.0.0.1. You should see something that looks like this:
- Install PHP (and some common development libraries):
sudo apt install php libapache2-mod-php php-dev php-xml php-json php-mbstring
- Restart Apache:
sudo service apache2 restart
- Create a test file in the Apache root directory:
sudo nano /var/www/html/testing.php
- Paste the following into the new file:
<?php phpinfo(); ?>
- Test PHP works by visiting http://127.0.0.1/testing.php in your browser. You should see something like this: Your version number will likely be a bit different, as this was done on a 21.10 installation.
- Start learning PHP 👍🏻
That's all there is to a basic setup with just Apache and PHP.