How to Use a Raspberry Pi as a Local Web Server

The Raspberry Pi might look like a small, almost inconsequential computer, but don’t let looks deceive you. It is a fully functional Linux machine capable of performing a large variety of functions including acting as a local web server.

The most common setup for a Linux-based web server is a “LAMP” server, where LAMP stands for Linux, Apache, MySQL and PHP. The default operating system for the Pi is Linux, specifically Raspbian and it can also run Apache, as the web server; MySQL, as a database; and PHP, for server-side scripting.

Before installing the web server, it is best to configure your Raspberry Pi with a static IP address. Follow the “How to set up a static IP address on a Raspberry Pi” tutorial.

The LAMP server components can all be installed from the Raspbian online repositories using “apt-get.” On the command line type the following:

During the installation, you will be asked to enter a password for the MySQL root user. This is the administrator’s password and stops unauthorized access.


When the install has finished, the Apache web server should be up and running. You can test it by opening the default web page at the IP address of you Raspberry Pi. Start a web browser on a PC and enter the “” where “” is the static IP address of your Pi. The resulting web page will look something like this:


You may have noticed an error message at the end of the installation process: “Starting web server: apache2apache2: Could not reliably determine the server’s fully qualified domain name, using for ServerName.


On the Internet, a web server needs a name to tell it what domain it is serving. During startup, Apache looks for that name, and if one isn’t configured, it displays a warning and uses For a local web server running on your private LAN, this isn’t a problem, but to get rid of the annoying message, it is best to configure the name.

Edit the Apache configuration file (/etc/apache2/apache2.conf) using the following command:

Scroll to the bottom of the file and add the following line:

You can actually use any domain name you like, but since your Pi is running as a local web server, “” is as good as any! Exit nano with “Ctrl + X”. Finally the web server needs to be restarted:

The default HTML directory for Apache is “/var/www”. To test PHP, create a file called “testphp.php” in that directory with just a single line, a call to the PHP test function – testinfo(). Create the file using nano:

And enter the following line:

Exit nano. Start a web browser on a PC and enter the “” where “” is the static IP address of your Pi. The resulting web page will look something like this:


Most web platforms, like WordPress, Drupal, Roundcube, MediaWiki  and so on, require a back-end database. When you install a web platform it is often necessary to provide the name of a database and a MySQL user for the software to use. To create a database and a user start the “mysql” command line tool using:

You will be prompted for the root password you set during the install.

To create a database use:

And to create a user use:

Finally, to grant the “mydbuser” user full access to the “mydb” database use:

At this point your Raspberry Pi is running Apache, PHP and MySQL and you have the tools you need to create databases. Just be mindful of the amount of free space on your SD card and everything should work great. If you have any questions please feel free to ask in the comment section below.


  1. You can also use this to administrate a MySQL server installed in Debian.

    $ sudo mysql –defaults-file=/etc/mysql/debian.cnf

    Then you don’t need to add the MySQL root users password on the command line and make it added to the history file and shown in command line through ps with: $ ps -fA

    (And you don’t need to remember the MySQL password ;-)

    1. After executing the command I get ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

  2. Yes, and you might want to run a web server that isn’t as heavy as apache2 is on a Raspberry Pi. But if you don’t have any GUI on the machine, it will be ok anyway.

Comments are closed.