When creating applications with PHP, unlike HTML files which you can open in your browser, to be able to run your PHP code or view PHP pages in your web browser, a web server is required to process the PHP code and send the results to your browser.
The Apache web server is the most commonly user web server in the world. It provides provides the features necessary for running PHP code among many other features.
In this guide, we will learn how to install a PHP Web Server on any computer running the Linux (Ubuntu or Debian based) or Windows Operating System. We will be using the minimum required configuration so as to make it as easy as possibly to get you started.
Linux
Here we learn to install the Apache Web Server on a Ubuntu or Debian based Linux Operating System. The package which provides the required features is known as apache2
and we will be using the apt package manager to install and configure the package.
Step 1: Open a Terminal Window
First we need to open a terminal window. To do this you can either user your application menu (start menu) and search for the word terminal
or use the keyboard shortcut Ctrl + Alt + T
. A graphical terminal windows should pop right up.
Note: You will need root (administrator) privileges to be able to install the package as the
sudo
command is required when installing new packages. If you are having any problems with this, feel free to contact me and I will assist you as best as I can.
Step 2: Update the System Repository
First let's update the the system repository so that we get the latest version of of apache2
. To do so, from the terminal, execute the following command:
sudo apt update
Step 3: Install apache2
Now we will install the apache2
package. To do so, execute the following command from the terminal:
sudo apt install apache2
During the install process, we may be asked for confirmation by entering the y
(yes) or n
(no) option. Enter y
to confirm, press Enter
and the apache2
package along with all it's dependencias will be installed on your system.
Once installed you can check the version of the Apache Server using the following command:
apache2 -version
Step 4: Configure the Firewall
In this step we will allow access to Apache so that we can view our PHP applications from the browser. To do so run the following command from the terminal:
sudo ufw allow 'Apache'
This is the highly restrictive profile which which only allows network activity on port 80
. You can use the following command to view the list of available profiles:
sudo ufw app list
We will not go in depth on the technicalities of the different profiles as this guid is aimed at getting you started as fast as possible.
To verify that the changes have been applied, you can run the following command:
sudo ufw status
This command will display a message showing all the connections that are allowed. You should now see Apache in that list.
Step 5: Check the Status of the Web Server
Before we continue, let's verify that the Apache Server is working. To do this, execute the following command in the terminal:
sudo systemctl status apache2
The status of the Apache Web Server should appear as active (running)
. And that's it, your web server is now up and you are ready to test your PHP code in your web browser.
Step 6: Test the Apache Web Server
When installed, the Apache Server created a default web page which you can access and view from your web browser. To do so, open your web browser, enter the URL http://localhost/
in the address bar and press Enter
. You should see the Apache welcome page which indicates that the Apache web server is working properly.
Step 7: Create Your Own Pages with PHP
All web pages for the Apache web server are stored in the directory /var/www/html/
from which the the pages are loaded when you enter the URL http://localhost/
into your web browser (index.html
). Let's create our first page.
You can use your File Manager to navigate to the folder or your favorite IDE to create your web pages and save them to that folder.
Windows
If you are running Windows, there are two great options which WAMPServer and XAMPP which are both great options but I believe that WAMPServer is much easier to install and configure to this will be our program of choice.
WAMP stands for Windows Apache MySQL and PHP and is a web development environment for WIndows. Similar to apache2
for Linux as we saw above, WAMPServer will allow you create applications in PHP and run your code directly from your computer.
Let's get to installing WAMPServer on our Windows computer.
Step 1: Download and Install WAMPServer
You can download the WAMPServer installer from the official website over at https://www.wampserver.com/en/. Select the one based on the type of Operating System you have (32-bit or 64- bit). When the download completes, run the installer.
Note:
The newer versions of WAMPServer are not compatible with Windows XP.
You can leave all the options of the installation as is except when asked to select your default browser and editor of choice. It's better to use a modern browser and your favorite editor for editing PHP files.
If a Windows Security Alert appears, be sure to select both the Private networks and Public networks options and then click the Allow access button.
When the installation finishes, check the box that says Start WAMPServer 2 now before clicking the Finish button. You should see the WAMPServer icon in your notification area which means that the programming is running.
The WAMPServer icon changes from red to orange and then to green indicating errors, warnings or a successful start respectively.
Step 2: Verify Your WAMPServer Installation
Let's check of that our server is working as expected. Left click on the WAMPServer icon in the notification area, then click the Localhost option from the menu. Your web browser should open and the default WAMPServer Homepage.
Step 3: Create Your Own Web Pages
All PHP files and web pages of the WAMP server are stored in the directory C:\wamp\www\
from which the pages are loaded. From there you can create your own folders and web pages and access them in your web browser from the URL http://localhost/
.
Note:
If you installed WAMPServer to a different drive other than theC:
drive, be sure to make the changes from the path given above. For example, if it was installed on theD:
drive, then the directory will be located atD:\wamp\www\
and notC:\wamp\www\
.
For example, if we create a folder called welcome\
which would have a path of C:\wamp\www\welcome\
and inside that directory, we create a file called index.php
with the following code:
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1><?php echo "Hello World! It's " . date("Y"); ?></h1>
</body>
</html>
If we open our web browser, enter the URL http://localhost/welcome/
or http://localhost/welcome/index.php
then press Enter
, we should see a message similar to this:
Hello World! It's 2022
Conclusion
Your new web server should now be up and running and you can now build and run your PHP applications directly from your computer.
If you are experiencing any difficulties with your installation, feel free to get in touch with me and I will try my best to help you resolve them as soon as possible.