Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages.
We all know that, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.
So, how to Install the Apache on a Raspberry Pi?
First of all, the First Easy Step is update your raspberry pi, by typing the following command into the Terminal:
sudo apt update
Then, install the apache2
package with this command:
sudo apt install apache2 -y
see the image.
You will see the output like this. Just Wait for a while.
And finish.
Now let's check of the Apache is installed correctly.
Test the webserver now, by typing the
http://localhost/
in your browser. or type your Raspberry Pi. , IP Address
To find the Pi's IP address, typehostname -I
at the command line (or read more about finding your IP ADDRESS).
if you type http://localhost/ in your browser, you will see this output. Or type the ip address of your raspberry. See image.
This means you have Apache working!
Now change the default web page.
This default web page is just an HTML file on the filesystem. It is located at
/var/www/html/index.html
.Navigate to this directory. Open your terminal and type the following command:
total 12
drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
drwxr-xr-x 12 root root 4096 Jan 8 01:28 ..
-rw-r--r-- 1 root root 177 Jan 8 01:29 index.html
This shows that by default there is one file in
it is owned by the/var/www/html/
calledindex.html
and iroot
user (as is the enclosing folder).
In order to edit the file, you need to change its ownership to your own username. Change the owner of the file (the default
pi
user is assumed here) using
sudo chown pi: index.html
.You can now try editing this file and then refreshing the browser to see the web page change.
Your own website
If you know HTML you can put your own HTML files and other assets in this directory and serve them as a website on your local network.
Additional - install PHP
To allow your Apache server to process PHP files, you'll need to install the latest version of PHP and the PHP module for Apache. Type the following command to install these:
sudo apt install php libapache2-mod-php -y
you will now see that it will downloading and extracting another packages. After it's finish. (See here)Now remove the
index.html
file:sudo rm index.html
sudo nano index.php
See image below
Put some PHP content in it:
<?php echo "hello world, welcome to cybersecx.blogspot.com"; ?>
(See the example output here)
Now save and refresh your browser. You should see "hello world".
This is not dynamic but still served by PHP. Try something dynamic:
<?php echo date('Y-m-d H:i:s'); ?>
or show your PHP info:
<?php phpinfo(); ?>
Finish...
Comments
Post a Comment