LAMP on Raspberry

Published by Adam Pielatowski on

This post is the last part of manual for preparing Raspberry Pi as web server from scratch. Previous parts were:

Before we start

Every administrator should at least one time prepare documentation of installing web server, so do I! 🙂 In this manual we consider case in which we have headless Raspberry, so we use only SSH to communicate with it, there is no monitor or keyboard attached.

LAMP server contains of:

  • Linux (we have it already, it’s our Rasbian)
  • Apache – open source http server
  • PHP
  • MySQL/MariaDB – we prefer second one, because of license problems in MySQL caused by Oracle.

Update your respository databases with command:

sudo apt update

Install Apache

This is the easiest part. Run command:

sudo apt install apache2 -y

You should be able to open default webpage now. Enter IP address of your Raspberry into address bar of your Internet browser and “Apache2 Debian Default Page” will appear.

You can now redirect port on your router. We will need 80 and 443 to point on Raspberry. Your domain should also point on your external IP address (I mean WAN address of your router). To check if it is working provide your external address in your browser.

Before we make any changes to Apache configuration let’s move on to PHP!

Install PHP

Here the command is also very simple:

sudo apt install php -y

That’s it. PHP is installed. You can check it by creating sample file which will show current PHP settings:

echo "<?php phpinfo(); ?>" >> /var/www/html/phpinfo.php

In your browser address bar put IP_ADDRESS/phpinfo.php and violet page with PHP information should appear.

Your server is looking first for file named index.html . It is default setting but in most cases we use index.php, so we can change an order in which apache is looking for file to open. Run command:

sudo nano /etc/apache2/mods-enabled/dir.conf

and move index.php to the first position like here on the image:

Install MySQL (actually MariaDB)

We are installing MariaDB but don’t get confused in future paragraphs, because MariaDB uses the same syntax and even start command is “mysql”. Everytime we talk about MySQL, we mean MariaDB.

sudo apt install mariadb-server php-mysql -y

and restart apache:

sudo systemctl restart apache2

MySQL needs configuration using following script script with simple questions:

sudo mysql_secure_installation
  • Enter current password for root (enter for none): press enter
  • Set root password? Type Y and enter
  • New password: type root password for MySQL. It’s not the same as your sudo password and should not be the same.
  • Remove anonymous users: type Y and enter
  • Disallow root login remotely type Y and enter
  • Remove test database and access to it. type Y and enter
  • Reload privilege tables now type Y and enter

Install phpMyAdmin

Sooner or later you will need handy tool to navigate through your databases. phpMyAdmin is perfect candidate for you.

sudo apt install phpmyadmin -y

phpMyAdmin will be available using your domain or IP address with alias /phpmyadmin (i.e. In my opinion it’s not secure to leave it like that, so we will change this alias to something not so “default”, for example: /myphpadminpanel:

sudo nano /etc/phpmyadmin/apache.conf

and change alias in line 3 to be as follows:

# phpMyAdmin default Apache configuration

Alias /myphpadminpanel /usr/share/phpmyadmin

Setup Virtual Host

Now we will set up virtual host, which means a website we want to run on our webserver. You have to repeat these steps for every new page you want to add. In this point I assume that you have redirected ports (80, 443) and your domain. I will use variable “your_domain” but it doesn’t need to be exactly domain, it can be a shortcut ( => pielatowski)

First create a folder where your webpage will be stored:

sudo mkdir /var/www/your_domain

Set permissions to this folder to make changes in it without sudo command:

sudo chmod -R 755 /var/www/your_domain

and create test file to be shown:

echo "<h1>Hello World</h1>" >> index.html

We are ready to create virtual host configuration file:

sudo nano /etc/apache2/sites-available/your_domain.conf

and put the following code (of course change your_domain in appropriate lines and email address):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save the file and exit (ctrl + o and ctrl + x). Your website is ready to be enabled:

sudo a2ensite your_domain.conf

and reload apache:

sudo systemctl reload apache2

If you enter your_domain in web browser you should see “Hello world” which is the test file we created before. Working? Great! You can disable default web page, we will not need it anymore:

sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Install WordPress

Most web pages are run on WordPress nowadays so we will install it here to prove our server is fully functional. You don’t need to follow this chapter if you are going to install Magento, Redmine or your own web page built from scratch.

First we need to create MySQL database. Type this command and use the password you created during MySQL configuration:

sudo mysql --user=root -p

Create database:

CREATE DATABASE wordpress_db;

Create wordpress user and give him access to wordpress_db:

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';

Exit mysql using:


Download and extract WordPress

Go to your_domain directory and remove test file:

cd /var/www/
rm -f *

Download lates WordPress with command:



tar xzf latest.tar.gz

move wordpress files from wordpress folder to current folder:

mv wordpress/* .

and delete unnecessary files:

rm -rf wordpress latest.tar.gz

In the end you need to change ownership of the files to apache user:

chown -R www-datar:www-data /var/www/your_domain/


If you want to use permalinks for your posts (probably you want) you will need to enable rewrite module in apache:

sudo a2enmod rewrite

and edit your site configuration:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add lines 2-4 after declaration to your file:

<VirtualHost *:80>
        <Directory "/var/www/your_domain">
                AllowOverride All
    ServerAdmin webmaster@localhost

and restart apache:

sudo systemctl restart apache

Configure WordPress

It’s time to run WordPress. Go to Internet Browser and put your_domain address. You should see page with language selection. Select your language and click continue. On the next screen hit “Let’s go” button and now fulfill information using settings you made in previous steps.

After clicking “Submit” button your WordPress is ready to use. Congratulations!

Secure web page using SSL certificate

Many LAMP server manuals stop here, but I want to present you wonderful tool to quickly and easy install SSL certificate for your website. This tool is certbot .

First install certbot:

 sudo apt-get install python-certbot-apache && sudo apt-get install certbot 

Now run certbot:

 sudo certbot --apache 

Certbot will ask you which domain you want to protect and if you want to redirect to https automatically. I recommend to enable this option and to secure both your_domain and subdomain www.your_domain.

Check temperature

As I mentioned in part 1, I wasn’t sure if the cheapest heatsinks would be enough. Let’s check temperature of the CPU with idle WordPress running:

sudo vcgencmd measure_temp

Do you think it’s not easy to remember this command? I agree, so let’s make an alias!

echo "alias temp=\"sudo vcgencmd measure_temp\"" >> ~/.bashrc

Now every time you type “temp” you will get temperature of your Raspberry!


Paweł · February 21, 2020 at 23:58

Całkiem spoko artykuł! Dzięki!

    pielat · February 21, 2020 at 23:59

    Dziękuję 🙂

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *