Introduction
[This guide has been fully updated in 2019 to reflect the changes to MySQL in Raspbian “Stretch” v9.x and the FTP issues have been fixed] You wouldn’t think a little PC like the Raspberry Pi 3 could handle the job of being a Web Server, but it does, and performs flawlessly too. Today I will show you how to source genuine Raspberry Pi 3 hardware, then setup a fully fledged LAMP Server (Linux, Apache, MySQL, PHP) along with instructions to install WordPress. You’ll also know how to access your web server via FTP, SSH and PHPMyAdmin. We will be using the latest version of Raspbian “Stretch” v9.x.Contents
- Introduction, Hardware, Download & Burn Raspbian Image
- Setup Raspbian
- Install LAMP Server Packages, Setup FTP
- Install WordPress, Summary
Hardware
My first experience buying a Raspberry Pi 2B didn’t go very well, I unwittingly purchased a fake knockoff from China via eBay that didn’t even work, needless to say I was very disappointed. Since then I have learned you have to be very careful where you buy any Raspberry Pi hardware. There are several official Raspberry Pi hardware suppliers that I’ll list below, but first let’s go through the list of parts you will need for this project.- Raspberry Pi 3 motherboard/SBC
- A suitable case for your Pi (I like metal cases)
- 8GB (or more) MicroSD card class 10
- Raspberry Pi Micro USB power supply
- LAN Network cable




- Raspberry Pi 3 Official & Authorized Resellers
- Element14 (main website)
- Farnell/Element14 World Wide Authorized Resellers
- RS Components (choose your country)
- Sources for Suitable MicroSD Cards
- Predefined eBay.com search for Sandisk Ultra 8GB MicroSD cards
- Predefined eBay.com search for Sandisk 32GB MicroSD cards
- Search for Sandisk 32GB MicroSD cards on Google Shopping
- Raspberry Pi 3 Cases
- Predefined eBay.com search for Raspberry Pi 3 Metal Cases
- Search for Raspberry PI 3 Metal Case on Google Shopping
Download & Burn Raspbian Image
Download Raspbian
Raspbian is the official operating system for the Raspberry Pi, and based on the excellent Debian Linux distribution. Raspain comes in to flavors: full Raspbian with the Pixel desktop, or Raspbian Lite with no desktop, which is the one we want. So go ahead and visit the Raspbian Downloads page, download the Lite version .zip file (if you have a BitTorrent client, choose the .torrent as it’s faster).Burn Raspbian to SD Card
Once you have finished downloading the Raspbian Zip file, follow the instructions below to burn the IMG file to your MicroSD card.- Unzip the .img file and take note of the file location
- Visit etcher.io and download the Etcher Image Burner for Windows, MAC or Linux and install
- Insert your MicroSD card into your PC’s card reader, take note of the drive letter assigned to it
- Run Etcher and select the .img file you unzipped earlier
- Make sure Etcher has selected the correct drive letter, if not then change it to your SD card’s drive letter (Important!)
- Once you’re sure the right .img file and drive letter is selected, click the Flash! button
How to flash MicroSD card with Etcher Image Burner - Once Etcher has finished, remove the MicroSD card and insert into your Raspberry Pi with the terminal contacts facing up as shown below
How to insert a MicroSD card into a Raspberry Pi 3 - Now connect up a USB keyboard (mouse not required), HDMI, LAN network cable and have the power supply plugged into the power socket and unit but don’t turn it on just yet
I think this tutorial is great, and I’m using it this evening to learn about LAMP servers and about my RPi 3. I’m basically new to Linux and while not “new car smell” new, I’m wondering how exactly you envision connecting SFTP? Just since i’m playing around, I figure the SSH-FTP route would be the way to go, but not sure how I’m supposed to use connection data. Are you suggesting using WinSCP or similar which takes the IP address, port, protocol, login, and password? I’m just not clear how that login process is supposed to happen. I’ll dig around on your site some more, but how you’re intending us to apply that information is unclear (I have a guess on some options, but your intention might be easier than what else I might try). Still, I greatly appreciate the post!
Hi Scott, thanks for your question and feedback. In this tutorial I give two ways to connect to your RPi; (1) via SSH-FTP (SFTP) which is a bit dangerous as you’ll login as root and have access to entire filesystem or (2) install and setup VSFTPD then login using FTPS which is the better option. The client for both connection types is PuTTY, but any FTP client should do. FTPS is FTP over a secure SSL or TLS encrypted connection, whereas SFTP or SSH-FTP is data transfer over the SSH connection. The reason I recommend FTPS is you can lock-down the folder access to just the
/var/www
folder, whereas the SSH-FTP gives access to the entire filesystem, I realize I could have created a separate Linux user, but it’s difficult to lock down to the/var/www
folder, and permissions are a nightmare. Cheers! RichieThank you Richie. The precaution of using VSFTPD makes sense both in your reply above, and in the main article for the security reasons you cited. For my purposes, which is just trying it out at home, the SSH-FTP route is just fine, but I understand your point and its validity.
One more point, which I think I have right and I’ll make here for anyone passing this way that momentarily is confused on that last section: Although you’re saying to use root for login after changing the sshd_config file, root is not enabled by default on Debian (which Raspbian is based) distributions. You would need to enable the root account, which is not covered in the procedure above.
Hi Scott, yes VSFTPD is the better/safer option. I actually do go through changing the
AllowRootLogin without-password
toAllowRootLogin yes
under the “Login to FTP as Root User” section, but you may not have read it as you went down the VSFTPD path 😉 Cheers, RichieMy bad…. I missed it…. you have a link to FileZilla in the section just above where I jumped to…. thank you!
I think a great addition to this would be to include a web IDE like Codiad into the tutorial. It allows a new user to get into the web development without getting too deep into setting up an IDE in Linux. This also allows allows the user to code their web page from other devices. I built a WAMP + Codiad + phpMyAdmin and will try to build the same on my R-Pi.
I would love to do many more RPi tutorials, time is the factor 😉 cheers, Richie
Thank you very much for this tutorial.
During the Install MySQL you mention “note* that you will be asked to enter and confirm a root password during the installation process”, this is not my case the process it doesn’t request MySQL password.
Late in the Setup WordPress Database request the above password, but I don’t know.
“Login to MySQL as root using the same MySQL root password we entered when installing it earlier”
Could you please tell me how to get the root MySQL password in your installation process?
Hi Angel, apparently Debian/Raspbian now has MariaDB as the default database instead of MySQL engine. This has made the setup slightly different in that no root password is given. Basically, instead of using the old
-u root -p
command you simply usesudo mysql
or if you’re already logged in as root just typemysql
to bring up the interface. AFAIK MariaDB has no root password making it impossible to actually login as root as a security measure, you need to login with a user that has lower permissions (ie. user of a particular database) or follow this guide on how to give root as password.Let me know how you go.
cheers!
Richie
Hi Richie
Thanks for the great tutorial!
I can’t access the phpmyadmin site… how can I delete it on the raspberry to install it again?
Hi Michu, are you sure you put in the right web address? Remember you need to find the Raspberry Pi IP address with
ifconfig
ORip address
to find your IP address, then enter http://[ip-address]/phpmyadmin/ so be sure you try that first.If you still want to uninstall PHPMyAdmin, login as root and enter the following code on line at a time:
dpkg -P phpmyadmin
rm -f /etc/apache2/conf.d/phpmyadmin.conf
service apache2 restart
Cheers!
Richie
Hi Richie,
Thanks for putting this tutorial together! I was going through the steps using Raspbian 9.3 and encountered a problem with http:///phpmyadmin/ similar to Michu after completing the phpmyadmin install instructions above. I corrected the 404 not found by including an extra step:
nano /etc/apache2/apache2.conf
At the bottom of the file append:
Include /etc/phpmyadmin/apache.conf
Save and quit, then restart the service:
service apache2 restart
These steps made the phpmyadmin web interface login available.
Hi Joshua, thanks for the info. The issue is due to Debian changing the default MySQL engine from MySQL to MariaDB in version 9.x which is why I link directly to version 8.x Jessie in the guide. I will have to update the guide to reflect these change as soon as I can.
cheers
Richie
I think something has changed in the way phpmyadmin is set up, I have tried several tutorials without success. I can log in as phpmyadmin, but then can’t create a new database (no privileges), and root access is denied:
#1698 – Access denied for user ‘root’@’localhost’
mysqli_real_connect(): (HY000/1698): Access denied for user ‘root’@’localhost’
Googling for a solution offers many, but there is no trace of AllowRootLogin in any of the configuration files……
I’ve done this many times on other machines, but this has be stumped.
Hi Richard, yes it’s actually MySQL that has changed and now does not allow root login via phpmyadmin interface. But you can fix this issue by following this guide: https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
Let me know how you go mate.
cheers!
Richie
Thanks alot for the guide. Please note that default phpmyadmin login details are: phpmyadmin with the password that you provided in the setup prompt.
Please Note* This guide has been fully updated (2018-01-30) to reflect the changes to MySQL in Raspbian “Stretch” version 9.x, which has caused a lot of the code to break. There are a few different aspects to how we create databases in that we run MySQL with the sudo prefix instead of logging in as root. Also, all code is now written to conform to the Linux sudo system which greatly increases security.
Let me know if you have any issues with the new code and instructions, but I think you’ll find it all works far better now.
Cheers!
Richie
awesome tutorial !
everything worked perfect, untill i installed wordpress.
after i Enter database details and then click Submit, i get this error
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.
i am 100% sure i enter the right username and password.
what could the problem be, and how can i fix it ?
Hi Kenneth, thanks mate. This error means that either one or more of the following is incorrect: db username, db password, db host (which should be localhost). When you entered the code to create the database user:
create user 'wordpress'@'localhost' identified by '[enter password]';
did you enter password without the brackets? like this:create user 'wordpress'@'localhost' identified by 'abc123';
? If you’re unsure, create a new database and user as follows:Then enter username: wordpress2, password: abc123, database name: wordpress2, host: localhost, db prefix: wp_ and see if that works mate.
Let me know how you go.
cheers
Richie
that did the trick 🙂
thank you ! and sorry for my noob mistake
No worries at all mate, cheers, Richie
This is great. Thanks for the thorough guide!
However, somewhere around this part it is failing:
“We also need to add user pi to the www-data group, give ownership of the /var/www folder to www-data user and group, change user pi’s home folder to same, and loosen some permissions on the /var/www folder”
the second usermod command, I was getting “the user pi is in use by process: XXX”. So I attempted to boot into the CLI instead to see if I could get it complete the command, I figured perhaps the GUI was causing an issue, as it seems most of this guide is written with CLI /lite installs in mind.
it still does not complete that command.
Do you have any idea why?
Howdy RC, thanks for the feedback mate. the error: “the user pi is in use by process: XXX” is usually because you’re not logged in as root, so be sure to
su
and enter root password. Let me know how that goes and will update the guide. cheers, RichieI’m having the same issue and getting the same error message (“the user pi is in use”). I saw this comment regarding logging in as root as followed that another tutorial for logging in as root because I had never done it before and didn’t know what the password was.
Anyways I logged in as root and was able to complete the command (sudo usermod -m -d /var/www pi). When I log back in as “pi” and run all 4 commands in that section they all appear to go through, however “sudo usermod -m -d /var/www pi” returns the message “No Changes” rather than “the user pi is in use”.
I thought everything had worked, but I still can’t log in to the pia via FTP/Filezilla. The settings match what is in the tutorial, but Filezilla tells me “Connection attempt failed with “ECONNREFUSED – Connection refused by server”.
I’m definitely a beginner level with most of this and just wanted to try learning PHP and MySQL. If there’s more information I can provide I’ll be happy to if I know how to get it.
Hi Phill, thanks for letting me know this, I have fixed the issue by creating a new user called piftp as it seems too difficult to alter user pi while logged in as pi, and I didn’t want to use the nuclear option of logging in as root. I have run through the entire tutorial again and all works 100% now, perhaps you could confirm this for me buy running through it again? cheers! Richie
Hey Richie! Thank you so much for the incredible guide! Just got my first pi 3 and am having a similar issue even with the updated guide using piftp instead of pi. Im still not able to ftp into my pi and get the ECONNREFUSED error. Any idea??
Hi Harrison, no worries at all 🙂 I went through the guide again with a fresh install of Raspbian Buster and it all works ok on my end. Usually this happens because of the FTP client settings, are you using Filezila? And with the following settings?
Also check out the FileZilla network config page and try some of the suggestions there. Let me know how you go. cheers! Richie
Hi,
Thank you very much for this tutorial.
i just have an issue with public IP that wordpress plugin installation error and ask for ftp login details. when i put login detail it says login failed.
please help me…
Thank you.
Hi there, no probs. Did you change ownership of the entire html folder to www-data? You’ll need to enter the following:
chown -Rf www-data:www-data /var/www/html
and then retry installing the WordPress plugin. Let me know how you go mate.cheers!
Richie
Many thanks Richie,
This is exactly the way the internet should be…. knowledge sharing at its best. You have enabled me to acomplish a pretty challenging task (for an old duffer like me) setting up LAMP on the Pi, I am a bit uncertain of what exactly WordPress does, but at least it is now parked on my Pi as I progress setting up the webserver.
One comment for Pi newbies… don’t forget to make a disk image backup of that fragile little SD card after doing all this.
Thanks Heath, glad you enjoyed the tutorial. WordPress is basically a publishing platform, think of it like Word for the web 🙂 You might also be interested in learning more about PHP? If so I can highly recommend Brad Traversy’s free YouTube course PHP Front to Back. Cheers! Richie
Where can I locate the location of the php.ini so I can edit the file? Thank you so much for the great tutorial!
Hi Alan, no worries glad you liked the tutorial! As we’re using Apache’s build-in PHP processor
libapache2-mod-php
the location of thephp.ini
is/etc/php/7.2/apache2/php.ini
but it will depend on which version of PHP you’re running, so replace the7.2
with your version. Runcd /etc/php
and then thedir
command to see what version you have installed.Cheers!
Richie
I tried this but it didn’t work or everything else seems to work but wordpress…booking in the wp doesn’t work for me althoug I quite sure I fill all fields as they shoud. I tried also manualy configurate wp but that didn’t work either. Maybe I try to remove wp user and db and create those again.
Hi Teme, most likely a folder permission/ownership issue or incorrect database details. Make sure you run
chown -R www-data:www-data /var/www/html/*
after downloading and unzipping WordPress, and double check your database details. If you’re unsure, try creating a new database and user, login to mysql:mysql -uroot -p'MyPassword'
then…
create database wordpress2;
create user 'wordpress2'@'localhost' indentified by 'MyPassword';
grant all privileges on wordpress2.* to 'wordpress2'@'localhost';
flush privileges;
exit;
See how you go and let me know if you have success.
cheers!
Richie
Actually now I found out what was wrong….I put password inside these []…total brain fart…now its working fine.
oh well not to worry 🙂
Hello, I got this installed without any real issues and have been testing some installs of some PHP Websites I’m working on. I’m using a Raspberry Pi 3b and of the 5 websites I working on only one is giving me a fit. Please note I have these all running on Production Web Servers and on a Windows EasyPHP setup.
Only one will not run and gives me a 500 error and for the life of me I can’t get it to run.
I was hoping to get everything running on a Pi so I could travel, do installs and run small training sessions.
Thanks for your attention and help.
Hi Bob, sounds like a PHP or Apache issue, without knowing exactly whats causing it you’ll need to look in your Apache error logs:
nano /var/log/apache2/error.log
and track down the error. There is also a guide on analyzing Apache 500 errors.Let me know how you go mate.
cheers!
Rich
Hi
first of all, thank you very much for this great tutorial for setup MariaDB and PHP. Before I fund your tutorial, I had 3 crashed installations of mariadb/mysql,
but now until finishing by the tutorial without any problem!
ok, the database is still not defined, it doesn’t exist until now
my system is a RPi4 with a SSD-USB drive and all the Databases has to be there
until now, I could not find any info’s, how to provide this
where I can find more info’s?
thanks again
Max
Hi Max, not sure exactly what information you’re after mate? So there are 3 databases on the SUB drive that you want to access? cheers! Rich
Thanks so much Richie. I’ve been going round in circles for days following various tutorials, almost at the point of giving up. Hopefully you’ll never hear from me again. Big up.
Dave
Thanks Dave. 🙂 cheers!
Hi Richie
Thanks so much for putting all this together, I’ve been trying for a few weeks now, following various tutorials and them all breaking at various points. Last night I ripped through yours and it’s all working brilliantly. My pi 4 is all wordpressed up 🙂
I’ve got a question if I may; when I try to move a folder (theme) into the server using Filezilla, it won’t create directories and fails. The Filezilla help threads point to server issues and it’s out of their hands. I know from the other tutorials I used, it seemed to always be permission/user errors that killed me off! This is the type of response people with the same problem have been getting-
The router already runs some sort of FTP server software. But that software must be configured correctly, currently it seems to be read only (at least you can’t create directories). This must be fixed on the server end directly, no way to do this through FileZilla Client.
Any ideas
Thanks again
Dave
Hi Dave, are you hooked into a web server via a company (GoDaddy hosting, DreamHost etc) or do you have something like a DigitalOcean droplet you setup yourself? Either way it sounds like a permission issue and your FTP user doesn’t have folder write permissions. If you’re with a hosting company, login to their control panel (possibly example.com/cpanel) and create an SFTP user with full write permissions for the website folder, then delete what you have already uploaded and re-upload with your new SFTP user, see if that solves the issue. cheers! Richie
Thanks for the super swift response Richie 🙂
In short no, I’m not hooked into any webservers companies. I was hoping to run my own server with the pi? I’m at the stage where I followed your tutorial and set up the LAMP server. I was hoping to get my domain name diverted/hooked up to my wordpress site when it’s all done. So, the FTP user is pi?
Apologies for the rookie knowledge!
Cheers
Dave
ahh I see, now you’re getting into DNS and networking which is more complicated, but I’ll guide you through it. You’re home Internet connection likely uses a dynamic IP address, which means it changes every few days or weeks, but if you want to host a web server, you need either a dedicated (consistent) IP address, or a service that automatically updates your website IP when it changes (preferred option). See this guide, this one and this one. You will also have to set this up in your router/modem and give your PI a dedicated local IP address (i.e 192.168.1.100) and make that IP a DMZ zone or port-forward ports 80 and 443 (will depend on what services you’re running, ports 80 and 443 are website ports) to your Pi’s local IP from your WAN.
Hope this helps mate.
cheers!
Richie
Hi Richie
Thanks for the networking info, I have set up some port forwarding settings but will need to do this properly. In terms of the write permissions problem, I’m presuming that it won’t be changed/solved by the networking elements….would this work –
sudo chgrp www-data /html
sudo chmod 777 /html
sudo chmod g+s /html
sudo usermod -a -G pi
sudo chown -R pi /html
Cheers
Dave
Ok try the following:
su [enter root password]
cd /var/www/html
chown -R www-data:www-data /var/www/html
usermod -a -G www-data pi
usermod -m -d /var/www pi
chmod -R 755 /var/www/html/
That should solve permission issues, then try sftp again.
cheers
Richie
After following your guide, how would I go about on enabling Python in Apache and linking it with the MySQL DB? What is the account for the DB is it what we use for phpmyadmin?
Hi Kev, (please read this thoroughly first). Better not to use the phpmyadmin account and instead login to MySQL as root via the command line
mysql -uroot -pMyPassword
then create a new database and user as follows:create database my_new_database;
create user 'MyNewUser'@'localhost' identified by 'MyUsersPassword';
grant all privileges on my_new_database.* to 'MyNewUser'@'localhost';
flush privileges;
exit;
From this point you can access MySQL via localhost, username and database name in your Python scripts. If you wanted to run Python scripts like web pages (index.py) similar to the way PHP runs, you’re better off following this guide as you’ll need FastCGI and mpm_event disabled. I advise the latter.
cheers!
Richie
Great update on this guide. I used a similar one about 2 years ago. Is anyone else having trouble installing apache at the moment? Mirror down??
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/a/apache2/apache2-bin_2.4.38-3+deb10u1_armhf.deb 404 Not Found [IP: 93.93.128.193 80]
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/a/apache2/apache2-data_2.4.38-3+deb10u1_all.deb 404 Not Found [IP: 93.93.128.193 80]
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/a/apache2/apache2-utils_2.4.38-3+deb10u1_armhf.deb 404 Not Found [IP: 93.93.128.193 80]
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/a/apache2/apache2_2.4.38-3+deb10u1_armhf.deb 404 Not Found [IP: 93.93.128.193 80]
hi where do i need to download the package for ftp in /var/www/html or does it need another directory
I may have found the issue, the php version is to high for this script, is there a way to install an older version before version7?
Thanks for your detailed DIY Guide. Will it be possible for you to create a bash script so that I can install LAMP and wordpress within a few minutes without any interaction?
Hi, writing bash scripts is pretty easy, try using the commands in my guide and build one, https://ryanstutorials.net/bash-scripting-tutorial/bash-input.php.
cheers
Richie
Hi Richie
Thanks again for the awesome tut. I’ve had lots of attempts to get it up and running and have largely succeeded. I’ve done the duckdns.org and CNAME pointed my domain to it, set up the Certbot SSL and it’s pretty solid 🙂 I had to change the wp-config settings to this, to resolve weird formatting issues:
define(‘WP_HOME’,’https://192.168.1.108′);
define(‘WP_SITEURL’,’https://www.davidhallows.com’);
Also to solve some ssl issues I did this too:
A simple solution is to force SSL by modifying wp-config.php.
Find:
define(‘WP_DEBUG’, false);
Directly below that, add:
if ( $_SERVER[‘HTTP_X_FORWARDED_PROTO’] == ‘https’ )
{
$_SERVER[‘HTTPS’] = ‘on’;
$_SERVER[‘SERVER_PORT’] = 443;
}
Save wp-config.php and reload the page.
Although it seemed to be rock solid last night, I can’t login to WordPress now, nor will it send me a email reset, I get this weird message –
The email could not be sent. Possible reason: your host may have disabled the mail() function.
So, the really weird bit seems to be the WordPress PHPmyadmin doesn’t show a WordPress table. Lots of fixes resolve around the access to that but I just have this in the table field:
information_schema
phpmyadmin
Any awesome advice or ideas much appreciated.
Thanks again
Dave
Hi Dave, no problems mate. Ok so I will try and answer your questions on a point by point:
That should solve any issues, let me know how you go.
cheers!
Richie
Hiya Richie
I stripped it out and follwed your tut again, changed both URL’s and bingo – http://www.davidhallows.com!! Super happy. Not entirely sure what weird password idea I’d had…I also spent some time on a thread with Let’s Encrypt who helped with the Certbot SSL encryption 🙂 Do you have any back up suggestions, I’d feel gifted if I knew how to reinstall after a bodge.
Thanks for all your help
Dave
Hi Dave, yep sometimes starting over is the best option. I don’t know how many times I’ve done that when writing guides etc to make sure it all works 😉 If you want to backup your database and WP installation, use WP-DBManager and then backup your wp-content folder via crontab. You can then download the compressed file to your local PC or rsync it to another server. There are also backup plugins available for WP.
cheers mate!
Richie
I manage to complete the lamp server but I cant access it on other device in my local network. I am both my other computer and my raspberry are connect to the same internet but can’t see it. Also how do you allow it to see the website on a phone?
Hi Josie, are you typing in the IP address of the Pi? it’s should be
192.168.x.x
or10.0.0.x
or172.16.x.x
. If you login to your Pi and enter this command:ip addr show
it will show your Pi’s IP address, then type that address into your web browsers address bar. If you’re trying to use a domain name, you’d need to setup a dynamic IP system with a company like DynDNS and set up Apache to accept requests for that domain name using VirtualHosts.cheers!
Richie